Computing.Net > Forums > Programming > Batch File To temporarily backup

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Batch File To temporarily backup

Reply to Message Icon

Original Message
Name: PumpkinKingJack
Date: March 9, 2004 at 10:18:21 Pacific
Subject: Batch File To temporarily backup
OS: Windows 2000 Pro Server
CPU/Ram: HP Brio/450
Comment:

Ok So I really need help,

I need this batch file to run a backup of files that are in a certain directory(directory A), then copy those to a new directory (directory B). Then I need the computer to to take all of the file folders created by the previous step that are less than two weeks old move those to a temp directory (dir C). Next I need the batch program to finally delete what's left in dirB and recreate the directory itself and move the items back from dirC back to the new dirB. I hope that made sense. Any help is alway appreciated.

Here's the code

@Echo Off
cls
Echo Running Veritas Batch File...
Echo.
Echo.

REM ***********************
REM Takes files in source directory and copies files to target directory.
REM NOTE* Creates new directory ARCHIVE in Target directory first time run.
REM ***********************

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:\Documents and Settings\Adam\My Documents\Batch\*.*" "C:\ARCHIVE\%v_date%"
ECHO.
ECHO Finished copying.

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
)

set num=1

REM ***********************
REM subtract 14 days to today's date to get backup directory
REM ***********************

ECHO.
ECHO.
ECHO Deleting Old Files...

:START

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

if %num% EQU 1 GOTO Why1:
if %num% EQU 2 GOTO Why2:
if %num% EQU 3 GOTO Why3:
if %num% EQU 4 GOTO Why4:
if %num% EQU 5 GOTO Why5:
if %num% EQU 6 GOTO Why6:
if %num% EQU 7 GOTO Why7:
if %num% EQU 8 GOTO Why8:
if %num% EQU 9 GOTO Why9:
if %num% EQU 10 GOTO Why10:
if %num% EQU 11 GOTO Why11:
if %num% EQU 12 GOTO Why12:
if %num% EQU 13 GOTO Why13:
if %num% EQU 14 GOTO Why14:
if %num% EQU 15 GOTO Why15:

:Why1
set %Lday% -= 14
GOTO PointA:
:Why2
set %Lday% -= 13
GOTO PointA:
:Why3
set %Lday% -= 12
GOTO PointA:
:Why4
set %Lday% -= 11
GOTO PointA:
:Why5
set %Lday% -= 10
GOTO PointA:
:Why6
set %Lday% -= 9
GOTO PointA:
:Why7
set %Lday% -= 8
GOTO PointA:
:Why8
set %Lday% -= 7
GOTO PointA:
:Why9
set %Lday% -= 6
GOTO PointA:
:Why10
set %Lday% -= 5
GOTO PointA:
:Why11
set %Lday% -= 4
GOTO PointA:
:Why12
set %Lday% -= 3
GOTO PointA:
:Why13
set %Lday% -= 2
GOTO PointA:
:Why14
set %Lday% -= 1
GOTO PointA:
:Why15
set %Lday% -= 0
GOTO PointA:

:PointA


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.
ECHO.

if %Lday% GEQ 1 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%

if %num% EQU 1 GOTO Loop1:
if %num% EQU 2 GOTO Loop2:
if %num% EQU 3 GOTO Loop3:
if %num% EQU 4 GOTO Loop4:
if %num% EQU 5 GOTO Loop5:
if %num% EQU 6 GOTO Loop6:
if %num% EQU 7 GOTO Loop7:
if %num% EQU 8 GOTO Loop8:
if %num% EQU 9 GOTO Loop9:
if %num% EQU 10 GOTO Loop10:
if %num% EQU 11 GOTO Loop11:
if %num% EQU 12 GOTO Loop12:
if %num% EQU 13 GOTO Loop13:
if %num% EQU 14 GOTO Loop14:
if %num% EQU 15 GOTO Loop15:

:Loop1
set /A v_date1= %Year%%Nmonth%%Nday%
MD C:\ARCHIVE2

ECHO.

MOVE C:\ARCHIVE\%v_date1% C:\Archive2\%v_date%

set num=2
GOTO Start:

:Loop2
set /A v_date2= %Year%%Nmonth%%Nday%
ECHO.

MOVE C:\ARCHIVE\%v_date2% C:\Archive2\%v_date%
set num=3
GOTO Start:

:Loop3
set /A v_date3= %Year%%Nmonth%%Nday%
ECHO.

MOVE C:\ARCHIVE\%v_date3% C:\Archive2\%v_date%
set num=4
GOTO Start:

:Loop4
set /A v_date4= %Year%%Nmonth%%Nday%
ECHO.

MOVE C:\ARCHIVE\%v_date4% C:\Archive2\%v_date%
set num=5
GOTO Start:

:Loop5
set /A v_date5= %Year%%Nmonth%%Nday%
ECHO.

MOVE C:\ARCHIVE\%v_date5% C:\Archive2\%v_date%
set num=6
GOTO Start:

:Loop6
set /A v_date6= %Year%%Nmonth%%Nday%
ECHO.

MOVE C:\ARCHIVE\%v_date6% C:\Archive2\%v_date%
set num=7
GOTO Start:

:Loop7
set /A v_date7= %Year%%Nmonth%%Nday%
ECHO.

MOVE C:\ARCHIVE\%v_date7% C:\Archive2\%v_date%
set num=8
GOTO Start:

:Loop8
set /A v_date8= %Year%%Nmonth%%Nday%
ECHO.

MOVE C:\ARCHIVE\%v_date8% C:\Archive2\%v_date%
set num=9
GOTO Start:

:Loop9
set /A v_date9= %Year%%Nmonth%%Nday%
ECHO.

MOVE C:\ARCHIVE\%v_date9% C:\Archive2\%v_date%
set num=10
GOTO Start:

:Loop10
set /A v_date10= %Year%%Nmonth%%Nday%
ECHO.

MOVE C:\ARCHIVE\%v_date10% C:\Archive2\%v_date%
set num=11
GOTO Start:

:Loop11
set /A v_date11= %Year%%Nmonth%%Nday%
ECHO.

MOVE C:\ARCHIVE\%v_date11% C:\Archive2\%v_date%
set num=12
GOTO Start:

:Loop12
set /A v_date12= %Year%%Nmonth%%Nday%
ECHO.

MOVE C:\ARCHIVE\%v_date12% C:\Archive2\%v_date%
set num=13
GOTO Start:

:Loop13
set /A v_date13= %Year%%Nmonth%%Nday%
ECHO.

MOVE C:\ARCHIVE\%v_date13% C:\Archive2\%v_date%
set num=14
GOTO Start:

:Loop14
set /A v_date14= %Year%%Nmonth%%Nday%
ECHO.

MOVE C:\ARCHIVE\%v_date14% C:\Archive2\%v_date%
set num=15
GOTO Start:

:Loop15
set /A v_date15= %Year%%Nmonth%%Nday%
ECHO.

MOVE C:\ARCHIVE\%v_date15% C:\Archive2\%v_date%


REM ***********************
REM deletes old files (past two weeks old)
REM ***********************

ECHO.
ECHO Deletes All Records Older Than 2 Weeks
ECHO.
RMDIR C:\ARCHIVE /s
MD C:ARCHIVE
MOVE C:ARCHIVE2\*.* C:ARCHIVE
ECHO.

ECHO Finished Deleting Old Files.
ECHO.

REM ***********************
REM Displays local computer's IP Address
REM ***********************

ECHO.
ECHO.

ipconfig.exe

Echo.
Echo.

REM ***********************
REM Displays current time
REM ***********************

Echo It's %Time% now

Pause
cls


Report Offensive Message For Removal


Response Number 1
Name: FishMonger
Date: March 9, 2004 at 16:58:30 Pacific
Reply: (edit)

I'm not much of a dos/nt batch programmer, but, If you're interested, I can help you with a Perl script that will do this a lot easier. For instance, your date manipulation (subtracting 14 days) can be done in Perl with 1 to 3 lines of code and would accommodate for leap years. Likewise, the code for moving of the files can be dramatically reduced.


Report Offensive Follow Up For Removal

Response Number 2
Name: PumpkinKingJack
Date: March 10, 2004 at 07:48:13 Pacific
Reply: (edit)

Thanks for the suggestion. Unfortunately I know that Perl would be much easier but the higher ups for some reason unbeknownst to us little interns want this done in Batch. If you can see anything in the code that I should double check specificaly though it'd be a help. This code just makes me dizzy now I've gone through it so many times.


Report Offensive Follow Up For Removal

Response Number 3
Name: FishMonger
Date: March 10, 2004 at 11:40:59 Pacific
Reply: (edit)

>> This code just makes me dizzy

I agree, I got a headache jumping back and forth between all those goto statements.

You should look at efficiency, that batch script is doing allot of unnecessary coping and moving of files. Do the file's datestamp test first and only copy (or move) those files that are less than 14 days old.

It might be in your best interest to present them with 2 solutions, the batch script and the Perl script. Currently this batch file has just over 400 lines just to copy files that are less than 14 days old! I haven't done any testing, but my guess is that we can do it in Perl in 20 lines or less.


Report Offensive Follow Up For Removal

Response Number 4
Name: FishMonger
Date: March 10, 2004 at 14:00:03 Pacific
Reply: (edit)

This might need a little tweeking, but if I understand what you're tring to accomplish, this (perl script) should do what you need. See if the higher ups are interested.


#!perl -w

use strict;
use POSIX;
use File::Find;
use File::Copy;

my $dir = 'c:/Documents and Settings/Adam/My Documents/Batch';
my $today = strftime ("%Y%m%d", localtime);

finddepth(\&archive, $dir);
sub archive {
if (-M $_ < 15) {
my $path = $File::Find::dir;
$path =~ s!$dir!c:/$today!;
mkdir $path unless (-d $path);
copy $_, "$path/$_" unless -d;
}
}


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you own an iPhone?

Yes
No, but soon
No


View Results

Poll Finishes In 7 Days.
Discuss in The Lounge
Poll History




Data Recovery Software