Solved

Rename Files

Posted on 2006-11-18
9
251 Views
Last Modified: 2010-04-30
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
0
Comment
Question by:Barnardos_2LS
  • 5
  • 4
9 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17971041
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
0
 
LVL 1

Author Comment

by:Barnardos_2LS
ID: 17971065
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?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17971080
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
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:Barnardos_2LS
ID: 17971087
Unfortunately i get the same error...
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17971132
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
0
 
LVL 1

Author Comment

by:Barnardos_2LS
ID: 17971546
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?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17971593
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
0
 
LVL 1

Author Comment

by:Barnardos_2LS
ID: 17971632
How do i get the length of the string for the Mid function then?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 17971650
you don't need that. if you leave the length argument out, it will take the rest, whatever length remains.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question