Link to home
Create AccountLog in
Avatar of ealmquist
ealmquistFlag for United States of America

asked on

Batch script to parse a date out of a filename.

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:

c:\Files\2013\04

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.

Thanks!

Josh
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
OK, first to getting the file, do you automate getting it from ftp at the moment, or does it appear in a local directory for you having been sent by ftp?  If you do have to pick it up we could always script that too...

As to the filename lets say you have this as the only matching file in C:\source directory then you could do something like this.  I have split the filename at the _ characters to get the date bit as it is unclear whether the "Y" and "X" bits could be different so not taking the nth letter etc.

I haven't tested this yet but will shortly.....

Steve

rem @echo off
setlocal enabledelayedexpansion
set source=c:\source
set dest=c:\dest
set matching=Monthly*.zip
set filename=

REM e.g. for filename like Monthly_Y_20130514_X.zip get date from 3rd token split by _
for /f "tokens=1-3* delims=_" %%A in ('dir /b /a-d "%source%\%matching%"') do (
  set filename=%%~A_%%~B_%%~C_%%~D
  set year=%%~C
  set /a month=1!year:~4,2! - 101
  set year=!year:~0,4!
  if !month!==0 (
      set month=12
      set /a year=year -1
  )
  if !month! LSS 10 set month=0!month!
)
if "%filename%"=="" (
  echo There was no file found
) ELSE (
  echo The file found was %filename% and it needs to go into %year% and %month%
  md  "%dest%\%year%\%month%"
  copy "%source%\%filename%"  "%dest%\%year%\%month%"
)
Avatar of ealmquist

ASKER

I will test these shortly, much thanks!

To answer some questions, the file is sent to us via FTP automatically.  There could be multiple "Monthly*" files sent in a given month, but they'll come automatically.  They come encrypted via PGP and I have a little script that removes the PGP encryption and puts them in a network share as a .zip file.  I then want to unzip this file from that share to that folder structure I mentioned above.

And yes, 7-zip is installed...
This seems to do the trick!  I had to do a little tweaking but it accomplished everything I needed.  Thanks so much!