41

I have files that are generated every morning:

  • Default_20140227_05.00.29.csv
  • Default_20140226_05.00.29.csv

I would like to rename the files:

  • VOD_20140227_05.00.29.csv
  • VOD_20140226_05.00.29.csv

I would basically be replacing Default with VOD.

I am using Powershell 1.0. Any help would be greatly appreciated.

4 Answers 4

68

simplified:

ls *.csv | Rename-Item -NewName {$_.name -replace "Default","VOD"}
Sign up to request clarification or add additional context in comments.

3 Comments

works like a charm. However please note that -replace treat search parameter value as regex expression so you have to use special regex characters for certain values, like \s to find and replace the space char
On Mac (and it will likely be the same on Linux), I had to use gci rather than ls likely because ls is a native command.
This gave an error for me. I ended up using the approach from this answer
26

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'}

Comments

7

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
}

2 Comments

Care to add any explanation? I don't get the impression that the poster has the ability to necessarily parse/maintain that. I drop in "it works, but I don't have a clue how" isn't much better than not knowing.
It seems pretty self explanatory if you know the commands, I shouldn't have been using the aliases or pipelining I guess.. But then again I tend to put as much effort into my answers as the asker does the question... updated anyways
1

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

2 Comments

This looks like exactly what I need, but it says '$Find' is not recognized as an internal or external command, operable program or batch file. I made a "test.cmd" file with your code, and get the error when running the file from a powershell prompt, and when I drag a folder and drop it on the test.cmd file (what I'm wanting to achieve), it just briefly flashes a cmd window with no renaming taking place.
Simple fix for ya @nollaf126 . This code is written using Powershell, so you'll want a .ps1 extension for your file, not .cmd - that should fix your error. You'll also need to provide two arguments so if the file is called rename.ps1 and you want to rename Hat to HeadSock, you'll need execute rename.ps1 Hat HeadSock

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.