1

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!

1 Answer 1

1

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.

3
  • As a side note, THANK YOU for not using periods to separate your dates. You have no idea how much trouble that has caused us here ._. Mar 11, 2016 at 12:49
  • Also worth noting this is assuming there will only ever be one file in each folder that doesn't have a date on the end. You would need to check $newitem -is [array] and handle with another Foreach loop otherwise. Mar 11, 2016 at 12:53
  • Thank you much for your answer! I thought about looping but I couldn't come up with anything that the names would have in common. Now that you pointed out that new files won't have date, all seems obvious! Thanks to your sugestion I now know what to do! Yes, there will always only be one new file (luckily!)
    – QWE
    Mar 11, 2016 at 12:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .