Solved

Rename Files

Posted on 2006-11-18
9
248 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 142

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 142

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
 
LVL 1

Author Comment

by:Barnardos_2LS
ID: 17971087
Unfortunately i get the same error...
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 142

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 142

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 142

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now