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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

752 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