We help IT Professionals succeed at work.

Need a batch file to select a certain file name from a directory - the file name changes daily

TenaCoe
TenaCoe asked
on
386 Views
Last Modified: 2010-04-21
I have a program that generates pdf file through the day.  It generates files named BatchAudit.pdf, BatchAudit_0001.pdf, BatchAudit_0002.pdf, BatchAudit_0003.pdf and so on through the day.  I only care about the last one, maybe BatchAudit_0009.pdf for example.  It could be 0009, 0008 or any number.

My goal is to parse \\workstation\folder\*.* and move only the last BatchAudit_000x.pdf file to \\server\dump_folder\*.*

The folder on the workstation also changes, but I have addressed that already.
:: Copies Branch Capture BatchAudit* files to a remote location based on date
:: Only copies yesterdays BatchAudit files, useful for setting up automated copies
 
@echo off
 
setlocal
 
call :GETDATEPARTS "%date%"
call :SUBTRACTDAYS 1
 
set yesterday=D%yy%%mm%%dd%
 
::Deletes destination files, change  directories to match your needs
del "C:\testing\mvxwalton\*.*"
 
:: copies files to destination, change source and destination to match your needs
:: would be nice to only copy the last BranchAudit_000x.pdf file created, but for now it copies all BranchAudit files
COPY "K:\BCreports\mvxwalton\%yesterday%\BatchAudit*.pdf" "C:\testing\mvxwalton\"
 
echo Reports have been transmitted!
pause
 
goto :EOF
 
:GETDATEPARTS
 
set dt=%~1
set tok=1-3
 
if "%dt:~0,1%" GTR "9" set tok=2-4
 
set yyyy=
 
for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)
 
if not "%yyyy%"=="" set yy=%yyyy%
 
if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%
 
goto :EOF
 
:SUBTRACTDAYS
 
set dayCnt=%1
 
if "%dayCnt%"=="" set dayCnt=1
 
REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100
 
:CHKDAY
 
if /I %dd% GTR 0 goto DONESUBTRACT
 
set /A mm=%mm% - 1
 
if /I %mm% GTR 0 goto ADJUSTDAY
 
set /A mm=12
set /A yy=%yy% - 1
 
:ADJUSTDAY
 
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through
 
:SET31
 
set /A dd=31 + %dd%
 
goto CHKDAY
 
:SET30
 
set /A dd=30 + %dd%
 
goto CHKDAY
 
:LEAPCHK
 
set /A tt=%yy% %% 4
 
if not %tt%==0 goto SET28
 
set /A tt=%yy% %% 100
 
if not %tt%==0 goto SET29
 
set /A tt=%yy% %% 400
 
if %tt%==0 goto SET29
 
:SET28
 
set /A dd=28 + %dd%
 
goto CHKDAY
 
:SET29
 
set /A dd=29 + %dd%
 
goto CHKDAY
 
:DONESUBTRACT
 
if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%
 
goto :EOF

Open in new window

Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Holy scripts Batman.  Sorry, had a moment.  Looks like that will work great.  I added your script to mine.  I get what your doing, but not exactly sure what the "for /F tokens" line does.  I know what it does, it parses the files and get the largest number, but I'm not sure what the switches do exactly.  If you get a chance to elaborate great, if not that's o.k.

I have included my complete, updated, script - if you can make any improvements, feel free :)

Thanks a bunch Shift -3!!!

:: Copies Branch Capture BatchAudit* files to a remote location based on date
:: Only copies yesterdays BatchAudit files, useful for setting up automated copies
 
@echo off
 
setlocal
 
call :GETDATEPARTS "%date%"
call :SUBTRACTDAYS 1
 
set yesterday=D%yy%%mm%%dd%
 
setlocal enabledelayedexpansion
 
set folder=K:\BCreports\mvxwalton\%yesterday%
set dest=c:\testing\mvxwalton
 
for /F "tokens=*" %%G in ('dir "%folder%\BatchAudit*.pdf" /A:-D /B /O:N') do (
 set file=%%G
)
 
:: del "C:\testing\mvxwalton\*.*" 
 
echo move "%folder%\%file%" "%dest%"
 
pause
 
::Deletes destination files, change  directories to match your needs
 
 
:: copies files to destination, change source and destination to match your needs
:: would be nice to only copy the last BranchAudit_000x.pdf file created, but for now it copies all BranchAudit files
:: COPY "K:\BCreports\mvxwalton\%yesterday%\BatchAudit*.pdf" "C:\testing\mvxwalton\"
 
echo Reports have been transmitted!
pause
 
goto :EOF
 
:GETDATEPARTS
 
set dt=%~1
set tok=1-3
 
if "%dt:~0,1%" GTR "9" set tok=2-4
 
set yyyy=
 
for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)
 
if not "%yyyy%"=="" set yy=%yyyy%
 
if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%
 
goto :EOF
 
:SUBTRACTDAYS
 
set dayCnt=%1
 
if "%dayCnt%"=="" set dayCnt=1
 
REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100
 
:CHKDAY
 
if /I %dd% GTR 0 goto DONESUBTRACT
 
set /A mm=%mm% - 1
 
if /I %mm% GTR 0 goto ADJUSTDAY
 
set /A mm=12
set /A yy=%yy% - 1
 
:ADJUSTDAY
 
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through
 
:SET31
 
set /A dd=31 + %dd%
 
goto CHKDAY
 
:SET30
 
set /A dd=30 + %dd%
 
goto CHKDAY
 
:LEAPCHK
 
set /A tt=%yy% %% 4
 
if not %tt%==0 goto SET28
 
set /A tt=%yy% %% 100
 
if not %tt%==0 goto SET29
 
set /A tt=%yy% %% 400
 
if %tt%==0 goto SET29
 
:SET28
 
set /A dd=28 + %dd%
 
goto CHKDAY
 
:SET29
 
set /A dd=29 + %dd%
 
goto CHKDAY
 
:DONESUBTRACT
 
if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%
 
goto :EOF

Open in new window

Author

Commented:
Thank you very much for relieving my headache!

Commented:
Here are good references for DIR and FOR /F:
http://www.ss64.com/nt/dir.html
http://www.ss64.com/nt/for_cmd.html

In this case FOR /F loops through the output of the DIR command.  The "tokens=*" tells it to capture the entire line of output, not just one portion.

The important part of this is the /O:N switch on DIR.  It orders output by name, so the last file to be listed will be the one with the highest number.  This is the last value that the file variable gets set to, so that's what gets used by the move command.

Author

Commented:
Great!  thanks again.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.