Solved

Rename Files

Posted on 2006-11-18
9
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

756 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