Solved

Adding directory recursion to a file name change script

Posted on 2007-03-20
4
257 Views
Last Modified: 2010-03-05
I have a script for renaming files and I want to add an instruction to recurse through subdirectories. What do I need to add?

Thanks!
JohnD


Dim filesys, file, folderName, folderObj, fileColl, objRegEx, newFile
Set filesys = CreateObject("Scripting.FileSystemObject")
folderName = InputBox("", "Folder Name")
'Make sure there's a trailing slash
If Right(folderName, 1) <> "\" Then folderName = folderName & "\"
Set folderObj = filesys.GetFolder(folderName)

For Each file In folderObj.Files
  If InStr(file.Name, "_copy") > 0 Then 'Only process files that have _copy
    filesys.MoveFile file.Path, Replace(file.Path, "_copy", "")
  End If
Next
0
Comment
Question by:johndarby
  • 3
4 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 18760986
Hi johndarby...try this..


Dim filesys, file, folderName, folderObj, fileColl, objRegEx, newFile
Set filesys = CreateObject("Scripting.FileSystemObject")
folderName = InputBox("", "Folder Name")
'Make sure there's a trailing slash
If Right(folderName, 1) <> "\" Then folderName = folderName & "\"

ProcessFolder (filesys.GetFolder(folderName))
set filesys=Nothing
wscript.quit

Sub ProcessFolder(strSource)
  ProcessFiles strSource
  For Each fld In strSource.SubFolders
    ProcessFolder fld
  Next
End Sub


Sub ProcessFiles(strSrc)
For Each file In strSrc.Files
  If InStr(file.Name, "_copy") > 0 Then 'Only process files that have _copy
    filesys.MoveFile file.Path, Replace(file.Path, "_copy", "")
  End If
Next
End Sub
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18760995
This is merely for cleanup - you've still got some unused declarations in there...
Shorten the first line to:

Dim filesys, file, folderName
0
 
LVL 1

Author Comment

by:johndarby
ID: 18761167
Thanks SB...I appreciate your help as I become a little bit better at script work!
JohnD
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18761172
Glad I could assist you once again...have fun!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Many admins will agree: WSUS is is a nice invention but using it on the client side when updating a newly installed computer is still time consuming as you have to do several reboots and furthermore, the procedure of installing updates, rebooting an…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

713 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