Batch file range

Thank you for your help! I have a question about a package for you guys. Therefore, we keep up with one of our client backups. They are located 200 miles from us. We carry out backups via remote desktop. We found that using the command line copy function is MUCH faster than any other copy / paste method. The person in charge of attracting them often forgets about it. I just started using batches, but created a small batch program that will ask the user which file to migrate, so he doesn't need to "copy n: \ backups \ blah \ tsclient \ h \ backups \ blah" which can be quite affected mistakes. Below is the batch file:

@echo off
title Copy Zipbacks

:loopagain
set /p date=Enter the date that needs to be copied over (yyyymmdd format):
copy h:\zipbackups\daily%date%.zipx \\tsclient\h\benton_off_site_backup\zipbackups

set /p again=Copy Another Daily Zip file? (Y/N):

IF "%again%"=="Y" GOTO loopAgain
IF "%again%"=="N" GOTO goAway

:goAway
exit

      

It's good if there are only a few backups left. My question is this; Is there a way to implement a range of backups? The file is configured as follows:

dailyYYYYMMDD.zipx
i.e. daily20140917.zipx

      

I have no problem asking for a date range, but getting a byte through that folder and only getting those that match the criteria is where I ran into the problem. Any thoughts?

+1


source to share


2 answers


You can use for

to list files:



@echo off
title Copy Zipbacks

:loopagain
set /p start_date=Enter start date that needs to be copied over (yyyymmdd format):
set /p end_date=Enter end date (yyyymmdd format) or nothing to match only start date:
if not defined end_date set end_date=%start_date%
for %%f in (h:\zipbackups\daily*.zipx) do if "%%~nf" geq "daily%start_date%" if "%%~nf" leq "daily%end_date%" copy %%f \\tsclient\h\benton_off_site_backup\zipbackups

set /p again=Copy Another Daily Zip file? (Y/N):

IF "%again%"=="Y" GOTO loopAgain
IF "%again%"=="N" GOTO goAway

:goAway
exit

      

+2


source


I came across a trick with xcopy that allows you to generate a sequence of valid dates. The following script will generate a sequence and then copy each of the files.

@echo off

::set /p date=Enter the start date that needs to be copied over (yyyymmdd format):
::set /p end=Enter the end date that needs to be copied over (yyyymmdd format):
set /a date=20010218
set /a end=20010302

set /a y=%date:~0,4%
set /a m=%date:~4,2%
set /a d=%date:~6,2%
echo INPUT = %y% %m% %d%

:getnextvaliddate
   set /a d+=1

   if %d% gtr 31 (
      set d=1
      set /a m+=1

      if %m% gtr 12 (
         set m=1
         set /a y+=1
      )
   )

echo %y% %m% %d%

xcopy /d:%m%-%d%-%y% /h /l "%~f0" "%~f0\" >nul 2>&1 || goto getnextvaliddate

call :prettydate

if %y% equ %end:~0,4% (
  if %m% equ %end:~4,2% (
    if %d% equ %end:~6,2% (
      goto :EOF
    )
  )
)

goto :getnextvaliddate

:prettydate
if %d% lss 10 (
  set dd=0%d%
) else (
  set dd=%d%
)
if %m% lss 10 (
  set mm=0%m%
) else (
  set mm=%m%
)
echo copy h:\zipbackups\daily%y%%mm%%dd%.zipx \\tsclient\h\benton_off_site_backup\zipbackups
goto :EOF

      



Output on my machine:

INPUT = 2001 2 26
2001 2 27
copy h:\zipbackups\daily20010227.zipx \\tsclient\h\benton_off_site_backup\zipbackups
2001 2 28
copy h:\zipbackups\daily20010228.zipx \\tsclient\h\benton_off_site_backup\zipbackups
2001 2 29
2001 2 30
2001 2 31
2001 3 1
copy h:\zipbackups\daily20010301.zipx \\tsclient\h\benton_off_site_backup\zipbackups
2001 3 2
copy h:\zipbackups\daily20010302.zipx \\tsclient\h\benton_off_site_backup\zipbackups

      

+1


source







All Articles