Batch script that compress the files for previuos month

Hi all,
I need to create a batch script that will run on a specific date.
It will compress the files for the previous month.
Note: what i need is, this will compress the file from 1 to 30/31st of the month and generate the compressed folder  on 15th of next month.
The code that will run on every 15th of the month and compress the files before this month.
Prakhar MishraAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rlandquistCommented:
When you say compress, do you mean putting them in a zip file?
Are the files named based on the date, or does the script need to read the date the file was created?
0
Prakhar MishraAuthor Commented:
yes, you are right, first it will compress the file using 7_Zip and then it will give the name of that compressed folder as YYYY_Mon.zip, and yes it will read the dateon which file was created.
0
NVITEnd-user supportCommented:
Notes:
- Assumes 7zip is installed.
- Change to your needs: SrcDir, SrcFiles, ZipEXEDir
- Puts the YYYY_MM.zip files in your %TEMP% folder.
- To run it every 15th of the month, use Task Scheduler.

@echo off
cls
SETLOCAL ENABLEDELAYEDEXPANSION

set SrcDir=%USERPROFILE%\my documents
set SrcFiles=*.txt
set ZipEXEDir=C:\Program Files\7-Zip

set tmpLogYYYYMM=%temp%\ArcByYYYYMM_YYYY1.txt
set LogYYYYMM=%temp%\ArcByYYYYMM_YYYY.txt
set LogYYYYMMSorted=%temp%\ArcByYYYYMM_YYYYSorted.txt
set tmpLogFN=%temp%\ArcByYYYYMM_tmp.txt
set LogFN=%temp%\ArcByYYYYMM.txt

if exist "%temp%\ArcByYYYYMM_ZipList*.*" del /q "%temp%\ArcByYYYYMM_ZipList*.*"

for /r "%SrcDir%" %%a in ("%SrcFiles%") do (
   REM 12/19/2013 02:32 PM
   set FlName=%%~fa
   set FlDate=%%~ta
   set FlMM=!FlDate:~0,2!
   set FlYYYY=!FlDate:~6,4!
   echo !FlYYYY! !FlMM! >>"%tmpLogYYYYMM%"
   echo !FlYYYY! !FlMM! "!FlName!">>"%tmpLogFN%"
)
sort <"%tmpLogFN%">"%LogFN%"
sort <"%tmpLogYYYYMM%">"%LogYYYYMM%"
call :UniqueLines "%LogYYYYMM%">%LogYYYYMMSorted%
for /f "tokens=1-2" %%a in (%LogYYYYMMSorted%) do (
   set GroupYYYYMM=%%a_%%b
   set ZipListFN=ArcByYYYYMM_ZipList_!GroupYYYYMM!.txt
   set ZipListFullFN=%temp%\!ZipListFN!
   for /f "tokens=1-2*" %%A in (%LogFN%) do (
      if %%A equ %%a if %%B equ %%b echo %%C>>"!ZipListFullFN!"
   )
   pushd "%temp%"
   "%ZipEXEDir%\7z" a "%%a_%%b.zip" @!ZipListFN!
   popd
   REM exit /b
)
if exist "%tmpLogFN%" del /q "%tmpLogFN%"
if exist "%tmpLogYYYYMM%" del /q "%tmpLogYYYYMM%"
goto :eof

:UniqueLines
@echo off
setlocal disableDelayedExpansion
set "old="
for /f delims^=^ eol^= %%A in ('sort %1') do (
  set "new=%%A"
  setlocal enableDelayedExpansion
  if "!new!" equ "!old!" (endlocal) else (
    echo !new!
    endlocal
    set "old=%%A"
  )
)
goto :eof

Open in new window

0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Bill PrewIT / Software Engineering ConsultantCommented:
Give this a try, should do what you described.  Did you want the files deleted when they are added to the ZIP?

@echo off
setlocal EnableDelayedExpansion

REM Configure files to work with
set BaseDir=D:\Backup
set DestDir=E:\Backups
set zip=c:\program files\7-zip\7z.exe

REM Get current dates month and year
for /f "skip=2 tokens=2-3 delims=," %%A in ('WMIC Path Win32_LocalTime Get Month^,Year /Format:csv') do (
  set "mm=%%A"
  set "yyyy=%%B"
)

REM Calculate prior month
if %mm% EQU 1 (
  set mm=12
  set /a yyyy-=1
) else (
  set /a mm-=1
)

REM Zero pad month if needed
if %mm% LSS 10 set "mm=0%mm%"

REM Zip files from prior month
for %%A in ("%BaseDir%\*.*") do (

  REM Get month and year from this file
  set FileTime=%%~tA
  set FileYYYYMM=!FileTime:~6,4!!FileTime:~0,2!

  REM If it is from a month ago, zip it
  if "!FileYYYYMM!" EQU "%yyyy%%mm%" (
    "%zip%" a "%DestDir%\%yyyy%_%mm%.zip" "%%~A" -ssw
  )
)

Open in new window

~bp
0
NVITEnd-user supportCommented:
Ignore my post - I posted the wrong one. It zips everything, not just last month's
0
Prakhar MishraAuthor Commented:
Bill, I have two folders in my system
1. Source
2. Backups

I have written a batch script that will zip the files using 7Zip .
It will zip the files on every 15th of the month, and on the same day it will delete files from source folder and save that compressed folder(2015_3.zip) into Backups folder.

Now what i am searching is, to write a batch script that will first look into source folder for the files for previous month(March)  and then in next month(say April) on 15th it will compress those files  for Previous month(March) only  and save those compress file in Backups. and  then it will delete all those files from source.

this process will carry on  for the coming months also.

hope you all can understand what i want to tell you guyz...

thank you so much guyz.
0
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, that's just about what mine did, I adjusted the folder names, and added the delete of the file in.  Make sure you test this first on a sample data area, not the real dat, in case it doesn't work as expected.

@echo off
setlocal EnableDelayedExpansion

REM Configure files to work with
set BaseDir=C:\Source
set DestDir=C:\Backup
set zip=c:\program files\7-zip\7z.exe

REM Get current dates month and year
for /f "skip=2 tokens=2-3 delims=," %%A in ('WMIC Path Win32_LocalTime Get Month^,Year /Format:csv') do (
  set "mm=%%A"
  set "yyyy=%%B"
)

REM Calculate prior month
if %mm% EQU 1 (
  set mm=12
  set /a yyyy-=1
) else (
  set /a mm-=1
)

REM Zero pad month if needed
if %mm% LSS 10 set "mm=0%mm%"

REM Zip files from prior month
for %%A in ("%BaseDir%\*.*") do (

  REM Get month and year from this file
  set FileTime=%%~tA
  set FileYYYYMM=!FileTime:~6,4!!FileTime:~0,2!

  REM If it is from a month ago, zip it
  if "!FileYYYYMM!" EQU "%yyyy%%mm%" (
    "%zip%" a "%DestDir%\%yyyy%_%mm%.zip" "%%~A" -ssw
    del "%%~A" > NU>
  )
)

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Prakhar MishraAuthor Commented:
Hi BP,
i have executed the same script and i am getting an error : "4~0 was unexpected at this Time.
I also want to correct some part of the script :

WRONG ONE:
set FileYYYYMM=!FileTime:~6,4!!FileTime:~0,2!

  REM If it is from a month ago, zip it
  if "!FileYYYYMM!" EQU "%yyyy%%mm%" (

Corrected:
set FileYYYYMM=%FileTime:~6,4%%FileTime:~0,2%
if %FileYYYYMM% EQU %yyyy%%mm% (
0
Bill PrewIT / Software Engineering ConsultantCommented:
Traveling this week, will look into this next week.

~bp
0
Bill PrewIT / Software Engineering ConsultantCommented:
The lines you said you changed will not work properly, they needed to use the ! versions I had, since we were referencing a variable set inside a FOR loop.

On the error, I don't see 4~0 anywhere in the script, so don't see how that error could be caused by the code I posted.

~bp
0
Bill PrewIT / Software Engineering ConsultantCommented:
I just tested the script I posted and it works error free here, and does create the ZIP file, etc.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.