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=imper sonate}!\\ " & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("S elect * from CIM_DataFile where Path = '\\Documents and Settings\Administrator\My Documents\NDS\Test\\'")
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
For Each objFile in colFiles
strOldName = objFile.Name
Msgbox strOldName
strNewName = Mid(strOldName,13,strOldNa me.Length)
objFSO.MoveFile strOldName,strNewName
Next
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=imper
Set colFiles = objWMIService.ExecQuery("S
Set objFSO = CreateObject("Scripting.Fi
For Each objFile in colFiles
strOldName = objFile.Name
Msgbox strOldName
strNewName = Mid(strOldName,13,strOldNa
objFSO.MoveFile strOldName,strNewName
Next
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?
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=imper sonate}!\\ " & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("S elect * from CIM_DataFile where Path = '" & strPath & "' ")
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
For Each objFile in colFiles
strOldName = objFile.Name
Msgbox strOldName
strNewName = Mid(strOldName,13,strOldNa me.Length)
objFSO.MoveFile strPath & strOldName, strPath & strNewName
Next
strComputer = "."
strPath = "C:\Documents and Settings\Administrator\My Documents\NDS\Test\"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=imper
Set colFiles = objWMIService.ExecQuery("S
Set objFSO = CreateObject("Scripting.Fi
For Each objFile in colFiles
strOldName = objFile.Name
Msgbox strOldName
strNewName = Mid(strOldName,13,strOldNa
objFSO.MoveFile strPath & strOldName, strPath & strNewName
Next
ASKER
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\\M y Documents\\NDS\\Test\\"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=imper sonate}!\\ " & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("S elect * from CIM_DataFile where drive='C:' and Path = '" & strPath & "' ")
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
For Each objFile in colFiles
strOldName = objFile.Name
Msgbox strOldName
strNewName = Mid(strOldName,13,strOldNa me.Length)
objFSO.MoveFile strPath & strOldName, strPath & strNewName
Next
strComputer = "."
strPath = "\\Documents and Settings\\Administrator\\M
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=imper
Set colFiles = objWMIService.ExecQuery("S
Set objFSO = CreateObject("Scripting.Fi
For Each objFile in colFiles
strOldName = objFile.Name
Msgbox strOldName
strNewName = Mid(strOldName,13,strOldNa
objFSO.MoveFile strPath & strOldName, strPath & strNewName
Next
ASKER
I now get an error for line 9 (strNewName = Mid(strOldName,13,strOldNa me.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\\M y Documents\\NDS\\Test\\"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=imper sonate}!\\ " & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("S elect * from CIM_DataFile where drive='C:' and Path = '" & strPath & "' ")
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
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
strComputer = "."
strPath = "\\Documents and Settings\\Administrator\\M
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=imper
Set colFiles = objWMIService.ExecQuery("S
Set objFSO = CreateObject("Scripting.Fi
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
ASKER
How do i get the length of the string for the Mid function then?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
strComputer = "."
strPath = "\\Documents and Settings\Administrator\My Documents\NDS\Test\"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=imper
Set colFiles = objWMIService.ExecQuery("S
Set objFSO = CreateObject("Scripting.Fi
For Each objFile in colFiles
strOldName = objFile.Name
Msgbox strOldName
strNewName = Mid(strOldName,13,strOldNa
objFSO.MoveFile strPath & strOldName, strPath & strNewName
Next