Solved

Adding directory recursion to a file name change script

Posted on 2007-03-20
4
225 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever had a hard drive that you can't boot into, but need to change the registry? Here is the solution! This article guides you through accessing and editing a registry of a non-primary drive. To read registry information on a non-prim…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now