I have a root dir that has hundreds of folders in it that are named with a legacy company name and date format, I want to rename these files within all the folders with PowerShell, I have been trying all day but no luck thus far :frowning: I’m a noob to scripting, only ever normal have to do login scripts and easy stuff.

I need to maintain the date format on the files so they need to look like example new_businessname06092021.csv

Get-ChildItem –Path "C:\temp\AMLReports" -Recurse -Filter *.csv
foreach($file in Get-ChildItem $SomeFolder)
{
    $newname = ([String]$File).Replace("oldbuisnessname","newbusinessname")
    Rename-item -Path "C:\temp\AMLReports\Renamed" $File $newname
}

Help appreciated :slight_smile:

3 Spice ups

Welcome

If you post code, please use the ‘Insert Code’ button. Please and thank you!

codebutton_small.png

how about like so?

$files = Get-ChildItem "C:\temp\AMLReports" *.csv -Recurse

foreach($file in $files){
    $newname = $file.name -replace "oldname","newname"
    Rename-item $file.fullname -NewName $newname -verbose
}

Test it before you let it lose in your prod environment.

How did you make that so easy!

I wish I had your brain :slight_smile:

Now I need to open every .csv with the root dir and find and replace… dont suppose this is as easy to achieve… I have been playing with

powershell -Command "(gc INPUT.csv) -replace 'oldname', 'newname' | Out-File -encoding ASCII OUTPUT.csv"

I have know idea how to incorporate this into the script you fixed up for me, thanks heaps!

this community is amazing

Miles

I’d update the content first and then rename.

$files = Get-ChildItem Get-ChildItem "C:\temp\AMLReports" *.csv -Recurse

foreach($file in $files){
    $old     = get-content $file.FullName
    $newfile =  $old -replace "oldname","newname"
    $newfile | out-file $file.FullName -Force

    $newname = $file.name -replace "oldname","newname"
    Rename-item $file.fullname -NewName $newname
}

Morning from NZ

I updated the names and run it in test, didn’t seem to do anything PowerShell just opened and closed, and didn’t appear to run any lines (Reminder I’m a complete Noob :P)

Just for clarification

I have a folder that contains hundreds of folders that have csv files in them that have been named oldbusinessname-date.csv

These files all need renamed to newbusinessname-date.csv (This Script tested working for this) Thanks!

$files = Get-ChildItem "C:\temp\AMLReports" *.csv -Recurse

foreach($file in $files){
    $newname = $file.name -replace "oldname","newname"
    Rename-item $file.fullname -NewName $newname -verbose
}

My second issue is the CSV files that live within the folders have a column within the spreadsheet that is named oldbusinessname that needs renamed to newbusinessname.

I tried running this and PS just opened and closed without running any lines and no error.

$files = Get-ChildItem Get-ChildItem "C:\temp\AMLReports" *.csv -Recurse

foreach($file in $files){
    $old     = get-content $file.FullName
    $newfile =  $old -replace "oldname","newname"
    $newfile | out-file $file.FullName -Force

    $newname = $file.name -replace "oldname","newname"
    Rename-item $file.fullname -NewName $newname
}

I do very much appreciate you taking the time to help me!

Miles

if you run it just like this, powershell would not indicate if anything was done, you’d just see red if it fails.

Did it actually update stuff? It worked fine in my test

I do not understand what you mean by " closed without running any lines" ? What do expect?

to see if powershell is actually doing something you need to add ‘-verbose’

$files = Get-ChildItem "C:\temp\AMLReports" *.csv -Recurse

foreach($file in $files){
    $old     = get-content $file.FullName
    $newfile =  $old -replace "oldname","newname"
    $newfile | out-file $file.FullName -Force
    write-verbose "Updated $($file.fullname)" -verbose

    $newname = $file.name -replace "oldname","newname"
    Rename-item $file.fullname -NewName $newname -Verbose
}

With the renaming on the csv files I could see PS running heaps of stuff with the renames as there is hundreds of them, I have tried with the Verbose and still the same thing ps just opens and closes again, the content hasn’t changed, I’m using office 365 Apps - don’t suppose I have to run ps an admin or something?

Here is a csv sample if it helps

297a4f68-59f3-4308-bb9b-4cda3d68b0a9-oldname_20210607_138_Customers_43.csv (855 Bytes)

the CSV should not matter.

Yes, as admin would help.

how do you run it?

$files = Get-ChildItem "C:\temp\AMLReports" *.csv -Recurse
$files.count

if you just run this, does it get a count? does it find files? how do you run it? in the ISE? from the console? as a script?

The same thing happens with that, just opens and closes.

I create a new file.ps1 then right click on it to edit paste the code in, save, then close, then right click on file and run with PowerShell

I’m wanting to run script as a daily scheduled task.

I’m not sure how to run as the file as admin, no shift right click options on the file

I get the attached with run from within the ps console

you open the powershell console as admin and then just call the script.

looks like you have also execution policy restriction.

in task scheduler you’d call it like so

powershell.exe -executionpolicy bypass -file "c:\scripts\script.ps1"

It works if I run PS as admin I get the file count, ill test now running the rename files :slight_smile:

ITS working! running as admin helped :slight_smile: however it has lost the formatting almost looks like it has comer separated and taken out the columns, does it need the encoding ASCII added somehow?

the encoding and such depends on your CSV files.

out-file has a few build in encoding option:

in my test it seemed to work fine

$csv = get-content ".\Input.csv"
$new = $csv -replace "dumbo","neally"
$new | out-file  ".\Input2.csv"

8477024d-8960-4b2a-83d6-5a094a57cfc6-2021-06-09_18-06-45.png

yeah so the default encoding
→ windows powershell (5.1) is unicode with BOM. (utf-16)
→ powershell (7.x) is utf8NoBOM

so try like so

out-file  $file.FullName -Encoding ascii -Force

On the working test I have just noticed that It did put this at the top not sure if that’s related.

Get-ChildItem : A positional parameter cannot be found that accepts argument '*.csv'.
At line:1 char:10
+ $files = Get-ChildItem Get-ChildItem "C:\temp\AMLReports" *.csv -Recu ...
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

I have attached the before file and after file for reference

dd8dac5e-8056-447e-91a7-087134f5c811-OLDNAME_20210127_008_Customers_02-_PRIOR.csv (846 Bytes)

output

2e9b2ad3-3020-4122-ad5e-d99e6ed49b49-NEWNAME_20210127_008_Customers_02-POST.csv (1.21 KB)

Looks like there is a typo and there is ‘get-childitem’ twice, should only be there once

You are my new best FRIEND !!!

For the record this is what your patients has produced for this noob and hopefully this can help others

$files = Get-ChildItem "C:\temp\AMLReports" *.csv -Recurse

foreach($file in $files){
    $old     = get-content $file.FullName
    $newfile =  $old -replace "oldname","new_name"
    $newfile | out-file  $file.FullName -Encoding ascii -Force

    $newname = $file.name -replace "old_name","new_name"
    Rename-item $file.fullname -NewName $newname
}

Once again thank you this has been a life saver!!