Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Part of File Name Powershell

Tags:

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.

like image 434
MRAdmin23 Avatar asked Mar 04 '14 20:03

MRAdmin23


People also ask

How do you change a filename in PowerShell?

In Windows PowerShell, go to a file folder, enter dir | rename-item -NewName {$_.name -replace “My”,”Our”} and press Enter.

How do I replace a string in PowerShell?

You can call the PowerShell replace method on any string to replace any literal string with another. If the string-to-be-replaced isn't found, the replace() method returns nothing. You don't need to assign a string to a variable to replace text in a string.

What is LS in PowerShell?

For example, the command ls lists the directory content in Linux and macOS. Running the ls alias in PowerShell on Windows calls the PowerShell command Get-ChildItem.


2 Answers

simplified:

ls *.csv | Rename-Item -NewName {$_.name -replace "Default","VOD"} 
like image 93
lacecoasts Avatar answered Sep 20 '22 00:09

lacecoasts


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'} 
like image 42
Adil Hindistan Avatar answered Sep 22 '22 00:09

Adil Hindistan