Batch script to parse a date out of a filename.
Posted on 2013-05-14
This is probably easy for someone who is great at batch scripting, but I can't seem to figure it out.
I receive a monthly file that is sent to me via FTP. The filename is similar to this: Monthly_Y_20130514_X.zip (20130514 represents a date stamp that will change to when the file is uploaded)
So I need to parse the filename and extract out the 2013 and the 05. The tricky part, is that the file has to go in a folder for the PRIOR month, so for that example above, I'd need to unzip it to a folder like this:
Not too tricky now, but January has me all confused. A file such as Monthly_Y_20140105_X.zip would go into a c:\Files\2013\12 folder.
The steps as I see it are this:
1. Parse filename, put the year in one variable and the month in another.
2. Take the month variable, and subtract 1 unless it's a 1. In that case, make it a 12 instead and subtract 1 from the year as well.
3. Check to see if the year folder exists, create it if not.
4. Check to see if the month folder exists, create it if not.
5. Unzip the file into the folder.
Would be most appreciative if someone can help with this... If more info is needed, please just ask.