Solved

Rename Files

Posted on 2006-11-18
9
250 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 1

Author Comment

by:Barnardos_2LS
ID: 17971087
Unfortunately i get the same error...
0
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 - Convert HH:MM into Decimal 8 54
MsgBox 4 59
VB 6 error 5 in windows 10 but not in XP 7 55
Clear Filter 8 32
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

786 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