Login | |
|
 |
Appending a number to a file name problem - 12/19/2006 12:54:06 AM
|
|
 |
|
| |
markmcrobie
Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
|
Sorry, me again! I've spotted a (minor) problem with my renaming script: In the For Each loop that goes through the files 1 by 1 renaming them, if it attempts to rename a file that would therefore end up with a file name the same as one it's already renamed, I've got it to append a number onto the new one, to avoid a "File already exists" crash. I've done it like this (intCounter is set to 0 early in the script, before the For loop) If fso.FileExists(strNewName & strExtension) Then intCounter = intCounter + 1 File.Name = strNewName & intCounter & strExtension Else File.Name = strNewName & strExtension End If So say it's trying to rename a file to "Myfile.jpg", but "Myfile.jpg" already exists - it'll simply rename the 2nd one "Myfile1.jpg". And if it happens again within the loop, "Myfile2.jpg" and so on, as intCounter gets incremented by 1 each time this happens. Perfect, I thought, and I figured out how to do this myself, and I was very pleased with myself ;-) However I've spotted a minor flaw: Say I have "A_Photo.jpg", "B_Photo.jpg", "C_Picture.jpg" and "D_Picture.jpg", and my script is designed to strip the first letter and underscore out. First time round the For loop it'll try to rename "A_Photo.jpg" to "Photo.jpg". It'll see that "Photo.jpg" doesn't already exist, and will rename "A_Photo.jpg" to "Photo.jpg", i.a.w the Else statement above 2nd time round it'll try to rename "B_Photo.jpg" to "Photo.jpg". It'll see this is already exists (due to it having processed "A_Photo.jpg" already), so it'll make intCounter = 1, and rename "B_Photo.jpg" to "Photo1.jpg". This is exactly what I want. 3rd time round it'll try to rename "C_Picture.jpg" to "Picture.jpg". It'll see "Picture.jpg" doesn't exist, and will rename "C_Picture.jpg" to "Picture.jpg". Fine. All good so far. But here's the "but"... ...4th time round it'll try rename "D_Picture.jpg" to "Picture.jpg". "Picture.jpg" now exists, so it'll increment intCounter (which will now be at 2 due to being used the 2nd time round the loop) and I'll end up with "Picture2.jpg". So after the script has done I'll be left with: Photo.jpg Photo1.jpg Picture.jpg Picture2.jpg. What I would really have hoped for is: Photo.jpg Photo1.jpg Picture.jpg Picture1.jpg Can anyone see the problem or a possible solution? Sorry for the lengthy post, I couldn't think of a way of explaining the problem easily.
|
|
| |
|
|
|
 |
RE: Appending a number to a file name problem - 12/19/2006 3:24:58 AM
|
|
 |
|
| |
ebgreen
Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
|
I would solve it via a dictionary. so here is some pseudo-code: Loop through all of the files determine the new file name Does that name already exists? Yes See if there is already an entry in the dictionary for this name No Add the file name to the dictionary with a value of 1 Yes Increment the value of this file name's entry in the dictionary Rename the file using the dictionary entry for this file name to determin the number
_____________________________
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm http://www.visualbasicscript.com/m_47117/tm.htm
|
|
| |
|
|
|
 |
RE: Appending a number to a file name problem - 12/19/2006 4:06:25 AM
|
|
 |
|
| |
ebgreen
Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
|
Try this: Dim d, fso, varFileName, strPath, intDictionaryIndex, strPath2, strNewName Set d = CreateObject("Scripting.Dictionary") Set fso = CreateObject("Scripting.FileSystemObject") strPath = "C:\Documents and Settings\mark.mcrobie\Desktop\Navynet" strPath2 = "C:\Documents and Settings\mark.mcrobie\Desktop\Navynet\Renamed\" intDictionaryIndex = 0 For Each File In fso.GetFolder(strPath).Files varFileName = File.Name If fso.FileExists(strPath2 & varFileName) Then If d.Exists(varFileName) Then d(varFileName) = d(varFileName) + 1 Else d.Add varFileName, 1 End If 'Rename the file here with something like: strNewName = Split(varFileName, ".")(0) & d(varFileName) & "." & Split(varFileName, ".")(1) File.Copy strPath2 & strNewName End If Next
_____________________________
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm http://www.visualbasicscript.com/m_47117/tm.htm
|
|
| |
|
|
|
 |
RE: Appending a number to a file name problem - 12/20/2006 2:34:15 AM
|
|
 |
|
| |
ebgreen
Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
|
Did you implement the code exactly as I have it? Here is what the code I posted should do: INITIAL CONDITIONS: destination directory empty First file to check is called File.jpg. That file name does not exist in the destination directory so copy it. The second file to check is also called file.jpg. That file does exist in the destination directory, so make an entry in the dictionary with the name of the file and a value of 1. Rename the file using the number for that file from the dictionary (in this case 1) so the new name is file1.jpg. The third file to check is also named file.jpg. That file already exists in the destination directory. The file name also already exists in the dictionary, so increment the dictionary value by 1. Rename the file using the value that is in the dictionary for this file name (in this case 2). FINAL CONDITIONS destination directory has file.jpg, file1.jpg, and file2.jpg Are you running the code and seeing different behavior?
_____________________________
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm http://www.visualbasicscript.com/m_47117/tm.htm
|
|
| |
|
|
|
 |
RE: Appending a number to a file name problem - 12/20/2006 3:16:16 AM
|
|
 |
|
| |
ebgreen
Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
|
I should have explained that my script would not handle files already being in the destination directory with names like file2.jpg. Gimme a minute and I'll post a script that will handle that and won't use dictionaries.
_____________________________
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm http://www.visualbasicscript.com/m_47117/tm.htm
|
|
| |
|
|
|
 |
RE: Appending a number to a file name problem - 12/20/2006 3:31:40 AM
|
|
 |
|
| |
ebgreen
Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
|
Here is a sample script for you. It will do what you want I believe with a couple of caveats. It will die on a file that does not have and extension and it will amngle the name of a file that has more than one . in it. Both of these issues can be overcome, but I'll leave that for you if you believe them to be show stoppers. By the way on the subject of subs/functions, this functionality would be a prime example of something that you might want to put into a sub for future use in other scripts. I'll repost in a minute with the code in a sub showing how I would implement it in a reuseable fashion. Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject") Dim oFile Dim strSrcDir Dim strDestDir Dim strNewName Dim nAppend Dim strRootName Dim strExt strDestDir = "C:\temp\Dest" strSrcDir = "C:\temp" For Each oFile In oFSO.GetFolder(strSrcDir).Files strNewName = strDestDir & "\" & oFile.Name nAppend = 0 strRootName = Split(oFile.Name, ".")(0) strExt = Split(oFile.Name, ".")(1) Do While oFSO.FileExists(strNewName) nAppend = nAppend + 1 strNewName = strDestDir & "\" & strRootName & nAppend & "." & strExt Loop oFile.Copy strNewName Next
_____________________________
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm http://www.visualbasicscript.com/m_47117/tm.htm
|
|
| |
|
|
|
 |
RE: Appending a number to a file name problem - 12/20/2006 3:36:35 AM
|
|
 |
|
| |
ebgreen
Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
|
Here it is implemented as a sub. Now you would be able to take this sub and drop it into any script where you want to copy a file and have the copy numbered if the file already exists. Option Explicit Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject") Dim oFile Dim strSrcDir Dim strDestDir strDestDir = "C:\temp\Dest" strSrcDir = "C:\temp" For Each oFile In oFSO.GetFolder(strSrcDir).Files NumberCopy oFile, strDestDir Next Sub NumberCopy(oFile, strDestDir) Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject") Dim strRootName Dim strExt Dim nAppend Dim strNewName strNewName = strDestDir & "\" & oFile.Name nAppend = 0 strRootName = Split(oFile.Name, ".")(0) strExt = Split(oFile.Name, ".")(1) Do While oFSO.FileExists(strNewName) nAppend = nAppend + 1 strNewName = strDestDir & "\" & strRootName & nAppend & "." & strExt Loop oFile.Copy strNewName End Sub
_____________________________
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm http://www.visualbasicscript.com/m_47117/tm.htm
|
|
| |
|
|
|
 |
RE: Appending a number to a file name problem - 12/20/2006 4:07:06 AM
|
|
 |
|
| |
ebgreen
Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
|
That could be addressed by changing the way you determine strRootName and strExt. I'll let you think on it. Let me know if you can't figure it out. At the same time you might as well address file names without an extension. You may not have a need for it now but it would make the sub more robust and reuseable if you have it handle that.
_____________________________
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm http://www.visualbasicscript.com/m_47117/tm.htm
|
|
| |
|
|
|
 |
RE: Appending a number to a file name problem - 1/7/2007 10:37:50 PM
|
|
 |
|
| |
markmcrobie
Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
|
I've had a complete rethink over Xmas about my script. I've stripped out all message boxes, etc, informing the user about how many files were renamed, etc. I've also amended it so that it moves each file as soon it's been renamed, rather than renaming them all then move them all afterwards in a separate For loop. It works much better now: before, it worked fine when testing a few files, but when I done a large test (500 files), I got weird results - all the files would be renamed and moved, but the "Append number if files exists" bit of code went haywire - there were files that had had a number appended, yet no other file with a similar name exists. Since modifying my code it seems to run perfectly - only files with a duplicate name after renaming have a number appended. Which leads me nicely back to this thread - my only remaining problem is the one you're helping me solve here. To recap, I store a number in intCounter that gets incremented every time the script comes across a duplicate file. intCounter then gets appended to the duplicated file name, to avoid FileExists errors. I need to implement your code so that intCounter resets for each different file name, but I'm not sure where to begin implementing it. Below is the final bit of my code, the bit that actually does the renaming and moving: 'Rename and move each file one by one, appending any input from the InputBoxes above. Also appends a number to the end 'in brackets if a file already exists with that name. If strSysSeqRevResult = "" Then File.Name = strNewName & strExtension If Not fso.FileExists(strPath & "Renamed\" & File.Name) Then fso.MoveFile strPath & File.Name, strPath & "Renamed\" & File.Name Else intCounter=intCounter + 1 File.Name = strNewName & " (" & intCounter & ")" & strExtension fso.MoveFile strPath & File.Name, strPath & "Renamed\" & File.Name End If Else File.Name = UCase(strSysSeqRevResult) & " - " & strNewName & strExtension If Not fso.FileExists(strPath & "Renamed\" & File.Name) Then fso.MoveFile strPath & File.Name, strPath & "Renamed\" & File.Name Else intCounter=intCounter + 1 File.Name = UCase(strSysSeqRevResult) & " - " & strNewName & " (" & intCounter & ")" & strExtension fso.MoveFile strPath & File.Name, strPath & "Renamed\" & File.Name End If End If End If
|
|
| |
|
|
|
| |
|
|
 |
|
 |
|
|