Luis Diaz
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.
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.
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:
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
[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
If you are just talking about doing the replacements in file names (not folder names) then this should work.
»bp
@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"
)
»bp
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I would just use fnr.exe - Find And Replace Tool for this
https://archive.codeplex.com/?p=findandreplace
https://archive.codeplex.com/?p=findandreplace
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much! Unable to test right now I will keep you informed!
ASKER
Question: espace " " Can be reported as SourceString?
Example: replace " " by "-".
SourceString= " "
TargetString= "-"
Example: replace " " by "-".
SourceString= " "
TargetString= "-"
Yep.
ASKER
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
set "SourceString= "
not:
set SourceString=" "
»bp
ASKER
Noted. Thank you very much!
»bp