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!