I’m in need a script, in PowerShell or batch script, that will do the following.
-
Rename a file to append creation date minus 1 day to the filename.
For example:
foo.xlsx (created 7/27/2011)
foo-2011-07-26.xlsx –note, it’s yesterday’s date.
Date format isn’t too important as long as it’s there. There will be 10 files (all with the same creation date), so either I can copy and paste the same renaming line for the different files (just rename the filename) or just have the script affect all *.xlsx files in the existing folder.
-
Create a new folder where those files are and name it ‘fooFolder-2011-07-26’ (yesterday’s date).
- Move those renamed files to that folder.
I only have limited experience with PowerShell. It’s on my todo list of languages to learn..
Here you go. It could be shortened up a lot using aliases and piping and whatnot, but since you’re unfamiliar with Powershell still, I decided to write in a more procedural style for your reading:
You would paste the above into your Powershell session (place it in your profile). Then you call the function like this:
I tend to use more aliases and piping than the above function. The first version I wrote was this, and then I separated parts of it to make it more comprehensible to a Powershell newcomer:
Edit: Modified both functions to create dated folder for the files that match that date. I considered making a temporary directory and grabbing a single date from the files moved to it, finally renaming the directory after the loop. However, if a day should be missed and files for 2 (or more) days get processed together, there would still be a folder for each day with these, which is more consistent.