Solved

Batch script to parse a date out of a filename.

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The viewer will learn how to count occurrences of each item in an array.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

705 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

19 Experts available now in Live!

Get 1:1 Help Now