Computing.Net > Forums > Programming > Batch File Help

Batch File Help

Reply to Message Icon

Original Message
Name: jwendel
Date: January 8, 2004 at 13:58:38 Pacific
Subject: Batch File Help
OS: XP
CPU/Ram: P3 1066mhz
Comment:

I have dated folders on local machines and I need some type of program or batch file to automatically pull those files to the server each morning. They have specific dates (20040108) and I need that same folder backep up on my server.


Report Offensive Message For Removal


Response Number 1
Name: Dr. Nick
Date: January 9, 2004 at 00:08:03 Pacific
Subject: Batch File Help
Reply: (edit)

How about this?
==========================================================================

@ECHO OFF
ECHO.
ECHO Copying today's folder...
FOR /F "tokens=2-4 DELIMS=/ " %%F IN ('date /T') DO (set v_date=%%H%%F%%G)
MD C:\ARCHIVE\%v_date%
ECHO.
ECHO Copying '%v_date%'...
COPY /V /Z "C:\DATA\%v_date%" "C:\ARCHIVE\%v_date%"
ECHO.
ECHO Finished copying.
ECHO.
PAUSE
ECHO.

==========================================================================

This script will copy a folder named "YYYYMMDD" from C:\DATA to C:\ARCHIVE. You will just need to replace the directory paths to fit your needs. You might want to tweak the COPY command too, I have it set to verify the copy as well as run in restartable mode (good for copying over a network).

You will also need to change it so that it copies to the right path to your server, depending on whether you run the batch on the server (pull the files from the clients) or on each workstation (push the files to the server).

Either way, the big thing was the date. If you need to modify the batch and aren't sure how, just shout.


Report Offensive Follow Up For Removal

Response Number 2
Name: jtwendel
Date: January 11, 2004 at 17:44:12 Pacific
Subject: Batch File Help
Reply: (edit)

This works great except I need the previous date folder copied not the current date. Is there anyway to do this? Also, can the folder be zipped after it is copied?


Report Offensive Follow Up For Removal

Response Number 3
Name: adamyoule
Date: January 12, 2004 at 13:42:09 Pacific
Subject: Batch File Help
Reply: (edit)

no, there isn't a way to do that, other than good old fasioned hard work

sorry!!


Report Offensive Follow Up For Removal

Response Number 4
Name: Dr. Nick
Date: January 12, 2004 at 16:39:52 Pacific
Subject: Batch File Help
Reply: (edit)

Sorry I haven't gotten back to you sooner... I really do have other things I do :)

As far as the date, I'm not sure. It would be tougher than getting the current date because you would have to subtract a day. This sounds easy until you think about new months, when you'd have to subtract a month and set the day back to 30 or 31. Then you've got years to worry about too. What you'd really need is a calendar system and from that end this doesn't seem like it will work from a batch file.

The zipping is pretty easy using either WinZip's undocumented command line options or for more options, WinZip's Command Line Addon.

After thinking about it though, I had an idea that might work a couple ways. First, is your server left on overnight? What you could do is run the batch file each day, and have it read an environmental variable and use that value as the previous day's value. Once it's done that, just have it reset that variable to the current day's value. This way when it's run the next day that variable will hold the previous day's value.

The problem with that is that if you shut the machine down, the environmental variable will be erased.

The other option is just to schedule the batch file to run at something like 11:55pm each night. This way you win two ways, first it's easy to get the folder name you want with the script I already came up with and second, there's nobody in the office to mess up the backup.

What do you think?


Report Offensive Follow Up For Removal

Response Number 5
Name: jwendel
Date: January 13, 2004 at 06:28:10 Pacific
Subject: Batch File Help
Reply: (edit)

This is what did it.

@echo off
cls
REM ***********************
REM extract current date
REM ***********************
for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
set DayOfWeek=%%i
set Month=%%j
set Day=%%k
set Year=%%l
)

REM ***********************
REM subtract 1 day to today's date to get backup directory
REM ***********************

set Lday=%Day%
set /A LMonth=%Month%
set /A Lday -= 1

REM ***********************
REM If 1st of month get previous month's variable
REM ***********************

if %Day% EQU 01 GOTO Mday
Goto BackP

:Mday
if %LMonth% EQU 1 Goto :Jan
if %LMonth% EQU 2 Goto :Feb
if %LMonth% EQU 3 Goto :Mar
if %LMonth% EQU 4 Goto :Apr
if %LMonth% EQU 5 Goto :May
if %LMonth% EQU 6 Goto :Jun
if %LMonth% EQU 7 Goto :Jul
if %LMonth% EQU 8 Goto :Aug
if %LMonth% EQU 9 Goto :Sep
if %LMonth% EQU 10 Goto :Oct
if %LMonth% EQU 11 Goto :Nov
if %LMonth% EQU 12 Goto :Dec

:Jan
if %LDay% EQU 1 goto PrevYear
set /A Lday += 31
set /A LMonth = 12
Goto Backp

:Feb
set /A Lday += 31
set /A LMonth = 1
Goto Backp

REM ***********************
REM Check for leap year
REM ***********************

:Mar
if %Year% EQU 2004 set /A Lday += 29
if %Year% EQU 2008 set /A Lday += 29
if %Year% EQU 2012 set /A Lday += 29
if %Year% EQU 2016 set /A Lday += 29
else set /A Lday += 28
set /A LMonth = 2
Goto Backp

:Apr
set /A Lday += 31
set /A LMonth = 3
Goto Backp

:May
set /A Lday += 30
set /A LMonth = 4
Goto Backp

:Jun
set /A Lday += 31
set /A LMonth = 5
Goto Backp

:Jul
set /A Lday += 30
set /A LMonth = 6
Goto Backp

:Aug
set /A Lday += 31
set /A LMonth = 7
Goto Backp

:Sep
set /A Lday += 31
set /A LMonth = 8
Goto Backp

:Oct
set /A Lday += 30
set /A LMonth = 9
Goto Backp

:Nov
set /A Lday += 31
set /A LMonth = 10
Goto Backp

:Dec
set /A Lday += 30
set /A LMonth = 11
Goto Backp

REM ***********************
REM PrevYear is to take account of jan 1st previous day, which is dec 31st
REM ***********************

:PrevYear
set /A Year -= 1
set /A LDay = 31
set /A LMonth = 12
goto Backp

REM ***********************
REM pad days and months with 0's to get correct format if day is 1-9, and month is 1-9
REM ***********************

:Backp
ECHO Copying today's folder...

if %Lday% LEQ 9 set Nday=0%Lday%
if %Lday% GEQ 10 set Nday=%Lday%
if %LMonth% LEQ 9 set Nmonth=0%LMonth%
if %LMonth% GEQ 10 set Nmonth=%LMonth%

set /A v_date = %Year%%Nmonth%%Nday%

REM ***********************
REM start backup here
REM ***********************
MD q:\Chili'sParis\Archive\%v_date%
ECHO.
ECHO Copying '%v_date%'...
COPY /V /Z "v:\Something\%v_date%" "q:\Something\Archive\%v_date%"
ECHO.
copy /Y v:\Something\*.txt q:

Goto End

:End
ECHO Finished copying.
ECHO.



Report Offensive Follow Up For Removal


Response Number 6
Name: Dr. Nick
Date: January 13, 2004 at 11:48:19 Pacific
Subject: Batch File Help
Reply: (edit)

Glad you got it.


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch File Help

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge