Monday, November 24, 2014

windows - Adding date to the name of multiple files in multiple folders


I have a directory which contains 400+ subfolders. In each of this subfolders a new file appears every day. Each new file has the same name as the file that appeared in the same folder yesterday. Each folder contains files with different names than in the other folder, but new files within the same folder always have the same name. I don't want the files to be overwritten everyday. Instead I would like Powershell to add todays date after the name of the file and before the extension. I know how to do it for a single subfolder, but I don't want to create 400+ scripts (one for each subfolder).


This is the script which does the job perfectly for each single folder.


$fileToRename = "E:\Backup\ABLBackup\folder1\Project1.txt"
if (Test-Path $fileToRename)
{
$fileName = [system.io.path]::GetFileNameWithoutExtension($fileToRename)
$dateSuffix = get-date -Format 'yyyy_MM_dd'
$fileExtension = [system.io.path]::GetExtension($fileToRename)
$newFileName = '{0}_{1}{2}' -f $fileName, $dateSuffix, $fileExtension
Rename-Item -Path $fileToRename -NewName $newFileName
}

I would like to know how to modify the script above so that all 400+ subfolders could be included in one script. I know that I can repeat the same command 400+ times and save it as one script, but thats not feasible because users add new folders without informing me. Surely there must be some automatic way of getting all new files that appeared in the subfolders.


Structure of the folders looks like this:


E:\Backup\ABLBackup\folder1\Project1.txt


E:\Backup\ABLBackup\folder2\Project2.txt


E:\Backup\ABLBackup\folder3\Project3.txt


E:\Backup\ABLBackup\folder4\Project4.txt


Any help will be greatly appreciated!


Answer



I'm a fan of keeping things simple, as long as it doesn't affect performance or function. You could add listeners to detect when new files are added and automatically rename them but that is a complexity I'm not particularly interested in learning right now, much less explaining lol.


So a simple loop-if-then is the way I would go.


First of all you need to loop through these folders:


Get-ChildItem "E:\Backup\ABLBackup" |
Where PSIsContainer |
Foreach-Object {}

Next as I understand it you need to check if there is a new file (basically one that does not contain a date)


if (Get-ChildItem $_.FullName | Where Name -notlike "*_????_??_??.*") {}

Put it together with your script, tidied to suit:


Get-ChildItem "E:\Backup\ABLBackup" |
Where PSIsContainer |
Foreach-Object {
$newitem = Get-ChildItem $_.FullName | Where Name -notlike "*_????_??_??.*"
if ($newitem)
{
$dateSuffix = get-date -Format 'yyyy_MM_dd'
$newFileName = '{0}_{1}{2}' -f $newitem.BaseName, $dateSuffix, $newitem.Extension
Rename-Item $newitem -NewName $newFileName
}
}

Set up a scheduled task to run every half hour or something and you're laughing.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...