Solved

Batch script to parse a date out of a filename.

Posted on 2013-05-14
4
1,160 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 83

Accepted Solution

by:
oBdA earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

947 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now