Batch script for previous month files

I have created a batch script that will look for the files which are from previous month(for e.g if current month is Nov then it will look for the files from Oct). The source from where it needs to look for the files having more than 75000 files. So my script is traversing each file and then checking it if it is from last month. If file is from last month it is moving them into a temp folder and then it is compressing that temp folder.

Doubts: As script needs to traverse ~ 75000 files it is taking so much of time. Is there any way that script will look for the files which are from last month only. It will not touch for the files which are from current month.

below I have given the code:
set src_dir=%1%
set back_up=%2%
set save_file=%3%
set ZipTool=%4%

echo Verify System date
echo -------------------
for /f "skip=2 tokens=2-3 delims=," %%A in ('WMIC Path Win32_LocalTime Get     Month^,Year /Format:csv') do (
    set /a "mm=%%A"
    set /a "yyyy=%%B" 
set err=%errorlevel%
if not %err%==0 goto Date_error

set /a pre_month=%mm%-1 
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%"
    if %pre_month% LSS 10 set "pre_month=0%pre_month%"
    echo Prev_Month in yyyymm formate is: %yyyy%%mm%

    REM creating TEMP folder
    set temp=%back_up%\temp\%yyyy%%pre_month%
    REM Creating  Temporary Folder
    mkdir %temp%
    echo %temp% created...
    set /a count=0

    REM moving files into TEMP Folder
    for %%A in ("%src_dir%\*.*") do (
    REM Get month and year from this file
    set  "FileTime=%%~tA"
    set "FileYYYYMM=!FileTime:~6,4!!FileTime:~,2!"
    ::echo !FileYYYYMM! and %yyyy%%mm%>nul
    REM If it is from a current Month, move it to temp.
    if "%yyyy%%mm%" EQU "!FileYYYYMM!" if !count! LSS 10000 (
    set /a count=count+1
    move %%~fA  %temp% >nul ) )

    set err=%errorlevel%
    if not %err%==0 goto Error

    echo Starting 7 Zip Process..
    echo --------------------------
    %ZipTool% a -mx=9 %back_up%\ %tmp%\*.* -ssw > nul 
    set err=%errorlevel%
    if not %err%==0 goto Error

    echo Compresed Files .... >%save_file%
    echo --------------------------->>%save_file%
    for %%G in ("%tmp%\*.*") do echo %%~nG >>%save_file%
    echo ---------------------------->>%save_file%

    if exist %save_file% ( 
    call filesize %save_file% 0
    echo %save_file% has been attached 

    pushd %back_up%\temp
    rd /q /s %yyyy%%pre_month%
    set err=10
    goto END

echo -----------------------
    echo ERROR: 
    echo Unable to fetch System Date.
    echo -----------------------
    set err=20

    echo -----------------------
    echo ERROR: 
    echo %yyyy%%pre_month% doesn't exist.Please verify.
    echo -----------------------
    set err=20
    echo -----------------------
    echo ERROR: 
    echo Script Execution Error..Please verify.
    echo -----------------------
    set err=20

    echo -----------------------
    echo Prompt Error: 
    echo Prompt missing from the job..Please verify.
    echo -----------------------
    set err=20

    echo Bat Return Code: %err%
    ECHO == END process ==

exit /b %err%

Open in new window

Note: I have tried with forfiles but didn't get required results. I appreciate your suggestions.
Prakhar MishraAsked:
Who is Participating?

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

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.

NVITEnd-user supportCommented:
> ... have tried with forfiles but didn't get required results

Forfiles is faster. What's different? Maybe can adapt your  code to work with it
Bill PrewIT / Software Engineering ConsultantCommented:
See if this gives you some ideas.  I stripped out a lot of the extra code but add back the error checking and such you want.

I used WMIC to find the files from the prior month and write them to a text list file.  It looked like you were using 7ZIP so it can take that list of files as input for the zip operation and add those listed files to the archive.

The WMIC query syntax takes some getting used to, so I had to do a little work to prepare the input directory to get the files from.  Also, it returns a slightly odd format with an extra carraige return at the end of each line, which can cause problems in BAT scripts, so we remove that too.

Let me know what questions you have, hope this makes sense.

@echo off
setlocal EnableDelayedExpansion

REM Get parms from command line into variables. Define a work file for zip files list.
set src_dir=%~1
set back_up=%~2
set save_file=%~3
set ZipTool=%~4
set TempList=%Temp%\_templist.txt

REM Main Processing
call :CalcPriorMonth
call :ParseFolder
call :FindFiles
call :ZipFiles
call :DeleteFiles

REM Done
exit /b

REM Calculate the prior month from todays date
    REM Get current month and year, store in variables
    for /f "skip=2 tokens=2-3 delims=," %%A in ('WMIC Path Win32_LocalTime Get Month^,Year /Format:csv') do (
        set /a "CurrMonth=%%A"
        set /a "CurrYear=%%B"
        REM If month one, then prior month will be 12 and prior year is one less than current year
        if %%A EQU 1 (
            set "PriorMonth=12"
            set /a "PriorYear=%%B-1"
        REM If not month one, then prior month will be one less than current month, prior year is current year
        ) else (
            set /a "PriorMonth=%%A-1"
            set "PriorYear=%%B"

    REM Add zero padding if needed to months less than 10
    if %CurrMonth% LSS 10 set "CurrMonth=0%CurrMonth%"
    if %PriorMonth% LSS 10 set "PriorMonth=0%PriorMonth%"

    REM Build current and prior month dates using the 1st day of the month (used as range to look for files via WMIC)
    set "CurrYYYYMMDD=%CurrYear%%CurrMonth%01"
    set "PriorYYYYMMDD=%PriorYear%%PriorMonth%01"

    exit /b

REM Prepare the input folder name for WMIC usage (split out drive letter, and change "\" to "\\")
    for /f "tokens=1-2 delims=:" %%A in ("%src_dir%") do (
        set WmicDrive=%%A:
        set WmicPath=%%B
    if "%WmicPath:~-1%" NEQ "\" set WmicPath=%WmicPath%\
    set WmicPath=%WmicPath:\=\\%
    exit /b

REM Find all files in the specified directory from prior month, save to a temp file listing them all
        REM Use WMIC DATAFILE to look for files in the date range of the prior month (remove carraige return from WMIC output lines)
        for /f "skip=1 tokens=*" %%A in ('wmic datafile where "drive='%WmicDrive%' and path='%WmicPath%' and lastModified>='%PriorYYYYMMDD%' and lastModified<'%CurrYYYYMMDD%'" get name') do (
            set line=%%A
            set line=!line:~0,-1!
            if "!line!" NEQ "" echo.!line!
    exit /b

REM Using the list of files from prior month, zip into a single file with the year and month of the prior month in it's name
    "%ZipTool%" a -mx=9 "%back_up%\%PriorYYYYMMDD:~0," -i@"%TempList%" -ssw >NUL
    exit /b

REM Delete files from the list of files that were archived
    for /f "tokens=*" %%A in ('type "%TempList%"') do del "%%~A" >NUL
    exit /b

Open in new window


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
Jose TorresCertified Database AdministratorCommented:
Come to the Dark Side: POWERSHELL
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.