Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename files in folder 1

Status
Not open for further replies.

patriciaxxx

Programmer
Jan 30, 2012
277
GB
I have the following vbscript which works as follows.
Giving that the specified directory contains 72 pics.
It renames them sequencially 01 to 72.
Must begin 01 and not 00

I need to modify it to include the following an optional mixed character prefix. The problem is when I add one that begins with a letter lets say I use “p12ax_” it adds it but the numbering begins 00 so I get p12ax_00 to p12ax_71 but I need p12ax_01 to p12ax_72.

Option Explicit

Dim strPath
Dim FSO
Dim FLD
Dim fil
Dim strOldName
Dim strNewName
Dim strLeadingZero
Dim intFileParts
Dim strFileParts

'Define file path.
strPath = "C:\Documents and Settings\Rename Pictures for Web Use\pic\"

'Create the instance of the FSO.
Set FSO = CreateObject("Scripting.FileSystemObject")
'Set the folder you want to search.
Set FLD = FSO.GetFolder(strPath)

'Loop through each file in the folder.
For Each fil in FLD.Files
'Get complete file name with path.
strOldName = fil.Path
'Build the new file name.
strLeadingZero = "00" 'Maximum files in folder 100.
intFileParts = intFileParts + 1
strFileParts = strLeadingZero & CStr(intFileParts)
strFileParts = Right(strFileParts, Len(strLeadingZero))
strNewName = strPath & strFileParts & Right(strOldName, 4) 'Right function keep file extension.
'Use the MoveFile method to rename the file.
FSO.MoveFile strOldName, strNewName
Next

'Cleanup the objects.
Set FLD = Nothing
Set FSO = Nothing
 
If the old code worked, then all you should need is this
Code:
...
Dim intFileParts
Dim strFileParts
[COLOR=blue][b]Dim strPrefix
strPrefix = "p12ax_"[/b][/color]

....

strNewName = strPath & [COLOR=blue][b]sPrefix & [/b][/color]strFileParts & Right(strOldName, 4)
 
I thought so too, and I tried it, but like I said it adds the string but the numbers begin 00 and not 01.

Without the additional string it works properly, the numbers begin 01.

The strange thing is that if the additional string begins with a number instead of a letter then it works properly and the numbers begin 01.

But I need the flexibility of the additional string beginning with number or letter and the numbers must always begin 01.

The additional string is obviously affecting it but I cannot work out why and therefore don’t know how to correct the problem.

 
This is not logical.

Post your code with the prefix addition. I'm sure you mixed up the order of string manipulation at some point.

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Here is the code with the pefix. I agree its not logical. Never the less the following is true.
If the prefix begins with a letter then the number begins 00 which is no good it must always begin 01
If the prefix begins with a number for example 3k13ab_ then the number begins 01
And of course if there is no prefix the number begins 01
Is there maybe a better way to code this.

Option Explicit

Dim strPath
Dim FSO
Dim FLD
Dim fil
Dim strOldName
Dim strNewName
Dim strLeadingZero
Dim intFileParts
Dim strFileParts
Dim strPrefix 'Optional.

'Define file path.
strPath = "C:\Documents and Settings\Rename Pictures for Web Use\pic\"

'Create the instance of the FSO.
Set FSO = CreateObject("Scripting.FileSystemObject")
'Set the folder you want to search.
Set FLD = FSO.GetFolder(strPath)

'Loop through each file in the folder.
For Each fil in FLD.Files
'Get complete file name with path.
strOldName = fil.Path
'Build the new file name.
strPrefix = "k13ab_"
strLeadingZero = "00" 'Maximum files in folder 100.
intFileParts = intFileParts + 1
strFileParts = strLeadingZero & CStr(intFileParts)
strFileParts = Right(strFileParts, Len(strLeadingZero))
strNewName = strPath & strPrefix & strFileParts & Right(strOldName, 4) 'Right function keep file extension.
'Use the MoveFile method to rename the file.
FSO.MoveFile strOldName, strNewName
Next

'Cleanup the objects.
Set FLD = Nothing
Set FSO = Nothing
 
It should not depend on the prefix at all as you are doing nothing with it save concatenating it!
[3eyes]
Perhaps the problem lies here:
Code:
intFileParts = intFileParts + 1

In contrast to full VB, you do not dim AS a type in VBScript, hence the type is determined when the variable is initialised. However, you are not initialising intFileParts but directly incrementing it.
I haven't come across such weird behaviour before but try by simply adding this before your loop:
Code:
intFileParts = 0
This way you are initialising the variable as a proper Integer variable. Perhaps there's the rub.
[ponder]

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
I tried the setting and discovered the following.
The setting seems to have no effect.
I discovered something I had not noticed before which is that the code works and numbers begin 01 with or without the setting but only if there are around 21 files if there are more files we are back to the 00 again.
I was originally testing a folder with 71 files and this still numbers 00 with or without your setting.
I am just not good enough with vbscript to understand why this happens or how to solve it.
 
Umm...lol...
Do you happen to have a... ummm... test version of your script with somewhat fixed numbers or so?
[tongue]

Anyway, try this: replace
Code:
 strLeadingZero = "00" 'Maximum files in folder 100.
        intFileParts = intFileParts + 1
        strFileParts = strLeadingZero & CStr(intFileParts)
        strFileParts = Right(strFileParts, Len(strLeadingZero))
with this:
Code:
intFileParts = intFileParts + 1
strFileParts = Format(intFileParts, "00")

And make sure the code is not in some number-limited loop.

Cheers,
MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
MakeItSo, there is NO Format function in VBS.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Ah damnit!
Sorry.

Thanks PHV.

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
patriciaxxx,

Not sure what to tell you. As MakeItSo put it, "not logical". Your code works fine. I am seeing no reason why the code snippet you provided would behave any different from what you want.

For example, try the code below. The MsgBox should read "strFileParts = 01" regardless of how many files are in the folder

Code:
Option Explicit

Dim strPath
Dim FSO
Dim FLD
Dim fil
Dim strOldName
Dim strNewName
Dim strLeadingZero
Dim intFileParts
Dim strFileParts
Dim strPrefix 'Optional.

    'Define file path.
    strPath = "C:\Documents and Settings\Rename Pictures for Web Use\pic\"

    'Create the instance of the FSO.
    Set FSO = CreateObject("Scripting.FileSystemObject")
    'Set the folder you want to search.
    Set FLD = FSO.GetFolder(strPath)

    'Loop through each file in the folder.
    For Each fil in FLD.Files
        'Get complete file name with path.
        strOldName = fil.Path
        'Build the new file name.
        strPrefix = "k13ab_"
        strLeadingZero = "00" 'Maximum files in folder 100.
        intFileParts = intFileParts + 1
        strFileParts = strLeadingZero & CStr(intFileParts)
        strFileParts = Right(strFileParts, Len(strLeadingZero))
        strNewName = strPath & strPrefix & strFileParts & Right(strOldName, 4) 'Right function keep file extension.
        'Use the MoveFile method to rename the file.
        [COLOR=green]'DONT MOVE THE FILE YET
        'FSO.MoveFile strOldName, strNewName[/color]
        [COLOR=blue][b]MsgBox "strFileParts = " & strFileParts

        Exit For[/b][/color]
    Next

    'Cleanup the objects.
    Set FLD = Nothing
    Set FSO = Nothing
 
Yes I agree its not logical never the less if you create a folder with 70 jpegs and run the script as follows you will see what I see.

Set
strLeadingZero = "000"
strPrefix = ""
It number them correct 001 to 070

I notice if you number them correctly as above then change zeros to less amount it all goes wrong again either beginning 0 or getting file exists error. However if you increase zeros it works.

With these settings
strLeadingZero = "000"
strPrefix = "L_"
It number them wrong 000 to 069

When you add the prefix as above I notice that the files count down through all the numbers from the amount of zeros until it reaches 000. so in the above example when I refresh the folder I see the files counting down from 999 until they reach 000 which is no good becouse with lots of leading zeros the count down takes forever. Also it must begin 001 not 000.

However if you move the prefix to after the fileparts it does not count down
strFileParts & strPrefix.

This needs an expert VBScriptor to run tests to unravel these issues and come up with a script that works in all situations.
 
I don't think it is a good idea to move files into the same folder in which you are looping through the files. To see why, add a counter to report how many files get processed; the results may surprise you.

As an alternative, you may want to create a subdirectory and move your renamed files there. When the script is done renaming, it can move the files back to the main folder and delete the subdirectory you created.
 
Ahhhhh I think jges may have nailed it... I had not noticed that behavior before, but yes in some cases that routine seems to "move" files that were already moved... This would screw up the numbering.

As a simpler implementation of jges' idea, why not try just changing the name attribute of the file:
Code:
   strNewName = strPrefix & strFileParts & Right(strOldName, 4) 'Right function keep file extension.
   'Rename the file
   fil.Name = strNewName
 
On second thought, no... same issue. I'm with jges... move to subfolder with the new names, and then move back when done.
 
An alternative, possibly simpler approach to the subfolder idea would be to add the file names to an array in the 'for each' loop, then process the files outside of the loop using the information in the array.
 
Yes the array sounds like it maybe the solution.
Can you modify my code below to show exactly how you would achieve this for it to work.

Option Explicit

Dim strPath
Dim FSO
Dim FLD
Dim fil
Dim strOldName
Dim strNewName
Dim strLeadingZero
Dim intFileParts
Dim strFileParts
Dim strPrefix 'Optional.

'Define file path.
strPath = "C:\Documents and Settings\Rename Pictures for Web Use\pic\"

'Create the instance of the FSO.
Set FSO = CreateObject("Scripting.FileSystemObject")
'Set the folder you want to search.
Set FLD = FSO.GetFolder(strPath)

'Loop through each file in the folder.
For Each fil in FLD.Files
'Get complete file name with path.
strOldName = fil.Path
'Build the new file name.
strPrefix = "k13ab_"
strLeadingZero = "00" 'Maximum files in folder 100.
intFileParts = intFileParts + 1
strFileParts = strLeadingZero & CStr(intFileParts)
strFileParts = Right(strFileParts, Len(strLeadingZero))
strNewName = strPath & strPrefix & strFileParts & Right(strOldName, 4) 'Right function keep file extension.
'Use the MoveFile method to rename the file.
FSO.MoveFile strOldName, strNewName
Next

'Cleanup the objects.
Set FLD = Nothing
Set FSO = Nothing
 
Can you modify my code below to show exactly
Can't you even try by your own ?
What have YOU tried so far and where in YOUR code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This is my code so far PHV

Option Explicit

Dim strPath
Dim FSO
Dim FLD
Dim fil
Dim strOldName
Dim strNewName
Dim strLeadingZero
Dim intFileParts
Dim strFileParts
Dim strPrefix 'Optional.

'Define file path.
strPath = "C:\Documents and Settings\Rename Pictures for Web Use\pic\"

'Create the instance of the FSO.
Set FSO = CreateObject("Scripting.FileSystemObject")
'Set the folder you want to search.
Set FLD = FSO.GetFolder(strPath)

'Loop through each file in the folder.
For Each fil in FLD.Files
'Get complete file name with path.
strOldName = fil.Path
'Build the new file name.
strPrefix = "k13ab_"
strLeadingZero = "00" 'Maximum files in folder 100.
intFileParts = intFileParts + 1
strFileParts = strLeadingZero & CStr(intFileParts)
strFileParts = Right(strFileParts, Len(strLeadingZero))
strNewName = strPath & strPrefix & strFileParts & Right(strOldName, 4) 'Right function keep file extension.
'Use the MoveFile method to rename the file.
FSO.MoveFile strOldName, strNewName
Next

'Cleanup the objects.
Set FLD = Nothing
Set FSO = Nothing

jges suggests an array would solve the problems I was asking to be shown how becouse I don't know how to do it. This is not my area and my VBS skills are at learning level. If you know what he was suggesting or how to solve the problems it will be helpful if you could post the modifycation to my existing code for me to test and learn.
 
Here is a version that uses a dictionary object.

Code:
Option Explicit

Dim strPath
Dim FSO
Dim FLD
Dim fil
Dim strNewName
Dim strLeadingZero
Dim intFileParts
Dim strFileParts
Dim strPrefix
strPrefix = "p12ax_"
Dim fileExt
Dim fileDic
Set fileDic = CreateObject("Scripting.Dictionary")
Dim key

    'Define file path.
    strPath = "C:\Documents and Settings\Rename Pictures for Web Use\pic\"

    'Create the instance of the FSO.
    Set FSO = CreateObject("Scripting.FileSystemObject")
    'Set the folder you want to search.
    Set FLD = FSO.GetFolder(strPath)

    'Loop through each file in the folder.
    For Each fil in FLD.Files
        'Get complete file name with path.
		fileExt = FSO.GetExtensionName(fil)
        'Build the new file name.
        strLeadingZero = "00" 'Maximum files in folder 100.
        intFileParts = intFileParts + 1
        strFileParts = strLeadingZero & CStr(intFileParts)
        strFileParts = Right(strFileParts, Len(strLeadingZero))
        strNewName = strPrefix & strFileParts & "." & fileExt
		'add new name and file object to the dictionary object
		fileDic.Add strNewName, fil
    Next
	
	'rename each file in the dictionary: key = new name, item = file object
	For Each key in fileDic
		Set fil = fileDic.Item(key)
		fil.Name = key
	Next
	
    'Cleanup the objects.
    Set FLD = Nothing
    Set FSO = Nothing
	Set fileDic = Nothing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top