Batch File Creation

Microsoft Windows xp professional w/serv...
March 11, 2010 at 12:44:02
Specs: Windows XP
Hi, i am in need of creating a dos batch file to simply copy all files from a directory that have a created date of today -1 (the previous days date) on one server to another directory on another server. After copying them i require them to be zipped up and the original files to then be deleted. i am only very new at batch file creation so am asking for as much help as possible please.

Thanks


See More: Batch File Creation

Report •


#1
March 11, 2010 at 12:56:51
Try this...

@echo off
setLocal EnableDELAYedExpansion

if "%1"=="" (
  echo Must specify a tombstone [number of days].
  goto :EOF
  )
if "%2"=="" (
  echo Must specify a source directory.
  goto :EOF
  )
if "%3"=="" (
  echo Must specify a destination directory.
  goto :EOF
  )


Set Day=%Date:~-7,2%
Set Month=%Date:~-10,2%
Set Year=%Date:~-4,4%

Call :jdate %Year% %Month% %Day%
Set /a CompairDate=%jdate%
Set /a CompairDate=!CompairDate!-%1 
Set FileDate=1

for /f "tokens=*" %%a in ('dir %2 /s/b') do (
  set T=%%~Ta
  set FileDay=!T:~3,2!
  set FileMonth=!T:~0,2!
  set FileYear=!T:~6,4!
  
  call :jdate !FileYear! !FileMonth! !FileDay!
  set /a FileDate=!Jdate!
  
  if "!FileDate!" lss "!CompairDate!" (
    copy %2 %3
    compact %3 /c
    attrib "%%a" -r
    del "%%a"
    echo *Copied and Deleted* %%a !FileMonth!-!FileDay!-!FileYear!
    )
  )
Goto :EOF

:JDate
:: Convert date to Julian

SET MM=%2
SET DD=%3
IF %MM:~0,1% EQU 0 SET MM=%MM:~1%
IF %DD:~0,1% EQU 0 SET DD=%DD:~1%

SET /A Month1 = ( %MM% - 14 ) / 12
SET /A Year1  = %1 + 4800
SET /A JDate  = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %MM% - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %DD% - 32075
SET Month1=
SET Year1=
GOTO:EOF

To use thia the syntax is depending on what you called your batch file...

mycopy 1 \\srvr1\share\thesourcedir \\srvr2\share\thedestinationdir

hope it helps.


Report •

#2
March 11, 2010 at 13:04:29
Hi ace omega,
Thanks for your quick response. i'm sorry but i really don't understand the script you have posted. are you able to explain what i have to do to get this to work?

Thanks again


Report •

#3
March 11, 2010 at 13:13:26
you have to save it in a batch file like mycopy.bat then you can run it from a command prompt.

To use thia the syntax is depending on what you called your batch file...

mycopy 1 \\srvr1\share\thesourcedir \\srvr2\share\thedestinationdir

its got three parameter...

1 - is the number of days
2 - source directory
3 - destination directory

hope it helps.


Report •

Related Solutions

#4
March 11, 2010 at 13:26:31
ok i just added in the below as a test but it started to randomly delete items from my desktop lol. i figure i still don't understand i'm sorry. number of days i set to 1 as i thought this would mean that it should select all files older than today? C:\temp as the source i thought and c:\batch test as the destination to copy the temp files into?


if "%1"=="1" (
echo Must specify a tombstone [number of days].
goto :EOF
)
if "%2"=="c:\temp" (
echo Must specify a source directory.
goto :EOF
)
if "%3"=="c:\batch test" (
echo Must specify a destination directory.
goto :EOF
)


Report •

#5
March 11, 2010 at 13:34:46
No you do not need to modify the code at all. You go to a command prompt and type...

c:\> mycopy 1 c:\temp c:\batch

This code is only to worn the user they forgot to fill in all of the parameters. Do you know how to use the command prompt?

If you want to use a short cut then you simply put the parameters into the "Target:" like...

"c:\documents and settings\yourname\desktop\mycopy.bat" 1 c:\temp c:\batch

Read this and teach your self batch scripts before you start using them or you could mess up your computer...

http://www.allenware.com/icsw/icswi...
http://www.robvanderwoude.com/batch...


Report •

#6
March 11, 2010 at 13:48:05
This is what command prompt showed me once i ran it. nothing was created in the destination directory and files still existed in the c:\temp? sorry i hope i am doing everything right as i believe i am.

c:\temp\lbia04012
c:\temp\lbia04304
c:\temp\lbia05580
c:\temp\lbia06140
c:\temp\print-temp-table.dmp
c:\temp\rcda01892
c:\temp\rcda02964
c:\temp\rcda03156
c:\temp\rcda03756
c:\temp\rcda04012
c:\temp\rcda04304
c:\temp\rcda05580
c:\temp\rcda06140
c:\temp\srt02024
c:\temp\srt03936
c:\temp\srta01892
c:\temp\srta02964
c:\temp\srta03156
c:\temp\srta03756
c:\temp\srta04012
c:\temp\srta04304
c:\temp\srta05580
c:\temp\srta06140
1 file(s) copied.

Compressing files in c:\


0 files within 1 directories were compressed.
0 total bytes of data are stored in 0 bytes.
The compression ratio is 1.0 to 1.
*Copied and Deleted* c:\temp\DBI3380a03784 12-02-2010
c:\temp\DBI02024
c:\temp\DBI03936
c:\temp\DBI1180a00988
c:\temp\DBI1268a03584
c:\temp\DBI1312a03828
c:\temp\DBI1596a06020
c:\temp\DBI1660a00968
c:\temp\DBI1704a04048
c:\temp\DBI1728a03572
c:\temp\DBI1844a01492
c:\temp\DBI1844a03824
c:\temp\DBI1872a04960
c:\temp\DBI1912a03716
c:\temp\DBI1940a05020
c:\temp\DBI2152a02620
c:\temp\DBI2368a05352
c:\temp\DBI2400a03968
c:\temp\DBI2472a03608
c:\temp\DBI2512a05144
c:\temp\DBI2612a03364
c:\temp\DBI2772a04536
c:\temp\DBI2808a02824
c:\temp\DBI2876a03880
c:\temp\DBI2932a02728
c:\temp\DBI2936a04012
c:\temp\DBI2980a04184
c:\temp\DBI3064a06088
c:\temp\DBI3084a00868
c:\temp\DBI3184a04304
c:\temp\DBI3188a01888
c:\temp\DBI3220a02644
c:\temp\DBI3264a01032
c:\temp\DBI3300a02320
c:\temp\DBI3404a04760
c:\temp\DBI3544a01576
c:\temp\DBI3612a00720
c:\temp\DBI3628a00744
c:\temp\DBI3652a02376
c:\temp\DBI3672a03844
c:\temp\DBI3684a03664
c:\temp\DBI3828a03252
c:\temp\DBI3828a03384
c:\temp\DBI3852a01016
c:\temp\DBI388a04452
c:\temp\DBI3900a05444
c:\temp\DBI3916a03472
c:\temp\DBI3932a05424
c:\temp\DBI4004a01624
c:\temp\DBI4012a04392
c:\temp\DBI4028a03404
c:\temp\DBI4044a04960
c:\temp\DBI4048a02716
c:\temp\DBI4092a02964
c:\temp\DBI4172a04752
c:\temp\DBI4196a05644
c:\temp\DBI4280a00552
c:\temp\DBI4316a05732
c:\temp\DBI4440a01360
c:\temp\DBI4548a04132
c:\temp\DBI4560a04764
c:\temp\DBI4764a05480
c:\temp\DBI4784a04752
c:\temp\DBI4852a04660
c:\temp\DBI5016a03968
c:\temp\DBI5064a04236
c:\temp\DBI5252a00716
c:\temp\DBI5328a04960
c:\temp\DBI5384a01712
c:\temp\DBI5396a06140
c:\temp\DBI5424a05416
c:\temp\DBI5436a02652
c:\temp\DBI5540a04688
c:\temp\DBI5560a05792
c:\temp\DBI5576a05580
c:\temp\DBI5680a05052
c:\temp\DBI5688a00692
c:\temp\DBI5804a05012
c:\temp\DBI5848a02960
c:\temp\DBI5848a06060
c:\temp\DBI5864a04680
c:\temp\DBI5932a03708
c:\temp\DBI6020a00512
c:\temp\DBI6020a05736
c:\temp\DBI6032a05064
c:\temp\DBI6048a05200
c:\temp\DBI6104a05620
c:\temp\DBI724a05400
c:\temp\DBI800a04064
c:\temp\invoice.pdf
c:\temp\lbi02024
c:\temp\lbi03936
c:\temp\lbia01892
c:\temp\lbia02964
c:\temp\lbia03156
c:\temp\lbia03756
c:\temp\lbia04012
c:\temp\lbia04304
c:\temp\lbia05580
c:\temp\lbia06140
c:\temp\print-temp-table.dmp
c:\temp\rcda01892
c:\temp\rcda02964
c:\temp\rcda03156
c:\temp\rcda03756
c:\temp\rcda04012
c:\temp\rcda04304
c:\temp\rcda05580
c:\temp\rcda06140
c:\temp\srt02024
c:\temp\srt03936
c:\temp\srta01892
c:\temp\srta02964
c:\temp\srta03156
c:\temp\srta03756
c:\temp\srta04012
c:\temp\srta04304
c:\temp\srta05580
c:\temp\srta06140
1 file(s) copied.

Compressing files in c:\


0 files within 1 directories were compressed.
0 total bytes of data are stored in 0 bytes.
The compression ratio is 1.0 to 1.
*Copied and Deleted* c:\temp\DBI5560a05792 12-02-2010
c:\temp\DBI02024
c:\temp\DBI03936
c:\temp\DBI1180a00988
c:\temp\DBI1268a03584
c:\temp\DBI1312a03828
c:\temp\DBI1596a06020
c:\temp\DBI1660a00968
c:\temp\DBI1704a04048
c:\temp\DBI1728a03572
c:\temp\DBI1844a01492
c:\temp\DBI1844a03824
c:\temp\DBI1872a04960
c:\temp\DBI1912a03716
c:\temp\DBI1940a05020
c:\temp\DBI2152a02620
c:\temp\DBI2368a05352
c:\temp\DBI2400a03968
c:\temp\DBI2472a03608
c:\temp\DBI2512a05144
c:\temp\DBI2612a03364
c:\temp\DBI2772a04536
c:\temp\DBI2808a02824
c:\temp\DBI2876a03880
c:\temp\DBI2932a02728
c:\temp\DBI2936a04012
c:\temp\DBI2980a04184
c:\temp\DBI3064a06088
c:\temp\DBI3084a00868
c:\temp\DBI3184a04304
c:\temp\DBI3188a01888
c:\temp\DBI3220a02644
c:\temp\DBI3264a01032
c:\temp\DBI3300a02320
c:\temp\DBI3404a04760
c:\temp\DBI3544a01576
c:\temp\DBI3612a00720
c:\temp\DBI3628a00744
c:\temp\DBI3652a02376
c:\temp\DBI3672a03844
c:\temp\DBI3684a03664
c:\temp\DBI3828a03252
c:\temp\DBI3828a03384
c:\temp\DBI3852a01016
c:\temp\DBI388a04452
c:\temp\DBI3900a05444
c:\temp\DBI3916a03472
c:\temp\DBI3932a05424
c:\temp\DBI4004a01624
c:\temp\DBI4012a04392
c:\temp\DBI4028a03404
c:\temp\DBI4044a04960
c:\temp\DBI4048a02716
c:\temp\DBI4092a02964
c:\temp\DBI4172a04752
c:\temp\DBI4196a05644
c:\temp\DBI4280a00552
c:\temp\DBI4316a05732
c:\temp\DBI4440a01360
c:\temp\DBI4548a04132
c:\temp\DBI4560a04764
c:\temp\DBI4764a05480
c:\temp\DBI4784a04752
c:\temp\DBI4852a04660
c:\temp\DBI5016a03968
c:\temp\DBI5064a04236
c:\temp\DBI5252a00716
c:\temp\DBI5328a04960
c:\temp\DBI5384a01712
c:\temp\DBI5396a06140
c:\temp\DBI5424a05416
c:\temp\DBI5436a02652
c:\temp\DBI5540a04688
c:\temp\DBI5576a05580
c:\temp\DBI5680a05052
c:\temp\DBI5688a00692
c:\temp\DBI5804a05012
c:\temp\DBI5848a02960
c:\temp\DBI5848a06060
c:\temp\DBI5864a04680
c:\temp\DBI5932a03708
c:\temp\DBI6020a00512
c:\temp\DBI6020a05736
c:\temp\DBI6032a05064
c:\temp\DBI6048a05200
c:\temp\DBI6104a05620
c:\temp\DBI724a05400
c:\temp\DBI800a04064
c:\temp\invoice.pdf
c:\temp\lbi02024
c:\temp\lbi03936
c:\temp\lbia01892
c:\temp\lbia02964
c:\temp\lbia03156
c:\temp\lbia03756
c:\temp\lbia04012
c:\temp\lbia04304
c:\temp\lbia05580
c:\temp\lbia06140
c:\temp\print-temp-table.dmp
c:\temp\rcda01892
c:\temp\rcda02964
c:\temp\rcda03156
c:\temp\rcda03756
c:\temp\rcda04012
c:\temp\rcda04304
c:\temp\rcda05580
c:\temp\rcda06140
c:\temp\srt02024
c:\temp\srt03936
c:\temp\srta01892
c:\temp\srta02964
c:\temp\srta03156
c:\temp\srta03756
c:\temp\srta04012
c:\temp\srta04304
c:\temp\srta05580
c:\temp\srta06140
1 file(s) copied.

Compressing files in c:\


0 files within 1 directories were compressed.
0 total bytes of data are stored in 0 bytes.
The compression ratio is 1.0 to 1.
*Copied and Deleted* c:\temp\DBI6104a05620 12-02-2010


Report •

#7
March 11, 2010 at 14:21:42
Oh sorry, I never used the compression in this code. I use it on my server but the destination I use is a compressed folder so I added the command for you. I missed the *.* change these lines..
copy %2 %3
compact %3 /c
to
copy %2\%%a %3\%%a
compact %3\*.* /c

Sorry.


Report •

Ask Question