1

I have never used Powershell (or VBScript) as I am new to IT-Admin and would appreciate some guidance with renaming files.

On a daily basis, I will be downloading files useing Filezilla manually from a SFTP connection to a local directory filder called YYYYMMDD (although later, I will want to write an automated task that does this form me on a scheduled basis). What I would like to set up is some kind of task that when the files land into the distination directory, they get renamed.

For today's download, I have the following files that I would like to rename:

Original Name                   Renamed Filename
wcm14444.csv.11.10.20_09.32     wcm14444_111020_0932.csv
wcm14444.csv.11.10.21_00.09     wcm14444_111021_0009.csv
wcm14444.pdf.11.10.20_09.32     wcm14444_111020_0932.pdf
wcm14444.pdf.11.10.21_00.10     wcm14444_111021_0010.pdf
wcm1cash.csv.11.10.21_00.56     wcm1cash_111021_0056.csv
wcm1money.csv.11.10.21_00.56    wcm1money_111021_0056.csv
wcm1opnpos.csv.11.10.21_00.56   wcm1opnpos_111021_0056.csv
wcm1trades.csv.11.10.21_00.56   wcm1trades_111021_0056.csv
wcm1_an.pdf.11.10.21_03.26      wcm1_an_111021_0326.pdf
wcm1_ds.pdf.11.10.21_00.22      wcm1_ds_111021_0022.pdf
wcm1_ep.csv.11.10.21_03.26      wcm1_ep_111021_0326.csv
wcm1_ms.pdf.11.10.21_03.26      wcm1_ms_111021_0326.pdf

You will notice in my renaming requirement: 1. The file extension appears in the middle of the filename and gets placed to the end. 2. I am replacing "." with "" where they appear as date seperators. If its of any help, I am only expecting to receive file types of (".csv", ".pdf" , ".xls") and where these appear within the filename, they get replaced with "_".

Currently I would use Excel to perform the task, but this seems quite hard to deploy as a system task? This seems more of a task for Powershell.

If I am creating YYYYMMDD folders for example N:\SFTP\Provider1\YYYYMMDD, what would be the best way of automating the renaming of the files as the files are downloaded into each day's YYYYMMDD (noting that on same days there may not be a folder created because there are no new files). Many thanks and kind regards, Bertie.


Thanks all for the help. More for the benefit of those stumbling accross this page. I have now created a Rename_SFTP.ps1 file at N:\SFTP\Provider1\ containing

$pattern = '^([^\.]+)\.(csv|xls|pdf)\.(\d{2})\.(\d{2})\.(\d{2})_(\d{2})\.(\d{2})'
$todayDate = Get-Date -Format "yyyyMMdd"

Get-ChildItem (".\" + $todayDate + "\") -Recurse | Foreach-Object{
    if($_.Name -match $pattern)
    {
        echo $NewName
        $NewName = "{0}_{1}{2}{3}_{4}{5}.{6}" -f $matches[1],$matches[3],$matches[4],$matches[5],$matches[6],$matches[7],$matches[2]
        Rename-Item (".\" + $todayDate + "\" + $_) -NewName $NewName
    }
}

I have then created a RunRenameTaskForToday.bat containing

powershell .\Rename_SFTP.ps1

At least this way, when I download the files manually, all I need to do is double click on a .bat file one level higher up and it will figure the right folder that needs the files to be renamed. All I need to do know is figure out a way to downlaod the SFTP stuff autonatically...

Thanks all.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Bertie
  • 1,163
  • 3
  • 14
  • 26

3 Answers3

2

Quite a complex task here, with several problems (and, most probably several solutions). Here are some guidelines to get you going:

  • Use Get-ChildItem | Foreach-Item to traverse all files. The $_ holds the current item.
  • Powershell has very good support for regular expressions, use if ($_ -match "regexp"). I tend to name the matches, to easier identify them. Something like this could work for your specific naming format:

    if ($s -match "(?<pre>.*)\.(?<ext>csv|pdf)(?<date>.*)_(?<hour>\d{2})\.(?<min>\d{2})") { $newname = $matches["pre"] + "_" + ($matches["date"] -replace "\.","") + "_" + $matches["hour"] + $matches["min"] + '.' + $matches["ext"] }

  • To get the current day, use Get-Date -Format "yyyyMMdd". Use that to create a new folder each day.

Johnno Nolan
  • 29,228
  • 19
  • 111
  • 160
Torbjörn Bergstedt
  • 3,359
  • 2
  • 21
  • 30
1

Well quickly something called bricolage ...

$ext = ("csv", "pdf", "xls")

foreach ($e in $ext)
{    
  Get-ChildItem "*$e*" | % {$newName = $_.name.replace('.',''); $newName;
                            Rename-Item $_.fullname -NewName ($newName -replace "(.*)($e)(.*)",'$1_$3.$2')}
}
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
1

Here's another way using a format string to create the new name. Each group of pattern characters surrounded in parenthesis denotes a part of the file name to be captured and is used later on in the $NewName to form the new file name:

$pattern = '^([^\.]+)\.(csv|xls|pdf)\.(\d{2})\.(\d{2})\.(\d{2})_(\d{2})\.(\d{2})'


Get-ChildItem D:\temp -Recurse | Foreach-Object{
    if($_.Name -match $pattern)
    {
        $NewName = "{0}_{1}{2}{3}_{4}{5}.{6}" -f $matches[1],$matches[3],$matches[4],$matches[5],$matches[6],$matches[7],$matches[2]
        Rename-Item $_ -NewName $NewName
    }
}
Shay Levy
  • 121,444
  • 32
  • 184
  • 206