Solved

Adding directory recursion to a file name change script

Posted on 2007-03-20
4
267 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
[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
  • 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
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…

631 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