Link to home
Start Free TrialLog in
Avatar of Barnardos_2LS
Barnardos_2LS

asked on

Rename Files

Hi,

I would like to rename all files in a folder by removing the first 13 characters of each file name. I have written the following VBScript however it fails - can anyone help?

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where Path = '\\Documents and Settings\Administrator\My Documents\NDS\Test\\'")
Set objFSO = CreateObject("Scripting.FileSystemObject")

For Each objFile in colFiles
      strOldName = objFile.Name
      Msgbox strOldName
      strNewName = Mid(strOldName,13,strOldName.Length)
      objFSO.MoveFile strOldName,strNewName
Next
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

it failes because in the MoveFile, you do not specify the path, only the name:

strComputer = "."
strPath = "\\Documents and Settings\Administrator\My Documents\NDS\Test\"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where Path = '" & strPath & "' ")
Set objFSO = CreateObject("Scripting.FileSystemObject")

For Each objFile in colFiles
     strOldName = objFile.Name
     Msgbox strOldName
     strNewName = Mid(strOldName,13,strOldName.Length)
     objFSO.MoveFile strPath & strOldName, strPath & strNewName
Next
Avatar of Barnardos_2LS
Barnardos_2LS

ASKER

I am now getting a null error on line 7. The folder which contains the files is:

C:\Documents and Settings\Administrator\My Documents\NDS\Test

is this the problem?
putting the entire path might help:

strComputer = "."
strPath = "C:\Documents and Settings\Administrator\My Documents\NDS\Test\"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where Path = '" & strPath & "' ")
Set objFSO = CreateObject("Scripting.FileSystemObject")

For Each objFile in colFiles
     strOldName = objFile.Name
     Msgbox strOldName
     strNewName = Mid(strOldName,13,strOldName.Length)
     objFSO.MoveFile strPath & strOldName, strPath & strNewName
Next
Unfortunately i get the same error...
I see. the drive needs to be in separate condition, and the path needs double backquotes everywhere:

strComputer = "."
strPath = "\\Documents and Settings\\Administrator\\My Documents\\NDS\\Test\\"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where drive='C:' and Path = '" & strPath & "' ")
Set objFSO = CreateObject("Scripting.FileSystemObject")

For Each objFile in colFiles
     strOldName = objFile.Name
     Msgbox strOldName
     strNewName = Mid(strOldName,13,strOldName.Length)
     objFSO.MoveFile strPath & strOldName, strPath & strNewName
Next
I now get an error for line 9 (strNewName = Mid(strOldName,13,strOldName.Length)) which says it cannot find the strOldName variable even though i have declared it in line 7 - any ideas?
please try this:

strComputer = "."
strPath = "\\Documents and Settings\\Administrator\\My Documents\\NDS\\Test\\"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where drive='C:' and Path = '" & strPath & "' ")
Set objFSO = CreateObject("Scripting.FileSystemObject")

For Each objFile in colFiles
     strOldName = objFile.Name
     Msgbox strOldName
     strNewName = Mid(strOldName,13)
     objFSO.MoveFile strPath & strOldName, strPath & strNewName
Next


strOldName.Length does not exist in vbscript, only in vb.net
How do i get the length of the string for the Mid function then?
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial