简体   繁体   中英

batch script to zip IIS Log files(text files) and copy to another folder with daily scheduling

The task is to Zip IIS Log Files(text files) and copy to another folder based on yesterday's date. And this batch script has to be Scheduled everyday.

I am very new to batch scripting. Any help is appreciated..!! Thanks.

I have this code which is copying the latest 3 files. But, I want to copy files only created on yesterday's date. And my file naming format is a_bc130510(a_bcYYMMDD).

enter code here

@ECHO OFF
SET srcdir=D:\IIS LOGS
SET tgtdir=D:\FileCopy
SET /A topcnt=3
SET /A cnt=0
FOR /F "tokens=*" %%F IN ('DIR /A-D /OD /TW /B "%srcdir%"') DO (
SET /A cnt+=1
SETLOCAL EnableDelayedExpansion
IF !cnt! GTR !topcnt! (ENDLOCAL & GOTO :EOF)
ENDLOCAL
COPY "%srcdir%\%%F" "%tgtdir%"
)

This is modified to copy the newest file from yesterday, as you asked.

This assumes your log filenames are abcYYMMDD.log
Change the term "%srcdir%\\*%day%.log" if you need something else.

@echo off
call :getdate today -1
SET "srcdir=D:\IIS LOGS"
SET "tgtdir=D:\FileCopy"
FOR /F "delims=" %%F IN ('DIR /A-D /O-D /TW /B "%srcdir%\*%day%.log"') DO (
COPY "%srcdir%\%%F" "%tgtdir%"
goto :done
)
:done
rem extra code after the copy goes here
goto :EOF
:getdate
set date1=%1
set qty=%2
set separator=%~3
if /i "%date1%" EQU "TODAY" (set date1=now) else (set date1="%date1%")
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set "YY=%result:~2,4%"&set "MM=%result:~4,2%"&set "DD=%result:~6,2%"
set "day=%YY%%separator%%MM%%separator%%DD%"
goto :EOF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM