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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with script to rename all subfolders with a sequence number.

Status
Not open for further replies.

mikewolf1

Technical User
Jul 17, 2013
4
GB
Hi Guys,

I am trying to write script that will rename all folders in a subfolder. Currently the folders all have a time stamp name. 2013-10-07-14-22 for example. I am hoping to find a way to rename all these folders with a code given by the user and then a sequence number starting at 001.
For example - 130012-001

I know how to batch rename folders by replacing a known string with user input but it's replacing an unknown string with a value from user input plus a sequence number that is giving me trouble.

Using the script below I have been able to do this with all files within a folder -

strJobname = InputBox ("PLEASE ENTER THE JOB CODE")
Const adVarChar = 200
Const MaxCharacters = 255

Set DataList = CreateObject("ADOR.Recordset")
DataList.Fields.Append "FileName", adVarChar, MaxCharacters
DataList.Fields.Append "FileDate", adVarChar, MaxCharacters
DataList.Open

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\STUDIO 16\Desktop\BACKUP\WEB JPEGS\")

Set colFiles = objFolder.Files

For Each objFile in colFiles
DataList.AddNew
DataList("FileName") = objFile.Path
'DataList("FileDate") = objFile.DateCreated
DataList.Update
Next

DataList.Sort = "FileName"

DataList.MoveFirst
i = 1

Do Until DataList.EOF
If i < 10 Then
x = CStr("00" & i)
ElseIf i < 100 Then
x = CStr("0" & i)
ElseIf i < 1000 Then
x = CStr("" & i)
Else
x = i
End If

strNewName = "C:\Users\STUDIO 16\Desktop\BACKUP\WEB JPEGS\" & ".jpg"
objFSO.MoveFile DataList.Fields.Item("FileName"), strNewName

i = i + 1
DataList.MoveNext
Loop

But I'm afraid that with my limited knowledge I have completely confused myself with trying to convert this to work on folder names.

This is where I have got so far -

strJobname = InputBox ("PLEASE ENTER THE JOB CODE")
Const adVarChar = 200
Const MaxCharacters = 255

Set DataList = CreateObject("ADOR.Recordset")
DataList.Fields.Append "FolderName", adVarChar, MaxCharacters
DataList.Fields.Append "FileDate", adVarChar, MaxCharacters
DataList.Open

Set objFSO = CreateObject("Scripting.FileSystemObject")


Set objFol = objfso.getfolder("C:\Users\STUDIO 16\Desktop\BACKUP\ORIGINAL FOLDERS\")
set colSubfolders = objfol.subfolders
for each objsubfolder in colsubfolders

DataList.AddNew
DataList("FolderName") = objSubfolder.Name
'DataList("FileDate") = objFile.DateCreated
DataList.Update
Next

DataList.Sort = "FolderName"

DataList.MoveFirst
i = 1

Do Until DataList.EOF
If i < 10 Then
x = CStr("00" & i)
ElseIf i < 100 Then
x = CStr("0" & i)
ElseIf i < 1000 Then
x = CStr("" & i)
Else
x = i
End If

Newfoldername = "C:\Users\STUDIO 16\Desktop\BACKUP\ORIGINAL FOLDERS" & strJobname & "-" & x
objFSO.MoveFolder DataList.Fields.Item("FolderName"), Newfoldername
i = i + 1
DataList.MoveNext
Loop

I realise that there is probably no way that this was going to work as it is but it really has me stumped.
I know there are lots of batch renaming programs available but this is all part of a much bigger script that does other stuff so I would like to keep it all in one place. Plus, I don't like being stumped!

Any thoughts or help greatly appreciated!

Thanks.
 
what about (i think i understand what you are doing)

Code:
set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("Wscript.Shell")

intSequence = 0
strDir = "C:\temp"

strJobCode = inputBox("Enter a job code:")

for each objFolder in objFSO.GetFolder(strDir)
	intSequence = intSequence + 1
	objFolder.Name = strJobCode & " - " & right("000" & intSequence, 3) 
next

-Geates

 
Hi Geates,

Thanks for responding!

When trying this I'm getting the error "Object doesn't support this property or method" on line 9.

Do I need to Set objFolder?

Thanks.
 
What is the value of strDir when the error raises ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I've got-

strDir = "C:\Users\STUDIO 16\Desktop\BACKUP\ORIGINAL FOLDERS\"

Thanks.
 
Geates you are a star! Simplicity is best...

Thanks so much for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top