?
Solved

VBS Rename Script not working correctly.

Posted on 2010-09-06
8
Medium Priority
?
462 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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.

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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 …
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…
Watch the video to know the process of migration of Exchange or Office 365 mailboxes in absence of MS Outlook. It is an eminent tool which can easily migrate Public, Archive user mailboxes from one another Exchange server and Office 365. Kernel Migr…

569 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