Solved

Replace substring in all files in all subfolders

Posted on 2011-09-26
3
312 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 53

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

856 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