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

Rename all files in folder to sequent number from A10001 and up..

Status
Not open for further replies.

slint

Technical User
Jul 2, 2006
48
SE
I'm about to change the file name of a lot of files and i found this script. I have modified it a bit but the prob is that it renames the first file to A10001.vbs the second to A10001_.vbs the third to A10002.vbs, forth A10002_.vbs

If i remove the last part in the script it only ren one file in the dir. What i'm looking for is first file A10001.vbs second A10002.vbs ....



Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='c:\test'} Where " _
        & "ResultClass = CIM_DataFile")
IntNumber = 10001
For Each objFile In FileList
    strFName = ("A"&IntNumber)
    strNewName = objFile.Drive & objFile.Path & _
       strFName & "." & "vbs"
    strNameCheck = Replace(strNewName, "\", "\\")

    i = 1
    Do While True
        Set colFiles = objWMIService.ExecQuery _
            ("Select * from Cim_Datafile Where Name = '" & strNameCheck & "'")
        If colFiles.Count = 0 Then
            errResult = objFile.Rename(strNewName)
            Exit Do
       Else
       
       IntNumber = IntNumber + 1
       
            i = i + 1
            strNewName = objFile.Drive & objFile.Path & _
                strFName & "_" & "." & "vbs"
            strNameCheck = Replace(strNewName, "\", "\\")
        End If
        
    Loop
Next
 

I fixed it... i moved the intnumber + 1 and then it worked..

Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='c:\test'} Where " _
        & "ResultClass = CIM_DataFile")
IntNumber = 10001
For Each objFile In FileList
    strFName = ("A"&IntNumber)
    strNewName = objFile.Drive & objFile.Path & _
       strFName & "." & "vbs"
    strNameCheck = Replace(strNewName, "\", "\\")
    
IntNumber = IntNumber + 1
    
    i = 1
    Do While True
        Set colFiles = objWMIService.ExecQuery _
            ("Select * from Cim_Datafile Where Name = '" & strNameCheck & "'")
        If colFiles.Count = 0 Then
            errResult = objFile.Rename(strNewName)
            Exit Do
       Else
       
            i = i + 1
            strNewName = objFile.Drive & objFile.Path & _
                strFName & "." & "vbs"
            strNameCheck = Replace(strNewName, "\", "\\")
        End If
        
    Loop
Next
 
Some comment is in order.

[1] You use i. Where does it have a impact on the filename when .count is not zero? It seems it is looping forever there (you may not have encountered the case in your testing).

[2] One major issue is to make sure there should be holes within the naming series. There is no guarantee of this. If you find one A10015 say in the colFiles, you try to avoid naming the present file to A10015. Then later, when A10015 is to be process at its turn, it will be renamed---leaving a hole at A10015.

[3] Another major issue of renaming is the stability. If you pass the routine twice, is the second time to have the effect of leaving them intact? Otherwise, it will cause trouble no end and you will never be sure what file is which and why its name keeps change all the time.

One solution off hand is this.

[tt]strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='c:\test'} Where " _
& "ResultClass = CIM_DataFile")

IntNumber = 10001
dim aReserve() : reDim aReserve(-1)
dim bCollide, bReserve
For Each objFile In FileList
bCollide=true
bReserve=false
for i=ubound(aReserve) to 0 step -1
if strcomp(objFile.Name,aReserve(i),1)=0 then
bReserve=true
exit for
end if
next

Do While bCollide and not bReserve
strFName = ("A"&IntNumber)
strNewName = objFile.Drive & objFile.Path & _
strFName & "." & "vbs"
strNameCheck = Replace(strNewName, "\", "\\")

Set colFiles = objWMIService.ExecQuery _
("Select * from Cim_Datafile Where Name = '" & strNameCheck & "'")
If colFiles.Count = 0 Then
errResult = objFile.Rename(strNewName)
bCollide=false
Else
reDim preserve aReserve(ubound(aReserve)+1)
aReserve(ubound(aReserve)=strNewName
End If
IntNumber = IntNumber + 1
Loop
Next
redim aReserve(-1)
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top