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

x
?
Solved

moving and copying files

Posted on 2003-03-20
2
Medium Priority
?
185 Views
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
0
Comment
Question by:JamesHolyoak
  • 2
2 Comments
 
LVL 7

Expert Comment

by:billious
ID: 8175301
Which Operating system?

...Bill
0
 
LVL 7

Accepted Solution

by:
billious earned 200 total points
ID: 8178463
JamesHolyoak:

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)

running

getdate

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)




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

@echo off
SET INDATE=%1
IF NOT "%INDATE%"=="" GOTO INPUT
for /f "tokens=2" %%i in ('date /t') do set thedate=%%i

:process
::
:: 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%

:SET30
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

:SETDAY
::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

:set29
set dd=29
goto done

:SET28
set dd=28

:DONE
:: 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
ECHO %YYYY%%MM%%DD%
GOTO EXIT

:INPUT
ECHO %1
set thedate=%1
goto process

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

:EXIT

---------------------------------------------------------



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

:ifold
::
:: 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


:process
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

:EXIT
 
---------------------------------------------------------

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

moveday

should move all files created before YESTERDAY

and

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

HTH

...Bill
0

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