Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Batch script to parse a date out of a filename.

Posted on 2013-05-14
4
Medium Priority
?
1,642 Views
Last Modified: 2013-05-15
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
0
Comment
Question by:ealmquist
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 39165932
Try this for starters; you didn't mention how/from where the file name can be obtained, so this just expects it as first argument on the command line.
I'm assuming 7-Zip is installed to unzip the archive. The script is currently in test mode and will only display the folder creation and unzipping commands it would normally run; remove the uppercase ECHOs in line 25 and 26 to run it for real.
@echo off
setlocal
if "%~1"=="" (
	echo Syntax:
	echo %~nx0 ^<Archive^>
	goto :eof
)
set Archive=%~1
if not exist "%Archive%" (
	echo Error: Archive '%Archive%' not found.
	goto :eof
)
for %%a in ("%Archive%") do set ArchiveName=%%~na
for /f "tokens=3 delims=_" %%a in ("%ArchiveName%") do set TimeStamp=%%a
set YYYY=%TimeStamp:~0,4%
set MM=1%TimeStamp:~4,2%
set /a MM -= 1
if "%MM%"=="100" (
	set MM=12
	set /a YYYY -= 1
) else (
	set MM=%MM:~1,2%
)
set TargetFolder=C:\Files\%YYYY%\%MM%
ECHO if not exist "%TargetFolder%" md "%TargetFolder%"
ECHO "%ProgramFiles%\7-Zip\7z.exe" x -y -o"%TargetFolder%" "%Archive%"

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39165937
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%"
)
0
 

Author Comment

by:ealmquist
ID: 39165984
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...
0
 

Author Closing Comment

by:ealmquist
ID: 39168343
This seems to do the trick!  I had to do a little tweaking but it accomplished everything I needed.  Thanks so much!
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question