?
Solved

Replace substring in all files in all subfolders

Posted on 2011-09-26
3
Medium Priority
?
324 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 56

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

743 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