32

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.

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
MRAdmin23
  • 321
  • 1
  • 3
  • 3

4 Answers4

56

simplified:

ls *.csv | Rename-Item -NewName {$_.name -replace "Default","VOD"}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
lacecoasts
  • 561
  • 4
  • 2
  • 7
    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 – oleksa Mar 15 '19 at 10:53
  • 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. – Robert Kaucher Jun 24 '22 at 11:58
23

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'}
Adil Hindistan
  • 6,351
  • 4
  • 25
  • 28
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
}
Cole9350
  • 5,444
  • 2
  • 34
  • 50
  • 3
    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. – Travis Griggs Mar 04 '14 at 21:05
  • 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 – Cole9350 Mar 04 '14 at 21:43
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

Eris
  • 11
  • 2
  • 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. – nollaf126 Aug 10 '22 at 20:17
  • 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` – Eris Sep 30 '22 at 21:25