Solved

Batch script to parse a date out of a filename.

Posted on 2013-05-14
4
1,319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 84

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

734 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