Reputation: 411
I have files that are generated every morning:
I would like to rename the files:
I would basically be replacing Default
with VOD
.
I am using Powershell 1.0. Any help would be greatly appreciated.
Upvotes: 41
Views: 95085
Reputation: 11
If you're looking for a generic version that will take your find and replace as parameters and log out what you've renamed (handy, imo), here's one building from Cole9350's answer:
$Find = $args[0]
$Replace = $args[1]
$TargetFiles = Get-ChildItem | Where-Object {$_.Name -like "*$Find*"}
ForEach($File in $TargetFiles)
{
$newname = ([String]$File).Replace($Find,$Replace)
write-host "Renaming $File to $newname"
Rename-item -Path $File.PSPath $newname
}
And if you want it recursive, just add -Recurse
after Get-ChildItem
Upvotes: 1
Reputation: 671
simplified:
ls *.csv | Rename-Item -NewName {$_.name -replace "Default","VOD"}
Upvotes: 67
Reputation: 5560
gci | ?{$_.Name -like "Default*"} | % {$newname = ([String]$_).Replace("Default","VOD"); Rename-item $_ $newname}
A more explanatory version:
$DefaultFiles = Get-ChildItem | Where-Object {$_.Name -like "Default*"}
ForEach($File in $DefaultFiles)
{
$newname = ([String]$File).Replace("Default","VOD")
Rename-item -Path $File $newname
}
Upvotes: 7
Reputation: 6605
I do not have PowerShell 1.0 to test, but assuming you are running this in the folder where files reside, you can try:
Get-ChildItem Default_*.csv |Rename-Item -NewName {$_.name -replace '^Default','VOD'}
Upvotes: 24