Solved

Replace substring in all files in all subfolders

Posted on 2011-09-26
3
320 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 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 54

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get all Files under specific folder 1 33
PowerCli import module 2 52
Powershell script to import email address from csv file 2 32
Strip 'trailing' spaces 10 32
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
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ā€¦

732 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