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.