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
 
Wow.
Thank you very much for your time jges.
I have studied your changes and tested in all kinds of scenarios and it works perfectly.
I would say problems have been solved. It’s been great learning for me too. I never knew of the Dictionary Object much less how to implement it.

I have updated my script somewhat believing my new script to be more robust and better scripted and of course with your suggestions it is now free of those illogical annoyances.

I do have one question.
Below the line:-
Set objDict = Nothing
Do I need to add:-
'Erase the contents of the dictionary object
objDict.RemoveAll
I don’t think I do because that’s what “Set objDict = Nothing “ is doing.

Here is my new script. I would be interested in any comments you may have.

Option Explicit

Const NUMBER_STEP = 1 'File numbering will increment by this amount.
Const NUMBER_DIGITS = 2 'Numbers will have this many digits (with leading zeros).
Const NUMBER_STRING = "F_" 'The characters that follows the leading number.

Dim objFSO, objFolder, objFiles, objDict, objKey, strPath, strFileExt, _
strNewName, lngNumber

'Define file path.
strPath = "C:\pics\"
lngNumber = 0 'Initialise the variable.

'Create the instance of the objFSO.
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set the folder you want to search.
Set objFolder = objFSO.GetFolder(strPath)
'Create the instance of the objDict.
Set objDict = CreateObject("Scripting.Dictionary")

'Loop through each file in the folder.
For Each objFiles in objFolder.Files
'Get the file extension.
strFileExt = objFSO.GetExtensionName(objFiles)
'Build the new file name.
lngNumber = lngNumber + 1
strNewName = NUMBER_STRING & Right(String(NUMBER_DIGITS, "0") & _
Cstr(lngNumber), NUMBER_DIGITS) & "." & strFileExt
'Add new name and file object to the dictionary object.
objDict.Add strNewName, objFiles
Next

'Rename each file in the dictionary.
For Each objKey in objDict
Set objFiles = objDict.Item(objKey)
objFiles.Name = objKey
Next

'Cleanup the objects.
Set objFolder = Nothing
Set objFSO = Nothing
Set objDict = Nothing
 
patriciaxxx said:
I do have one question.
Below the line:-
Set objDict = Nothing
Do I need to add:-
'Erase the contents of the dictionary object
objDict.RemoveAll
I don't think I do because that's what "Set objDict = Nothing " is doing.

If you want to clear out the dictionary, do it before "Set objDict = Nothing", otherwise you will likely get an error. I think "Set objDict = Nothing" is sufficient to release the memory resources, and even that may not be necessary. It is my understanding that for the VB languages (VB, VBA, VBScript), Windows keeps a reference count for each object. When the count hits zero, the memory is freed. Once the script finishes executing, all the resources it used should be freed whether you "Set objDict = Nothing" or not. I hope someone on the forum with more insight into such matters will correct me if I am wrong.

Also, I'd like to suggest one addition. Before the file is renamed, you should check to see if a file with that name already exists. I left it out of my example code for brevity and also because I don't know what you want to happen if such a file already exists.
 
jges: Yes, that is my understanding also. Once the object goes out of scope (the script ends, or the function/sub ends), the object is cleared anyway, so setting it to Nothing is redundant.

The only time it may be important is if you have two objects that interact with each other, and have to be cleared in a certain order, but that is very rare. This article explains it somewhat.

 
So if I understand correctly Set objDict = Nothing not only releases the memory resources but also clears out the dictionary.

As for the if files exists I would like to include it but it just gets more complicated. Ideally if a file name already exists I wolud like the script to ignore that one and keep going with the rest.

Thank you for your continued help your input has helped me understtand vbs a little better.
 
woops my reply was as guitarzan was replying.
So my understanding now is do not need any of the following.
Set objFolder = Nothing
Set objFSO = Nothing
objDict.RemoveAll
Set objDict = Nothing
 
patriciaxxx said:
Ideally if a file name already exists I would like the script to ignore that one and keep going with the rest.
Then you will need to add the check; as it stands now, the code will either:[ol][li]halt execution with an error condition[/li] [li]overwrite the existing file[/li][/ol] (I'm not 100% sure which will happen).

Set objDict = Nothing not only releases the memory resources but also clears out the dictionary.
It's a technicality, but I wouldn't say it "clears out the dictionary". Those memory addresses simply get marked by Windows as unused. Windows can then assign those addresses to the next process that requests some memory. The next process will initialize and use that memory as it sees fit, it doesn't care if the memory is 'zeroed' out or if it has leftovers from your script.
 
Thank you for all your help and comments.
Its been a good learning curve for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top