Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Replace substring in all files in all subfolders

Posted on 2011-09-26
3
Medium Priority
?
334 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
[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 Comments
 
LVL 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 600 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 800 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 58

Assisted Solution

by:Bill Prew
Bill Prew earned 600 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

Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

636 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