VBS Rename Script not working correctly.


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

LVL 4
JohnLoAsked:
Who is Participating?
 
RobSampsonCommented:
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
 
RobSampsonCommented:
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
 
JohnLoAuthor Commented:
this is so weird, Back to.....
(24, 9) Microsoft VBScript runtime error: File already exists
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
RobSampsonCommented:
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
 
JohnLoAuthor Commented:
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
 
RobSampsonCommented:
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
 
JohnLoAuthor Commented:
ill try it tonight.......
0
 
RobSampsonCommented:
Hi JohnLo, did you have any luck with your testing?

Regards,

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.