Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

Powershell/Windows Batch/VB Script: replace string in folders and files

Hello experts,

I am looking for a batch, powershell or vbscript to cover the following requirement:

-Loop a reported folder with drill down
-Check the various files and folder
-Replace SourceString by TargetString
-SourceString and TargetString should be reported as parameter

-Example:
BaseDir= C:\Test
SourceString= “_”
TargetString “-“

-Various files and folders which contains source string “_” should be replaced by target  “-“.

If you have questions, please contact me.
Thank you.
Avatar of Bill Prew
Bill Prew

Are the replacements being made in the file name?  Of folders and files, or just files?  Or the extension?


»bp
Save this as Rename-ItemRecurse.ps1 or Whatever.ps1 (not in test mode, see below).
You can pass the start path, the source string, and the target string as parameters; if an argument is not passed, the defaults will be taken  from the command line.
In addition, you can use the following arguments:
-Verbose
-WhatIf (test mode, will not rename anything)
-Confirm (will list and prompt before renaming anything)
Examples:
.\Rename-ItemRecurse.ps1 -Path C:\toto -WhatIf
.\Rename-ItemRecurse.ps1 -Confirm

Open in new window

[CmdletBinding(SupportsShouldProcess=$true)]
Param(
	[Parameter(Position=0)]
	[string]$Path = 'C:\Test',
	[Parameter(Position=1)]
	[string]$SourceString = '_',
	[Parameter(Position=2)]
	[string]$TargetString = '-'
)
$verbose = $PSBoundParameters.ContainsKey('Verbose') -and $PSBoundParameters['Verbose'].ToBool()
$pattern = [regex]::Escape($SourceString)

Get-ChildItem -Path $Path -File -Recurse |
	Where-Object {$_.Name -match $pattern} |
	Rename-Item -NewName {$_.Name -replace $pattern, $TargetString} -Verbose:$verbose

Get-ChildItem -Path $Path -Directory -Recurse |
	Where-Object {$_.Name -match $pattern} |
	Sort-Object -Property @{e={$_.FullName.Split('\').Count}} -Descending |
	Rename-Item -NewName {$_.Name -replace $pattern, $TargetString} -Verbose:$verbose

Open in new window

If you are just talking about doing the replacements in file names (not folder names) then this should work.

@echo off
setlocal EnableDelayedExpansion

set BaseDir=B:\EE\EE29144987\test
set SourceString=_
set TargetString=-

for /f "tokens=*" %%A in ('dir /b /s /a-d "%BaseDir%\*%SourceString%*.*"') do (
    set FileName=%%~nA
    set FileName=!FileName:%SourceString%=%TargetString%!
    ren "%%~A" "!FileName!%%~xA"
)

Open in new window


»bp
Avatar of Luis Diaz

ASKER

Hello,
Is it possible to have flag to calibrate the procedure. 1 for file names 2 for folder names.
Batches and powershell approaches?
Thank you very much for your help.
The procedure concerns file names and not extensions.
EXPERT CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would just use fnr.exe - Find And Replace Tool for this
User generated imagehttps://archive.codeplex.com/?p=findandreplace
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much! Unable to test right now I will keep you informed!
Question: espace " " Can be reported as SourceString?
Example: replace " " by  "-".
SourceString= " "
TargetString= "-"
Yep.
Thank you.
To replace a space in the BAT approach, you would do this (there is one space after the equal sign):

set "SourceString= "

not:

set SourceString=" "


»bp
Noted. Thank you very much!