Solved

Replace substring in all files in all subfolders

Posted on 2011-09-26
3
301 Views
Last Modified: 2012-05-12
Hi,

I'm looking for a vb script or a .bat script to rename all my files in a folder (and subfolders).

e.g.

There are 4 subfolders in a folder, with names like newyear2010, easter2010, christmas2010.
And I would like to rename the 2010 into 2011.

Can you guys help me?
0
Comment
Question by:Silencer001
3 Comments
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 150 total points
ID: 36600621
Dim fso, f, sf

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("c:\folder")
For Each sf In f.SubFolders
    If Right(sf.Name, 4) = "2010" Then sf.Move Replace(sf.Path, "2010", "2011")
Next
Set sf = Nothing
Set f = Nothing
Set fso = Nothing
0
 
LVL 9

Accepted Solution

by:
chrismerritt earned 200 total points
ID: 36601499
Powershell variation:

$RootPath = "C:\TEMP\Subfolder"

$Files = Get-ChildItem $RootPath -Recurse | ? {$_.PSIsContainer -eq $False}

foreach ($File in $Files)
{
	if ($File.Name -match "2010")
	{
		$NewFileName = ($File.Name).Replace("2010","2011")
		Rename-Item $File.FullName $NewFileName
		
		Write-Host -ForegroundColor "yellow" "Renamed File : $($File.FullName) to $NewFileName"
	}
}

Open in new window

0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 150 total points
ID: 36717035
Here's a BAT approach, since that was one of the ones you showed interest in.  If you go this route I'd appreciate it if you could ad the NS-DOS zone to the question before closing it.  Save the below and adjust the BaseDir as needed.

@echo off
setlocal EnableDelayedExpansion
set BaseDir=c:\EE\EE27327959
for /F "tokens=*" %%A in ('dir /B /S /AD "%BaseDir%\*2010*"') do (
  set NewName=%%~nA
  set NewName=!NewName:2010=2011!
  ren "%%~A" "!NewName!"
)

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

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

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 …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

22 Experts available now in Live!

Get 1:1 Help Now