Need help to write a batch file.

January 17, 2012 at 06:15:30
Specs: Windows XP
Hi All,
Writing batch file for the first time. So help will be appreciated. I am trying to write a batch file that finds all the pdf files in a folder that were modified 5 minutes ago and copy then to other folder and rename them. Renaming is such that all the spaces in the name of pdf should be replaced by _.
Source: C:\ExcelAuto
Destination: C:\ExcelAuto\Temp

If there is a pdf named IND TEST.pdf in C:\ExcelAuto and its modified in last 5 mins then it should be copied to C:\ExcelAutomation\Temp with name IND_TEST.pdf.

This batch file will run every 5 mins and copy all those files that are modified in last 5 mins.


See More: Need help to write a batch file.

Report •

#1
January 17, 2012 at 08:48:28
Your post has me intrigued, so I am working a solution. This is what I have so far, but it's not working correctly yet:

@echo on
setlocal enabledelayedexpansion

:script_start
REM setting directory variables
set source=c:\ExcelAuto
set destination=c:\ExcelAuto\Temp
REM setting new date variable that doesn't have day of the week in it
set mydate=%date:~-10%

:timer
REM running only when minutes of the hour is an increment of 5
cls
echo Currently waiting for a 5 minute interval...
REM breaking time into individual variables
for /f "tokens=1,2,3,4 delims=:." %%a in ("%time%") do (
set myhour=%%a
set myminute=%%b
set mysecond=%%c
set mycenti=%%d
)
REM adding leading zero to hour values that are less than 10 to make it match dir output hour format
if %myhour% lss 10 set myhour=0%myhour%
REM figuring out meridian based on myhour variable
set meridian=AM
if %myhour% gtr 11 set meridian=PM
REM watching for 5 minute increments to base start of operations on
if %myminute% equ 05 goto start
if %myminute% equ 10 goto start
if %myminute% equ 15 goto start
if %myminute% equ 20 goto start
if %myminute% equ 25 goto start
if %myminute% equ 30 goto start
if %myminute% equ 35 goto start
if %myminute% equ 40 goto start
if %myminute% equ 45 goto start
if %myminute% equ 50 goto start
if %myminute% equ 55 goto start
if %myminute% equ 00 goto start_special
timeout 1
goto timer

:start
echo Regular 5 minute interval reached. Current time = %time%
REM getting list of files in directory and recording them in temp file
dir %source% > dir.tmp
REM using findstr to find files that were created today and recording them in temp file
findstr /B %mydate% dir.tmp > matches_date.tmp
REM using findstr to find files that were created in matching hour and recording them in temp file
findstr /B %myhour%: matches_date.tmp > matches_hour.tmp
REM using findstr to find files that were created in the matching meridian (AM or PM)
findstr %meridian% matches_hour.tmp > matches_meridian.tmp
REM using findstr to find files that were created in last 5 minutes and recording them in temp file
for /f %%a in (matches_meridian.tmp) do (
findstr :%myminute% matches_meridian.tmp > created_last_5.tmp
set /a myminute=%myminute% -1
findstr :%myminute% matches_meridian.tmp >> created_last_5.tmp
set /a myminute=%myminute% -1
findstr :%myminute% matches_meridian.tmp >> created_last_5.tmp
set /a myminute=%myminute% -1
findstr :%myminute% matches_meridian.tmp >> created_last_5.tmp
set /a myminute=%myminute% -1
findstr :%myminute% matches_meridian.tmp >> created_last_5.tmp
REM copying files created in last 5 minutes to destination
for /f "tokens=5 delims= " %%a in (created_last_5.tmp) do (
copy %source%\%%a %destination%
goto ending

:start_special
echo Special "00" minute interval reached. Current time = %time%
REM this section is for a myminute value of 00 which makes regular subtraction an invalid way to check for last 5 minutes
REM getting list of files in directory and recording them in temp file
dir %source% > dir.tmp
REM using findstr to find files that were created today and recording them in temp file
findstr /B %mydate% dir.tmp > matches_date.tmp
REM using findstr to find files that were created in matching hour and recording them in temp file
findstr /B %myhour%: matches_date.tmp > matches_hour.tmp
REM using findstr to find files that were created in the matching meridian (AM or PM)
findstr %meridian% matches_hour.tmp > matches_meridian.tmp
REM using findstr to find files that were created in last 5 minutes and recording them in temp file
for /f %%a in (matches_meridian.tmp) do (
findstr :%myminute% matches_meridian.tmp > created_last_5.tmp
set /a myminute=%myminute% +59
findstr :%myminute% matches_meridian.tmp >> created_last_5.tmp
set /a myminute=%myminute% +58
findstr :%myminute% matches_meridian.tmp >> created_last_5.tmp
set /a myminute=%myminute% +57
findstr :%myminute% matches_meridian.tmp >> created_last_5.tmp
set /a myminute=%myminute% +56
findstr :%myminute% matches_meridian.tmp >> created_last_5.tmp
REM copying files created in last 5 minutes to destination
for /f "tokens=5 delims= " %%a in (created_last_5.tmp) do (
copy %source%\%%a %destination%

:ending
REM quietly deleting temp files
del /q dir.tmp
del /q matches_date.tmp
del /q matches_hour.tmp
del /q matches_meridian.tmp
del /q created_last_5.tmp

goto script_start


Report •

#2
January 17, 2012 at 12:41:27
This is what I've done so far. The script works up to the point where I try to copy the file to the destination. Also, I still need to write the module that renames it once its copied. Maybe one of the batch script ninjas around here will spot this, critique my amateur code, and then we can all learn something new.

@echo on
setlocal enabledelayedexpansion

:script_start
REM cls
REM setting directory variables
set source=c:\ExcelAuto
set destination=c:\ExcelAuto\Temp
REM setting new date variable that doesn't have day of the week in it
set mydate=%date:~-10%
set testminute=%time:~4,1%
if %testminute% equ 5 timeout 59 && echo Waiting until next non 5 minute interval to start timer...
if %testminute% equ 0 timeout 59 && echo Waiting until next non 5 minute interval to start timer...
:timer
REM running only when minutes of the hour is an increment of 5
cls
echo Currently waiting for a 5 minute interval...
echo Current time is: %myhour%:%myminute%:%mysecond%:%mycenti% %meridian%
REM breaking time into individual variables
for /f "tokens=1,2,3,4 delims=:." %%a in ("%time%") do (
set myhour=%%a
set myminute=%%b
set mysecond=%%c
set mycenti=%%d
)
REM adding leading zero to hour values that are less than 10 to make it match dir output hour format
if %myhour% lss 10 set myhour=0%myhour%
REM figuring out meridian based on myhour variable
set meridian=AM
if %myhour% gtr 11 set meridian=PM
REM watching for 5 minute increments to base start of operations on
if %myminute% equ 05 goto start
if %myminute% equ 10 goto start
if %myminute% equ 15 goto start
if %myminute% equ 20 goto start
if %myminute% equ 25 goto start
if %myminute% equ 30 goto start
if %myminute% equ 35 goto start
if %myminute% equ 40 goto start
if %myminute% equ 45 goto start
if %myminute% equ 50 goto start
if %myminute% equ 55 goto start
if %myminute% equ 00 goto start_special
timeout 1
goto timer

:start
echo Regular 5 minute interval reached. Current time = %time%
REM getting list of files in directory and recording them in temp file
dir %source% > dir.tmp
REM using findstr to find files that were created today and recording them in temp file
findstr /B %mydate% dir.tmp > matches_date.tmp
REM using findstr to find files that were created in matching hour and recording them in temp file
findstr %myhour%: matches_date.tmp > matches_hour.tmp
REM using findstr to find files that were created in the matching meridian (AM or PM)
findstr %meridian% matches_hour.tmp > matches_meridian.tmp
REM using findstr to find files that were created in last 5 minutes and recording them in temp file
findstr :%myminute% matches_meridian.tmp > created_last_5.tmp
set /a myminute=%myminute% -1
findstr :%myminute% matches_meridian.tmp >> created_last_5.tmp
set /a myminute=%myminute% -1
findstr :%myminute% matches_meridian.tmp >> created_last_5.tmp
set /a myminute=%myminute% -1
findstr :%myminute% matches_meridian.tmp >> created_last_5.tmp
set /a myminute=%myminute% -1
findstr :%myminute% matches_meridian.tmp >> created_last_5.tmp
REM copying files created in last 5 minutes to destination
for /f "tokens=5 delims= skip=2" %%a in (created_last_5.tmp) do (
copy "%source%\%%a" %destination%
)
goto ending

:start_special
echo Special "00" minute interval reached. Current time = %time%
REM this section is for a myminute value of 00 which makes regular subtraction an invalid way to check for last 5 minutes
REM getting list of files in directory and recording them in temp file
dir %source% > dir.tmp
REM using findstr to find files that were created today and recording them in temp file
findstr /B %mydate% dir.tmp > matches_date.tmp
REM using findstr to find files that were created in matching hour and recording them in temp file
findstr %myhour%: matches_date.tmp > matches_hour.tmp
REM using findstr to find files that were created in the matching meridian (AM or PM)
findstr %meridian% matches_hour.tmp > matches_meridian.tmp
REM using findstr to find files that were created in last 5 minutes and recording them in temp file
findstr :%myminute% matches_meridian.tmp > created_last_5.tmp
set /a myminute=%myminute% +59
findstr :%myminute% matches_meridian.tmp >> created_last_5.tmp
set /a myminute=%myminute% +58
findstr :%myminute% matches_meridian.tmp >> created_last_5.tmp
set /a myminute=%myminute% +57
findstr :%myminute% matches_meridian.tmp >> created_last_5.tmp
set /a myminute=%myminute% +56
findstr :%myminute% matches_meridian.tmp >> created_last_5.tmp
REM copying files created in last 5 minutes to destination
for /f "tokens=5 delims= skip=2" %%a in (created_last_5.tmp) do (
copy "%source%\%%a" %destination%
)

:ending
REM quietly deleting temp files
del /q dir.tmp
del /q matches_date.tmp
del /q matches_hour.tmp
del /q matches_meridian.tmp
REM del /q created_last_5.tmp

goto script_start


Report •

#3
January 18, 2012 at 05:02:13
Hey jowah, thanks for your help. But dont know for some reason the code that you have given above is not working. : (
Can someone please help me with this issue as its very urgent?

Report •

Related Solutions

#4
January 18, 2012 at 08:21:29
It's not working because I still need to do some work on the part that parses out the filename from the tmp file and copies it.

Report •

#5
January 18, 2012 at 22:28:56
For some reason this has turned out to be quite the challenge, but its fun to figure out. I am getting very close now and I hope to post something for you in the next couple days.

Report •

#6
January 18, 2012 at 23:35:24
Maybe this batch script type it.

@ECHO OFF
SET SOURCE=C:\ExcelAuto
SET DESTINATION=C:\ExcelAuto\Temp

dir *.pdf | find /v /i "Dir"|find /v /i "File(s)" >> FILELIST.tmp

:: FIND CURRENT TIME AND IDENTIFY LAST 5 MINUTE
SETlocal enabledelayedexpansion
REM FOR /F "tokens=1,2,3* delims==" %%A IN ('reg query "HKCU\Control Panel\International" ^| find /i "sShortDate"') DO (SET FDATE=%%C)
SET CDATE=%DATE%
FOR /F "tokens=1,2,3* delims==: " %%A IN ('TIME /T') DO (
SET TH=%%A
SET TM=%%B
SET AMPM=%%C
)
ECHO Current Date Time=%CDATE%%TH%:%TM%
SET Z=0
SET X=1
SET THZ=%TH%
:LOOP
IF !X! EQU 6 GOTO :ENDLOOP
SET /a TH%X%=%THZ%
SET N=%TM%
SET /a N-=%X%
IF !N! LSS 0 GOTO :LST60
IF !N! GEQ 10 SET LS%X%M=%N%
IF !N! LSS 10 SET LS%X%M=0%N%
SET /a X+=1
GOTO :LOOP
:LST60
SET /a N+=60
IF !N! GEQ 10 SET LS%X%M=%N%
IF !N! LSS 10 SET LS%X%M=0%N%
IF !Z! EQU 0 SET /a THZ-=1
SET /a TH%X%=%THZ%
SET /a X+=1
IF !X! EQU 6 GOTO :ENDLOOP
IF !Z! EQU 1 GOTO :LOOP
SET /a Z+=1
GOTO :LOOP
:ENDLOOP

SET LS1=%CDATE% %TH1%:%LS1M% %AMPM%
SET LS2=%CDATE% %TH2%:%LS2M% %AMPM%
SET LS3=%CDATE% %TH3%:%LS3M% %AMPM%
SET LS4=%CDATE% %TH4%:%LS4M% %AMPM%
SET LS5=%CDATE% %TH5%:%LS5M% %AMPM%
@ECHO Last 1 Mins=%LS1%
@ECHO Last 2 Mins=%LS2%
@ECHO Last 3 Mins=%LS3%
@ECHO Last 4 Mins=%LS4%
@ECHO Last 5 Mins=%LS5%

FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type FILELIST.tmp ^| find /i "%LS1%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type FILELIST.tmp ^| find /i "%LS2%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type FILELIST.tmp ^| find /i "%LS3%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type FILELIST.tmp ^| find /i "%LS4%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type FILELIST.tmp ^| find /i "%LS5%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)

ENDLOCAL
DEL /Q filelist.tmp
Exit

----------------------------------------------
http://tgbatch.blogspot.com


Report •

#7
January 19, 2012 at 01:43:21
Hi oUMMo,
Thanks for your help. But when I ran the script, it is calculating the last 5 mins accurately but the script is not copying the files. I was thinking where the %%E value is calculated. This variable is used in the line where the actual copying of files is done. Can you figure out why the copying is done???

Report •

#8
January 19, 2012 at 02:13:40
Hi smita1989
I fixed the Source and temporary path, try it again.

@ECHO OFF
:: FIND CURRENT TIME AND IDENTIFY LAST 5 MINUTE
SETlocal enabledelayedexpansion
REM FOR /F "tokens=1,2,3* delims==" %%A IN ('reg query "HKCU\Control Panel\International" ^| find /i "sShortDate"') DO (SET FDATE=%%C)
SET CDATE=%DATE%
FOR /F "tokens=1,2,3* delims==: " %%A IN ('TIME /T') DO (
SET TH=%%A
SET TM=%%B
SET AMPM=%%C
)
ECHO Current Date Time=%CDATE%%TH%:%TM%
SET Z=0
SET X=1
SET THZ=%TH%
:LOOP
IF !X! EQU 6 GOTO :ENDLOOP
SET /a TH%X%=%THZ%
SET N=%TM%
SET /a N-=%X%
IF !N! LSS 0 GOTO :LST60
IF !N! GEQ 10 SET LS%X%M=%N%
IF !N! LSS 10 SET LS%X%M=0%N%
SET /a X+=1
GOTO :LOOP
:LST60
SET /a N+=60
IF !N! GEQ 10 SET LS%X%M=%N%
IF !N! LSS 10 SET LS%X%M=0%N%
IF !Z! EQU 0 SET /a THZ-=1
SET /a TH%X%=%THZ%
SET /a X+=1
IF !X! EQU 6 GOTO :ENDLOOP
IF !Z! EQU 1 GOTO :LOOP
SET /a Z+=1
GOTO :LOOP
:ENDLOOP

SET LS1=%CDATE% %TH1%:%LS1M% %AMPM%
SET LS2=%CDATE% %TH2%:%LS2M% %AMPM%
SET LS3=%CDATE% %TH3%:%LS3M% %AMPM%
SET LS4=%CDATE% %TH4%:%LS4M% %AMPM%
SET LS5=%CDATE% %TH5%:%LS5M% %AMPM%
@ECHO Last 1 Mins=%LS1%
@ECHO Last 2 Mins=%LS2%
@ECHO Last 3 Mins=%LS3%
@ECHO Last 4 Mins=%LS4%
@ECHO Last 5 Mins=%LS5%

SET SOURCE=C:\ExcelAuto
SET DESTINATION=C:\ExcelAuto\Temp
IF EXIST "%temp%\FILELIST.tmp" DEL /Q "%temp%\filelist.tmp"
dir %SOURCE%\*.pdf |find /v /i "Dir" |find /v /i "File(s)" >> "%temp%\FILELIST.tmp"

FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS1%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS2%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS3%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS4%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS5%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)

ENDLOCAL
DEL /Q "%temp%\filelist.tmp"
Exit


Report •

#9
January 19, 2012 at 05:21:47
Hi oMMUo,
I am getting the following error on command promt when I run the script:

Current Date Time=Thu 01/19/2012 08:13
Invalid Number. Numeric constants are either decimal <17>.
hexadecimal <0*11>, or octal <021>.
Invalid Number. Numeric constants are either decimal <17>.
hexadecimal <0*11>, or octal <021>.
Invalid Number. Numeric constants are either decimal <17>.
hexadecimal <0*11>, or octal <021>.
Invalid Number. Numeric constants are either decimal <17>.
hexadecimal <0*11>, or octal <021>.
Invalid Number. Numeric constants are either decimal <17>.
hexadecimal <0*11>, or octal <021>.
Last 1 mins=Thu 01/19/2012 :12 AM
Last 2 mins=Thu 01/19/2012 :11 AM
Last 3 mins=Thu 01/19/2012 :10 AM
Last 4 mins=Thu 01/19/2012 :09 AM
Last 5 mins=Thu 01/19/2012 :08 AM

And it is still not able to copy. :(


Report •

#10
January 19, 2012 at 06:56:59
Hi smita1989
I fixed mistake some wording, Try it again.

@ECHO OFF
:: FIND CURRENT TIME AND IDENTIFY LAST 5 MINUTE
SETlocal enabledelayedexpansion
REM FOR /F "tokens=1,2,3* delims==" %%A IN ('reg query "HKCU\Control Panel\International" ^| find /i "sShortDate"') DO (SET FDATE=%%C)
FOR /F "tokens=1,2,3* delims==: " %%A IN ('DATE /T') DO (SET CDATE=%%B)
FOR /F "tokens=1,2,3* delims==: " %%A IN ('TIME /T') DO (
SET TH=%%A
SET TM=%%B
SET AMPM=%%C
)
ECHO Current Date Time=%CDATE% %TH%:%TM% %AMPM%
SET Z=0
SET X=1
SET THZ=%TH%
:LOOP
IF !X! EQU 6 GOTO :ENDLOOP
SET TH%X%=%THZ%
SET N=%TM%
SET /a N-=%X%
IF !N! LSS 0 GOTO :LST60
IF !N! GEQ 10 SET LS%X%M=%N%
IF !N! LSS 10 SET LS%X%M=0%N%
SET /a X+=1
GOTO :LOOP
:LST60
SET /a N+=60
IF !N! GEQ 10 SET LS%X%M=%N%
IF !N! LSS 10 SET LS%X%M=0%N%
IF !Z! EQU 0 SET /a THZ-=1
SET TH%X%=%THZ%
SET /a X+=1
IF !X! EQU 6 GOTO :ENDLOOP
IF !Z! EQU 1 GOTO :LOOP
SET /a Z+=1
GOTO :LOOP
:ENDLOOP

SET LS1=%CDATE% %TH1%:%LS1M% %AMPM%
SET LS2=%CDATE% %TH2%:%LS2M% %AMPM%
SET LS3=%CDATE% %TH3%:%LS3M% %AMPM%
SET LS4=%CDATE% %TH4%:%LS4M% %AMPM%
SET LS5=%CDATE% %TH5%:%LS5M% %AMPM%
@ECHO Last 1 Mins=%LS1%
@ECHO Last 2 Mins=%LS2%
@ECHO Last 3 Mins=%LS3%
@ECHO Last 4 Mins=%LS4%
@ECHO Last 5 Mins=%LS5%

SET SOURCE=C:\ExcelAuto
SET DESTINATION=C:\ExcelAuto\Temp
IF EXIST "%temp%\FILELIST.tmp" DEL /Q "%temp%\FILELIST.tmp"
dir %SOURCE%\*.pdf |find /v /i "Dir" |find /v /i "File(s)" >> "%temp%\FILELIST.tmp"

FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS1%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS2%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS3%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS4%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS5%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)

ENDLOCAL
DEL /Q "%temp%\FILELIST.tmp"
Exit


Report •

#11
January 19, 2012 at 22:28:26
Hi oMMUo,
No errors shown when I run the updated script. Time is also calculated accurately but still copying is not done. Can you test it at your end once? Not sure what is going wrong and why the files are not copied in destination folder. I guess the source and destination should be stored in string variables.

Report •

#12
January 19, 2012 at 23:04:11
Hi smita1989
I mistake space in SET LSx lines, we need twice space
I have been test It work fine, try again smita1989 good luck.


@ECHO OFF
:: FIND CURRENT TIME AND IDENTIFY LAST 5 MINUTE
SETlocal enabledelayedexpansion
REM FOR /F "tokens=1,2,3* delims==" %%A IN ('reg query "HKCU\Control Panel\International" ^| find /i "sShortDate"') DO (SET FDATE=%%C)
FOR /F "tokens=1,2,3* delims==: " %%A IN ('DATE /T') DO (SET CDATE=%%B)
FOR /F "tokens=1,2,3* delims==: " %%A IN ('TIME /T') DO (
SET TH=%%A
SET TM=%%B
SET AMPM=%%C
)
ECHO Current Date Time=%CDATE% %TH%:%TM% %AMPM%
SET Z=0
SET X=1
SET THZ=%TH%
:LOOP
IF !X! EQU 6 GOTO :ENDLOOP
SET TH%X%=%THZ%
SET N=%TM%
SET /a N-=%X%
IF !N! LSS 0 GOTO :LST60
IF !N! GEQ 10 SET LS%X%M=%N%
IF !N! LSS 10 SET LS%X%M=0%N%
SET /a X+=1
GOTO :LOOP
:LST60
SET /a N+=60
IF !N! GEQ 10 SET LS%X%M=%N%
IF !N! LSS 10 SET LS%X%M=0%N%
IF !Z! EQU 0 SET /a THZ-=1
SET TH%X%=%THZ%
SET /a X+=1
IF !X! EQU 6 GOTO :ENDLOOP
IF !Z! EQU 1 GOTO :LOOP
SET /a Z+=1
GOTO :LOOP
:ENDLOOP

SET LS1=%CDATE% %TH1%:%LS1M% %AMPM%
SET LS2=%CDATE% %TH2%:%LS2M% %AMPM%
SET LS3=%CDATE% %TH3%:%LS3M% %AMPM%
SET LS4=%CDATE% %TH4%:%LS4M% %AMPM%
SET LS5=%CDATE% %TH5%:%LS5M% %AMPM%


SET SOURCE=C:\ExcelAuto
SET DESTINATION=C:\ExcelAuto\Temp
IF EXIST "%temp%\FILELIST.tmp" DEL /Q "%temp%\FILELIST.tmp"
dir %SOURCE%\*.pdf |find /v /i "Dir" |find /v /i "File(s)" >> "%temp%\FILELIST.tmp"

FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS1%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
@ECHO Last 1 Mins=%LS1%
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS2%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
@ECHO Last 2 Mins=%LS2%
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS3%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
@ECHO Last 3 Mins=%LS3%
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS4%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
@ECHO Last 4 Mins=%LS4%
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS5%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
@ECHO Last 5 Mins=%LS5%

ENDLOCAL
DEL /Q "%temp%\FILELIST.tmp"



Report •

#13
January 20, 2012 at 02:20:59
Hey oMMUo,
Sorry to bother you again and again but on my machine, the script is still not copying the files. I wanted to know whether this script is copying only those files that are modified exactly 5 mins ago. Coz I want to copy all the files that are modified or created within last 5 mins from the minute the script is run. I also changed the properties of source and destination folders by unchecking read-only option. But still unsuccessfull. :( :(

Report •

#14
January 20, 2012 at 04:56:53
You Mean all file right *.*, Before Script is effective only *.pdf
OK, I Fix it again for All Extension file *.* Try again smita1989, good luck.

@ECHO OFF
:: FIND CURRENT TIME AND IDENTIFY LAST 5 MINUTE
SETlocal enabledelayedexpansion
REM FOR /F "tokens=1,2,3* delims==" %%A IN ('reg query "HKCU\Control Panel\International" ^| find /i "sShortDate"') DO (SET FDATE=%%C)
FOR /F "tokens=1,2,3* delims==: " %%A IN ('DATE /T') DO (SET CDATE=%%B)
FOR /F "tokens=1,2,3* delims==: " %%A IN ('TIME /T') DO (
SET TH=%%A
SET TM=%%B
SET AMPM=%%C
)
ECHO Current Date Time=%CDATE% %TH%:%TM% %AMPM%
SET Z=0
SET X=1
SET THZ=%TH%
:LOOP
IF !X! EQU 6 GOTO :ENDLOOP
SET TH%X%=%THZ%
SET N=%TM%
SET /a N-=%X%
IF !N! LSS 0 GOTO :LST60
IF !N! GEQ 10 SET LS%X%M=%N%
IF !N! LSS 10 SET LS%X%M=0%N%
SET /a X+=1
GOTO :LOOP
:LST60
SET /a N+=60
IF !N! GEQ 10 SET LS%X%M=%N%
IF !N! LSS 10 SET LS%X%M=0%N%
IF !Z! EQU 0 SET /a THZ-=1
SET TH%X%=%THZ%
SET /a X+=1
IF !X! EQU 6 GOTO :ENDLOOP
IF !Z! EQU 1 GOTO :LOOP
SET /a Z+=1
GOTO :LOOP
:ENDLOOP

SET LS1=%CDATE% %TH1%:%LS1M% %AMPM%
SET LS2=%CDATE% %TH2%:%LS2M% %AMPM%
SET LS3=%CDATE% %TH3%:%LS3M% %AMPM%
SET LS4=%CDATE% %TH4%:%LS4M% %AMPM%
SET LS5=%CDATE% %TH5%:%LS5M% %AMPM%


SET SOURCE=C:\ExcelAuto
SET DESTINATION=C:\ExcelAuto\Temp
IF EXIST "%temp%\FILELIST.tmp" DEL /Q "%temp%\FILELIST.tmp"
dir %SOURCE%\*.* |find /v /i "Dir" |find /v /i "File(s)" >> "%temp%\FILELIST.tmp"

FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS1%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
@ECHO Last 1 Mins=%LS1%
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS2%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
@ECHO Last 2 Mins=%LS2%
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS3%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
@ECHO Last 3 Mins=%LS3%
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS4%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
@ECHO Last 4 Mins=%LS4%
FOR /F "tokens=1,2,3,4,5* delims== " %%A IN ('type "%temp%\FILELIST.tmp" ^| find /i "%LS5%"') DO (COPY %SOURCE%\%%E %DESTINATION%\IND_%%E)
@ECHO Last 5 Mins=%LS5%

ENDLOCAL
DEL /Q "%temp%\FILELIST.tmp"
EXIT



Report •

#15
January 20, 2012 at 05:20:46
no no no. I meant all the pdf files only. And I tried *.* option also but dnt know whats happening. Can you tell me from where the %%E value is coming in the COPY command? I guess the problem lies there.

Report •

#16
January 20, 2012 at 06:18:18
OK, I think we have differential environment, Now Can you show Me the Result after Run.

:: result is in %temp%\FILELIST.tmp

dir %SOURCE%\*.* |find /v /i "Dir" |find /v /i "File(s)" >> "%temp%\FILELIST.tmp"

and

:: result of last 1 Minute

@ECHO Last 1 Mins=%LS1%


; the %%E value is files name for my result.




Report •

#17
January 20, 2012 at 13:55:55
Hi Smita1989,

I finished the one I was working on and, though it isn't very pretty, it seems to be working.

@echo off
:script_start
setlocal enabledelayedexpansion
cls
REM setting directory variables
set source=c:\ExcelAuto
set destination=c:\ExcelAuto\Temp
REM setting new date variable that doesn't have day of the week in it
set mydate=%date:~-10%

:testminutes
set testminute=%time:~4,1%
cls
echo Waiting until next non 5 minute interval to start timer...
timeout 1
if %testminute% equ 5 goto testminutes
if %testminute% equ 0 goto testminutes

:timer
REM running only when minutes of the hour is an increment of 5
REM did this to prevent running multiple times since runtime is less than one minute
cls
echo Currently waiting for next 5 minute interval...
echo Current time is: %myhour%:%myminute%:%mysecond%:%mycenti% %meridian%
REM breaking time into individual variables
for /f "tokens=1,2,3,4 delims=:." %%a in ("%time%") do (
set myhour=%%a
set myminute=%%b
set mysecond=%%c
set mycenti=%%d
)
REM removing leading space from AM hour
set myhour=%myhour: =%
REM figuring out meridian based on myhour variable
set meridian=AM
if %myhour% gtr 11 set meridian=PM
if %meridian% equ PM goto PM
:AM
REM adding leading zero to hour values that are less than 10 to make it match dir output hour format
if %myhour% lss 10 set myhour=0%myhour%
:PM
REM converting hour values that are greater than 10 to 12 hour format so they will match dir output time stamps
if %myhour% equ 13 set myhour=01
if %myhour% equ 14 set myhour=02
if %myhour% equ 15 set myhour=03
if %myhour% equ 16 set myhour=04
if %myhour% equ 17 set myhour=05
if %myhour% equ 18 set myhour=06
if %myhour% equ 19 set myhour=07
if %myhour% equ 20 set myhour=08
if %myhour% equ 21 set myhour=09
if %myhour% equ 22 set myhour=10
if %myhour% equ 23 set myhour=11
if %myhour% equ 24 set myhour=12

REM watching for 5 minute increments to base start of operations on
if %myminute% equ 05 goto start
if %myminute% equ 10 goto start
if %myminute% equ 15 goto start
if %myminute% equ 20 goto start
if %myminute% equ 25 goto start
if %myminute% equ 30 goto start
if %myminute% equ 35 goto start
if %myminute% equ 40 goto start
if %myminute% equ 45 goto start
if %myminute% equ 50 goto start
if %myminute% equ 55 goto start
if %myminute% equ 00 goto start_special
timeout 1
goto timer
REM Script by Jowah for smita1989 1-20-2012
:start
echo Regular 5 minute interval reached. Current time = %time%
REM getting list of files in directory and recording them in temp file
dir %source% > 1dir.tmp
REM using findstr to find files that were created today and recording them in temp file
findstr /B %mydate% 1dir.tmp > 2matches_date.tmp
REM using findstr to find files that were created in matching hour and recording them in temp file
findstr %myhour%: 2matches_date.tmp > 3matches_hour.tmp
REM using findstr to find files that were created in the matching meridian (AM or PM)
findstr %meridian% 3matches_hour.tmp > 4matches_meridian.tmp
REM using findstr to find files that were created in last 5 minutes and recording them in temp file
findstr :%myminute% 4matches_meridian.tmp > 5created_last_5.tmp
set /a myminute=%myminute% -1
findstr :%myminute% 4matches_meridian.tmp >> 5created_last_5.tmp
set /a myminute=%myminute% -1
findstr :%myminute% 4matches_meridian.tmp >> 5created_last_5.tmp
set /a myminute=%myminute% -1
findstr :%myminute% 4matches_meridian.tmp >> 5created_last_5.tmp
set /a myminute=%myminute% -1
findstr :%myminute% 4matches_meridian.tmp >> 5created_last_5.tmp
REM cleaning 5created_last_5.tmp leaving only filenames
for /f "skip=2 tokens=*" %%a in (5created_last_5.tmp) do (
set tmpvar=%%a
set tmpvar=!tmpvar:~39!
echo !tmpvar! >> 6created_last_5_clean.tmp
)
REM copying files created in last 5 minutes to destination
for /f "tokens=*" %%a in (6created_last_5_clean.tmp) do (
copy "%source%\%%a" "%destination%"
)
REM replacing spaces with underscores
dir /b "%destination%" > 7tmp.tmp
findstr /V _ 7tmp.tmp > with_spaces.tmp
for /f "tokens=*" %%a in (with_spaces.tmp) do (
set var=%%a
ren "%destination%\!var!" "!var: =_!"
)
goto ending

:start_special
echo Special "00" minute interval reached. Current time = %time%
REM this section is for a myminute value of 00 which makes regular subtraction an invalid way to check for last 5 minutes
REM getting list of files in directory and recording them in temp file
dir %source% > 1dir.tmp
REM using findstr to find files that were created today and recording them in temp file
findstr /B %mydate% 1dir.tmp > 2matches_date.tmp
REM using findstr to find files that were created in matching hour and recording them in temp file
findstr %myhour%: 2matches_date.tmp > 3matches_hour.tmp
REM using findstr to find files that were created in the matching meridian (AM or PM)
findstr %meridian% 3matches_hour.tmp > 4matches_meridian.tmp
REM using findstr to find files that were created in last 5 minutes and recording them in temp file
findstr :%myminute% 4matches_meridian.tmp > 5created_last_5.tmp
set /a myminute=59
findstr :%myminute% 4matches_meridian.tmp >> 5created_last_5.tmp
set /a myminute=58
findstr :%myminute% 4matches_meridian.tmp >> 5created_last_5.tmp
set /a myminute=57
findstr :%myminute% 4matches_meridian.tmp >> 5created_last_5.tmp
set /a myminute=56
findstr :%myminute% 4matches_meridian.tmp >> 5created_last_5.tmp
REM cleaning 5created_last_5.tmp leaving only filenames
for /f "skip=2 tokens=*" %%a in (5created_last_5.tmp) do (
set tmpvar=%%a
set tmpvar=!tmpvar:~39!
echo !tmpvar! >> 6created_last_5_clean.tmp
)
REM copying files created in last 5 minutes to destination
for /f "tokens=*" %%a in (6created_last_5_clean.tmp) do (
copy "%source%\%%a" "%destination%"
)
REM replacing spaces with underscores
dir /b "%destination%" > 7tmp.tmp
findstr /V _ 7tmp.tmp > with_spaces.tmp
for /f "tokens=*" %%a in (with_spaces.tmp) do (
set var=%%a
ren "%destination%\!var!" "!var: =_!"
)

:ending
REM quietly deleting temp files
del /q 1dir.tmp
del /q 2matches_date.tmp
del /q 3matches_hour.tmp
del /q 4matches_meridian.tmp
del /q 5created_last_5.tmp
del /q 6created_last_5_clean.tmp
del /q 7tmp.tmp
del /q with_spaces.tmp
endlocal

goto script_start


Report •

#18
January 23, 2012 at 02:50:42
Hi oMMUo,
I tried ur code and got the following output:

C:\Documents and Settings\irsbatch\Desktop>dir \*.* | find /v /i "Dir" | fin
d /v /i "File(s)" 1>>"C:\DOCUME~1\irsbatch\LOCALS~1\Temp\1\FILELIST.tmp"

C:\Documents and Settings\irsbatch\Desktop>and
Last 1 Mins=

C:\Documents and Settings\irsbatch\Desktop>pause
Press any key to continue . . .


Report •

#19
January 23, 2012 at 07:20:22
smita1989... Did you try mine? Here it is again:

@echo off
:script_start
setlocal enabledelayedexpansion
cls
REM setting directory variables
set source=c:\ExcelAuto
set destination=c:\ExcelAuto\Temp
REM setting new date variable that doesn't have day of the week in it
set mydate=%date:~-10%
:testminutes
set testminute=%time:~4,1%
cls
echo Waiting until next non 5 minute interval to start timer...
timeout 1
if %testminute% equ 5 goto testminutes
if %testminute% equ 0 goto testminutes
:timer
REM running only when minutes of the hour is an increment of 5
REM did this to prevent running multiple times since runtime is less than one minute
cls
echo Currently waiting for next 5 minute interval...
echo Current time is: %myhour%:%myminute%:%mysecond%:%mycenti% %meridian%
REM breaking time into individual variables
for /f "tokens=1,2,3,4 delims=:." %%a in ("%time%") do (
set myhour=%%a
set myminute=%%b
set mysecond=%%c
set mycenti=%%d
)
REM removing leading space from AM hour
set myhour=%myhour: =%
REM figuring out meridian based on myhour variable
set meridian=AM
if %myhour% gtr 11 set meridian=PM
if %meridian% equ PM goto PM
:AM
REM adding leading zero to hour values that are less than 10 to make it match dir output hour format
if %myhour% lss 10 set myhour=0%myhour%
:PM
REM converting hour values that are greater than 10 to 12 hour format so they will match dir output time stamps
if %myhour% equ 13 set myhour=01
if %myhour% equ 14 set myhour=02
if %myhour% equ 15 set myhour=03
if %myhour% equ 16 set myhour=04
if %myhour% equ 17 set myhour=05
if %myhour% equ 18 set myhour=06
if %myhour% equ 19 set myhour=07
if %myhour% equ 20 set myhour=08
if %myhour% equ 21 set myhour=09
if %myhour% equ 22 set myhour=10
if %myhour% equ 23 set myhour=11
if %myhour% equ 24 set myhour=12

REM watching for 5 minute increments to base start of operations on
if %myminute% equ 05 goto start
if %myminute% equ 10 goto start
if %myminute% equ 15 goto start
if %myminute% equ 20 goto start
if %myminute% equ 25 goto start
if %myminute% equ 30 goto start
if %myminute% equ 35 goto start
if %myminute% equ 40 goto start
if %myminute% equ 45 goto start
if %myminute% equ 50 goto start
if %myminute% equ 55 goto start
if %myminute% equ 00 goto start_special
timeout 1
goto timer
REM Script by Jowah for smita1989 1-20-2012
:start
echo Regular 5 minute interval reached. Current time = %time%
REM getting list of files in directory and recording them in temp file
dir %source% > 1dir.tmp
REM using findstr to find files that were created today and recording them in temp file
findstr /B %mydate% 1dir.tmp > 2matches_date.tmp
REM using findstr to find files that were created in matching hour and recording them in temp file
findstr %myhour%: 2matches_date.tmp > 3matches_hour.tmp
REM using findstr to find files that were created in the matching meridian (AM or PM)
findstr %meridian% 3matches_hour.tmp > 4matches_meridian.tmp
REM using findstr to find files that were created in last 5 minutes and recording them in temp file
findstr :%myminute% 4matches_meridian.tmp > 5created_last_5.tmp
set /a myminute=%myminute% -1
findstr :%myminute% 4matches_meridian.tmp >> 5created_last_5.tmp
set /a myminute=%myminute% -1
findstr :%myminute% 4matches_meridian.tmp >> 5created_last_5.tmp
set /a myminute=%myminute% -1
findstr :%myminute% 4matches_meridian.tmp >> 5created_last_5.tmp
set /a myminute=%myminute% -1
findstr :%myminute% 4matches_meridian.tmp >> 5created_last_5.tmp
REM cleaning 5created_last_5.tmp leaving only filenames
for /f "skip=2 tokens=*" %%a in (5created_last_5.tmp) do (
set tmpvar=%%a
set tmpvar=!tmpvar:~39!
echo !tmpvar! >> 6created_last_5_clean.tmp
)
REM copying files created in last 5 minutes to destination
for /f "tokens=*" %%a in (6created_last_5_clean.tmp) do (
copy "%source%\%%a" "%destination%"
)
REM replacing spaces with underscores
dir /b "%destination%" > 7tmp.tmp
findstr /V _ 7tmp.tmp > with_spaces.tmp
for /f "tokens=*" %%a in (with_spaces.tmp) do (
set var=%%a
ren "%destination%\!var!" "!var: =_!"
)
goto ending
:start_special
echo Special "00" minute interval reached. Current time = %time%
REM this section is for a myminute value of 00 which makes regular subtraction an invalid way to check for last 5 minutes
REM getting list of files in directory and recording them in temp file
dir %source% > 1dir.tmp
REM using findstr to find files that were created today and recording them in temp file
findstr /B %mydate% 1dir.tmp > 2matches_date.tmp
REM using findstr to find files that were created in matching hour and recording them in temp file
findstr %myhour%: 2matches_date.tmp > 3matches_hour.tmp
REM using findstr to find files that were created in the matching meridian (AM or PM)
findstr %meridian% 3matches_hour.tmp > 4matches_meridian.tmp
REM using findstr to find files that were created in last 5 minutes and recording them in temp file
findstr :%myminute% 4matches_meridian.tmp > 5created_last_5.tmp
set /a myminute=59
findstr :%myminute% 4matches_meridian.tmp >> 5created_last_5.tmp
set /a myminute=58
findstr :%myminute% 4matches_meridian.tmp >> 5created_last_5.tmp
set /a myminute=57
findstr :%myminute% 4matches_meridian.tmp >> 5created_last_5.tmp
set /a myminute=56
findstr :%myminute% 4matches_meridian.tmp >> 5created_last_5.tmp
REM cleaning 5created_last_5.tmp leaving only filenames
for /f "skip=2 tokens=*" %%a in (5created_last_5.tmp) do (
set tmpvar=%%a
set tmpvar=!tmpvar:~39!
echo !tmpvar! >> 6created_last_5_clean.tmp
)
REM copying files created in last 5 minutes to destination
for /f "tokens=*" %%a in (6created_last_5_clean.tmp) do (
copy "%source%\%%a" "%destination%"
)
REM replacing spaces with underscores
dir /b "%destination%" > 7tmp.tmp
findstr /V _ 7tmp.tmp > with_spaces.tmp
for /f "tokens=*" %%a in (with_spaces.tmp) do (
set var=%%a
ren "%destination%\!var!" "!var: =_!"
)

:ending
REM quietly deleting temp files
del /q 1dir.tmp
del /q 2matches_date.tmp
del /q 3matches_hour.tmp
del /q 4matches_meridian.tmp
del /q 5created_last_5.tmp
del /q 6created_last_5_clean.tmp
del /q 7tmp.tmp
del /q with_spaces.tmp
endlocal
goto script_start


Report •

#20
January 24, 2012 at 01:55:05
Hi jowah,
I trie your script. First I got a command promt which showed the following message:

Currently waiting for next 5 minute interval . . .
Current time is: 04:41:10:14 AM
Waiting for 1/0 seconds. Press any key to continue . . .

Then after this one I get the second command prompt saying :

Waiting until next non 5 minute interval to start timer . . .
Waiting for 1 seconds. Press any key to continue . . .

And this second command promt goes on. When I prees the key its not closing. So I closed it manually. But it did not copy the files.


Report •

#21
January 24, 2012 at 01:57:19
hey guyz,
I was thinking can we get the current date and time as well as the timestamp of a file in epoch time. Then v'll be able to do numeric opertaions on both of these times. we can subtract these times to see if the difference is 5 minutes. If yes then copy the file. Any suggestions on this?

Report •

#22
January 24, 2012 at 08:50:40
smita1989

Those messages are intentional...

Currently waiting for next 5 minute interval . . .
Current time is: 04:41:10:14 AM
Waiting for 1/0 seconds. Press any key to continue . . .
Then after this one I get the second command prompt saying :
Waiting until next non 5 minute interval to start timer . . .
Waiting for 1 seconds. Press any key to continue . . .

It's doing exactly what you wanted, every 5 minutes. Just let it run.


Report •

#23
January 25, 2012 at 03:49:32
Hi jowah,
Ur batch file is working well. Yipieeee......... And it is copying the files also. Thanks a lot. Can you tell me that can I schedule this batch file in Autosys Tool. Coz all our bat files are beign scheduled like that. And then they run at specific times by the autosys. If that is possible then what changes I have to make in above code?? Also I have to rename the copied files.

Report •

#24
January 25, 2012 at 08:33:17
I am not familiar with autosys tool. This script makes its own schedule and runs every 5 minutes. If you want it to end when it is finished, remove the goto script_start command at the bottom. Also, I just saw your question about epoch time. I've only ever used epoch in a Unix environment; I'm not aware of a way to use it in windows. What do you mean when you say you have to rename the copied files?

Report •

#25
January 30, 2012 at 01:21:16
Hi jowah,
Renaming copied files means when a particular pdf is copied It name should be changed. Spaces are to replaced by _.
For Eg: If IND Automation Test.pdf is copied then after copying it should be named as IND_Automation_Test.pdf.

Report •

#26
January 30, 2012 at 06:59:33
The script should be automatically renaming to replace spaces with underscores. This section does that:

REM replacing spaces with underscores
dir /b "%destination%" > 7tmp.tmp
findstr /V _ 7tmp.tmp > with_spaces.tmp
for /f "tokens=*" %%a in (with_spaces.tmp) do (
set var=%%a
ren "%destination%\!var!" "!var: =_!"
)


Report •

Ask Question