Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


moving and copying files

Posted on 2003-03-20
Medium Priority
Last Modified: 2010-04-17
I have been assigned the task of creating a BAT file to go onto the server and retrieve app log files from one folder and move them into another folder on a different server. I have to create a program that will do this every day and retrieve logs that are older than one day. I have looked into the target software from MCAFEE but would rather not use outside software or exe
Question by:JamesHolyoak
  • 2

Expert Comment

ID: 8175301
Which Operating system?


Accepted Solution

billious earned 200 total points
ID: 8178463

This is a solution for NT/2K/XP.

There are two scripts involved. The first is "getdate.bat" (not a great name, but that's what it was named by the person who asked http://www.experts-exchange.com/Operating_Systems/MSDOS/Q_20554441.html)



will set up environment variables as described to (today - 1)

and running

getdate mm/dd/yyyy

will set those environment variables to the supplied mm/dd/yyyy - 1.

Since getting the date (today - 1) is a FAQ, I intend to use this as a FMR (Frequently Made Reply)

:: set dd, mm, yyyy to today - 1 or supplied-date - 1
:: dd, mm in leading-zero-filled format

@echo off
for /f "tokens=2" %%i in ('date /t') do set thedate=%%i

:: for dd/mm/yy format, switch mm and dd around in
:: the following two lines
set /a mm=%thedate:~0,1% * 10 + %thedate:~1,1%
set /a dd=%thedate:~3,1% * 10 + %thedate:~4,1%
set /a yyyy=%thedate:~6,4%

set /A dd=%dd% - 1
if %dd% GTR 0 goto DONE
:: else last day of month - assume 31
set dd=31
set /A mm=%mm% - 1
if %mm% GTR 0 goto SETDAY
:: Must be Dec. 31st
set /A mm=12
set /A yyyy=%yyyy% - 1
goto DONE

::Problem months are Feb, Apr,Jun,Sep and Nov
if %mm%==4 goto SET30
if %mm%==6 goto SET30
if %mm%==9 goto SET30
if %mm%==11 goto SET30
if not %mm%==2 goto DONE
:: February - default to 28 days (exception for leap years)
set /A dd=%yyyy% %% 4
if not %dd%==0 goto set28
set /A dd=%yyyy% %% 100
if not %dd%==0 goto set29
set /A dd=%yyyy% %% 400
if not %dd%==0 goto set28

set dd=29
goto done

set dd=28

:: now convert back to 2-digit dd and mm
set thedate=
for %%i in (1 2 3 4 5 6 7 8 9) do call :lz %%i

set thedate=%1
goto process

:: prefix %dd%, %mm% with leading zero
if %dd%==%1 set dd=0%dd%
if %mm%==%1 set mm=0%mm%
goto :eof



The second batch job NEARLY does what you want.

@echo off
call getdate %1
set yesterday=%yyyy:~2,2%%mm%%dd%
for /f %%i in ('dir /b /a:-d') do call :ifold "%%i"
for %%i in (yesterday thedate yymmdd) do set %%i=
goto :eof

:: for "-" as a date separator, change '^|find "/"'
:: below to '^|find "-" ^|find ":"'
for /f %%i in ('dir %1 ^|find "/"') do call :process %%i %1
goto :eof

set thedate=%1
:: for dd/mm/yy format, switch "~3" and "~0" around in
:: the following line
set yymmdd=%thedate:~6,2%%thedate:~3,2%%thedate:~0,2%
:: To actually MOVE the files, change 'echo move %2'
:: below to 'move %2 X:'  
:: (without the quotes) where X: is your destination
:: drive/directory
if /i %yymmdd% LSS %yesterday% echo move %2
goto :eof


I say "nearly" because I have deliberately set it up to merely ECHO the names of the files that would be moved, so that you can verify that it works according to your requirements.

To activate the move, you would need to change the change the line below the obvious comment in the ":process" subroutine

Note that the second batch CALLS the first, so the first batch (which does the "find yesterday" calculation) must be in the "path" (best : same directory as the second)

If you save the second batch as "moveday.bat" then


should move all files created before YESTERDAY


moveday mm/dd/yyyy

should move all files created before (mm/dd/yyyy - 1)

One caveat: Both batches are sensitive to date format, and are set up for mm/dd/yyyy format.

If you use dd/mm/yyyy format, then you need to make the changes indicated in the comments

If you use "-" as a date separator, then you need to make the indicated change to the second batch



Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question