Solved

VBS Rename Script not working correctly.

Posted on 2010-09-06
8
452 Views
Last Modified: 2012-05-10

Hey Rob files only.....
Set fso = CreateObject("Scripting.FileSystemObject") 
Set re  = New RegExp 
 
re.Pattern = "[&]" ' put all characters that you want to strip inside the brackets' 
re.IgnoreCase = True 
re.Global = True 
 
If WScript.Arguments.Unnamed.Count = 1 Then 
  If fso.FolderExists(WScript.Arguments.Unnamed(0)) Then 
    Recurse fso.GetFolder(WScript.Arguments.Unnamed(0)) 
  Else 
    WScript.Echo "Folder not found." 
  End If 
Else 
  WScript.Echo "Please give folder name as argument 1." 
End If 
 
 
Sub Recurse(f) 
  For Each sf In f.SubFolders 
    Recurse sf 
     WScript.Echo sf.Name, " -> ", re.Replace(sf.Name, "and") 
    If fso.FolderExists(Replace(sf.Name, re.Pattern, "and")) = False Then
       sf.Name = re.Replace(sf.Name, "and") 
    Else
       MsgBox "Cannot rename " & sf.Name & " to " & Replace(sf.Name, re.Pattern, "and") & ". Folder already exists."
    End If
  Next 
  For Each sf In f.Files 
    If Right(LCase(sf.Name), 4) = LCase(".doc") Then
	     WScript.Echo sf.Name, " -> ", re.Replace(sf.Name, "and") 
	    If fso.FileExists(Replace(sf.Name, re.Pattern, "and")) = False Then
	       sf.Name = re.Replace(sf.Name, "and") 
	    Else
	       MsgBox "Cannot rename " & sf.Name & " to " & Replace(sf.Name, re.Pattern, "and") & ". File already exists."
	    End If
    End If
  Next 
End Sub

Open in new window

0
Comment
Question by:JohnLo
[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
  • 3
8 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 33614526
Try this to have it rename files only, and not folders, but still iterate through all subfolders.

Regards,

Rob.
Set fso = CreateObject("Scripting.FileSystemObject") 
Set re  = New RegExp 
 
re.Pattern = "[&]" ' put all characters that you want to strip inside the brackets' 
re.IgnoreCase = True 
re.Global = True 
 
If WScript.Arguments.Unnamed.Count = 1 Then 
  If fso.FolderExists(WScript.Arguments.Unnamed(0)) Then 
    Recurse fso.GetFolder(WScript.Arguments.Unnamed(0)) 
  Else 
    WScript.Echo "Folder not found." 
  End If 
Else 
  WScript.Echo "Please give folder name as argument 1." 
End If 
 
 
Sub Recurse(f) 
  For Each sf In f.Files 
    If Right(LCase(sf.Name), 4) = LCase(".doc") Then
	     WScript.Echo sf.Name, " -> ", re.Replace(sf.Name, "and") 
	    If fso.FileExists(Replace(sf.Name, re.Pattern, "and")) = False Then
	       sf.Name = re.Replace(sf.Name, "and") 
	    Else
	       MsgBox "Cannot rename " & sf.Name & " to " & Replace(sf.Name, re.Pattern, "and") & ". File already exists."
	    End If
    End If
  Next 
  For Each sf In f.SubFolders
    Recurse sf
  Next
End Sub

Open in new window

0
 
LVL 4

Author Comment

by:JohnLo
ID: 33614552
this is so weird, Back to.....
(24, 9) Microsoft VBScript runtime error: File already exists
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 33614560
OK, if this next one doesn't work....I'll just add error checking to the post after that....
Set fso = CreateObject("Scripting.FileSystemObject") 
Set re  = New RegExp 
 
re.Pattern = "[&]" ' put all characters that you want to strip inside the brackets' 
re.IgnoreCase = True 
re.Global = True 
 
If WScript.Arguments.Unnamed.Count = 1 Then 
  If fso.FolderExists(WScript.Arguments.Unnamed(0)) Then 
    Recurse fso.GetFolder(WScript.Arguments.Unnamed(0)) 
  Else 
    WScript.Echo "Folder not found." 
  End If 
Else 
  WScript.Echo "Please give folder name as argument 1." 
End If 
 
 
Sub Recurse(f) 
  For Each sf In f.Files 
    If Right(LCase(sf.Name), 4) = LCase(".doc") Then
	     WScript.Echo sf.Name, " -> ", Replace(sf.Name, re.Pattern, "and")
	    If fso.FileExists(Replace(sf.Name, re.Pattern, "and")) = False Then
	       sf.Name = re.Replace(sf.Name, "and") 
	    Else
	       MsgBox "Cannot rename " & sf.Name & " to " & Replace(sf.Name, re.Pattern, "and") & ". File already exists."
	    End If
    End If
  Next 
  For Each sf In f.SubFolders
    Recurse sf
  Next
End Sub

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 33614566
This is has error checking instead.
Set fso = CreateObject("Scripting.FileSystemObject") 
Set re  = New RegExp 
 
re.Pattern = "[&]" ' put all characters that you want to strip inside the brackets' 
re.IgnoreCase = True 
re.Global = True 
 
If WScript.Arguments.Unnamed.Count = 1 Then 
  If fso.FolderExists(WScript.Arguments.Unnamed(0)) Then 
    Recurse fso.GetFolder(WScript.Arguments.Unnamed(0)) 
  Else 
    WScript.Echo "Folder not found." 
  End If 
Else 
  WScript.Echo "Please give folder name as argument 1." 
End If 
 
 
Sub Recurse(f) 
  For Each sf In f.Files 
    If Right(LCase(sf.Name), 4) = LCase(".doc") Then
	     WScript.Echo sf.Name, " -> ", Replace(sf.Name, re.Pattern, "and")
	    On Error Resume Next
	       sf.Name = re.Replace(sf.Name, "and") 
	    If Err.Number <> 0 Then
	       MsgBox "Cannot rename " & sf.Name & " to " & Replace(sf.Name, re.Pattern, "and") & ". Error " & Err.Number & ": " & Err.Description
	       Err.Clear
	    End If
	    On Error GoTo 0
    End If
  Next 
  For Each sf In f.SubFolders
    Recurse sf
  Next
End Sub

Open in new window

0
 
LVL 4

Author Comment

by:JohnLo
ID: 33614593
ok lots of error 58's but here is what i think it might be doing. I think it is trying to rename anything with "&" in the name as well as  anything with "and" in the name. if i click ok to all the errors it will go through and finally rename it. issue is i cant click ok when this runs against a folder at night.
 
e.g. filenameandext.doc and not be renamed filenameandext.doc
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 33614613
If you change
MsgBox
to
wscript.echo
and then run the script with
cscript MyScript.vbs

it won't make you click OK....but does it still appear to work correctly?

Rob.
0
 
LVL 4

Author Comment

by:JohnLo
ID: 33630766
ill try it tonight.......
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 33670856
Hi JohnLo, did you have any luck with your testing?

Regards,

Rob.
0

Featured Post

Industry Leaders: 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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses

617 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