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!

Increment filename number by 60

Status
Not open for further replies.

beenew

Technical User
Aug 2, 2012
2
US

itkamaraj helped me with a related script, but this script doesnt work. It gives me a error:

The file names are:

Wind Attn Coeff Tunnel - 474.wrl
Wind Attn Coeff Tunnel - 475.wrl
Wind Attn Coeff Tunnel - 476.wrl, etc.

I expect the script below to increment the above as:

Wind Attn Coeff Tunnel - 534.wrl
Wind Attn Coeff Tunnel - 535.wrl
Wind Attn Coeff Tunnel - 536.wrl, etc.

But it doesnt work. Please help. The error that I get is: rename.vbs(10, 2) Microsoft VBScript runtime error: Invalid procedure call or argument: 'Mid'


filename: rename.vbs
--------------------------------------------

Dim WshShell, oExec
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "R:\temp\RUN2-VRML-DAT\Train\temp\"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
startpos=instr(objFile.Name,"-")
endpos=instr(objFile.Name,".dat")
Number=Mid(objFile.Name,startpos+3,(endpos-(startpos+3)))
Number=Number+60
Fpart=Mid(objFile.Name,1,startpos)
Spart=Mid(objFile.Name,(Len(objFile.Name)-3),4)
newFileName=Fpart & " " & Number & Spart
wscript.echo "OLD FILE : " & objFile.Name & " NEW FILE : " & newFileName
FileName = objStartFolder & "\\" & objFile.Name
newFileName = objStartFolder & "\\" & newFileName
'objFSO.moveFile FileName, newFileName
Next

-----end of script--------
 
Why are you searching for ".dat" if your files end in ".wrl"?
 
Guitarzan, I had used the script previously on .dat files, and I mistakenly left it there. It should be .wrl, you are right. It doesn't work.

Can you find other bugs that is preventing the script from working?


The error that I get is: rename.vbs(10, 2) Microsoft VBScript runtime error: Invalid procedure call or argument: 'Mid'


filename: rename.vbs
--------------------------------------------

Dim WshShell, oExec
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "R:\temp\RUN2-VRML-DAT\Train\temp\"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
startpos=instr(objFile.Name,"-")
endpos=instr(objFile.Name,".wrl")
Number=Mid(objFile.Name,startpos+3,(endpos-(startpos+3)))
Number=Number+60
Fpart=Mid(objFile.Name,1,startpos)
Spart=Mid(objFile.Name,(Len(objFile.Name)-3),4)
newFileName=Fpart & " " & Number & Spart
wscript.echo "OLD FILE : " & objFile.Name & " NEW FILE : " & newFileName
FileName = objStartFolder & "\\" & objFile.Name
newFileName = objStartFolder & "\\" & newFileName
'objFSO.moveFile FileName, newFileName
Next

-----end of script--------
 
Maybe one or more of the files are not in the format you are expecting. This addition might help.
Code:
Dim WshShell, oExec
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "R:\temp\RUN2-VRML-DAT\Train\temp\"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
   startpos=instr(objFile.Name,"-")
   endpos=instr(objFile.Name,".wrl")
   [COLOR=blue]If (startpos > 0) and (endpos > 0) Then[/color]
      Number=Mid(objFile.Name,startpos+3,(endpos-(startpos+3)))
      Number=Number+60
      Fpart=Mid(objFile.Name,1,startpos)
      Spart=Mid(objFile.Name,(Len(objFile.Name)-3),4)
      newFileName=Fpart & " " & Number & Spart
   [COLOR=blue]Else
      wscript.echo "Skipped " & objFile.Name & " (not in expected format)"
   End If[/color]
   wscript.echo "OLD FILE : " & objFile.Name & " NEW FILE : " & newFileName
   FileName = objStartFolder & "\\" & objFile.Name
   newFileName = objStartFolder & "\\" & newFileName
   'objFSO.moveFile FileName, newFileName
Next

Otherwise, you will have to state your exact specifications. Do the files ALWAYS contain EXACTLY one hyphen, followed by a space, followed by a 3-digit number, followed by the extension? Or are there variations?
 
Oops, put the else / end if in the wrong spot:
Code:
Dim WshShell, oExec
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "R:\temp\RUN2-VRML-DAT\Train\temp\"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
   startpos=instr(objFile.Name,"-")
   endpos=instr(objFile.Name,".wrl")
   [COLOR=blue]If (startpos > 0) and (endpos > 0) Then[/color]
      Number=Mid(objFile.Name,startpos+3,(endpos-(startpos+3)))
      Number=Number+60
      Fpart=Mid(objFile.Name,1,startpos)
      Spart=Mid(objFile.Name,(Len(objFile.Name)-3),4)
      newFileName=Fpart & " " & Number & Spart
      wscript.echo "OLD FILE : " & objFile.Name & " NEW FILE : " & newFileName
      FileName = objStartFolder & "\\" & objFile.Name
      newFileName = objStartFolder & "\\" & newFileName
      'objFSO.moveFile FileName, newFileName
   [COLOR=blue]Else
      wscript.echo "Skipped " & objFile.Name & " (not in expected format)"
   End If[/color]
Next
 
While guitarzan is correct, it doesn't account for Spart. How can one get 4 characters if there are only 3?

Code:
Spart=Mid(objFile.Name,[red](Len(objFile.Name)-3),4[/red])

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Why not use some of the additional features of the FileSystemObject for pulling apart and rebuilding (legitimate) paths. So something like:

Code:
[blue]    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objStartFolder = "R:\temp\RUN2-VRML-DAT\Train\temp\"
    Set objFolder = objFSO.GetFolder(objStartFolder)
    For Each objFile In objFolder.Files
        If objFSO.GetExtensionName(objFile) = "wrl" Then
            temp = Split(objFSO.GetBaseName(objFile))
            temp(UBound(temp)) = temp(UBound(temp)) + 60
            newFileName = objFSO.BuildPath(objStartFolder, Join(temp) & ".wrl")
            wscript.echo objFile, newFileName
            'objFSO.moveFile objFile, newFileName
        End If
    Next[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top