Page 1 of 1

[solved] Batch files and DOS

Posted: Mon Feb 06, 2006 5:36 am
by phpScott
Remeber the days, batch files and DOS, command line and the like?
I thought I would try here first instead of being forced to create a membership on a forum I'll likely use only once.

I have created a batch file that will copy all the files from one directory to another if they have been modified after a certian date.
Great so far.
The batch file then ftp's them up to the site into another folder waiting for processing.
The remote side is easy, cron job to process nightly.

Problem:
when using the /D:%date% swith in xcopy it returns a date of 14/02/2006
what xcopy is looking for is 02/12/02006
the difference being that the computer is set to use the british way of date and xcopy wants the north american form of date.

I can't find a way to change the date format to one the xcopy would like.

Any ideas, suggestions, solutions would be great.

Cheers

Posted: Mon Feb 06, 2006 5:53 am
by JayBird
sorry i cant help you with your problem, but i would be interested in that batch file myself.

Any chance of passing it on?

Thanks

Posted: Mon Feb 06, 2006 6:06 am
by phpScott
If I can get this silly date thing sorted, no worries.
Actually here:

main file checks to see if the directory upload exists, if it does delete it, I want a clean slate everytime
copy all the files and directories from c:\image to c:\upload that have been modified on todays date or later(this is where the problem is).
open an ftp connection using the DosFtpFile.ftp file
when finished uploading remove the c:\upload directory


main file: I called it copyPage.bat

Code: Select all

@echo off
if exist C:\upload\nul rmdir C:\upload /s /Q
if not exist C:\upload\nul md C:\upload
xcopy c:\image\*.* c:\upload /d:%date% /s/K/C
pause
ftp -i -s:DosFtpFile.ftp xxx.xxx.xxx.xxx
pause
rmdir C:\upload /s /Q
second file called DosFtpFile.ftp
this provides the username and password of the ftp site as well as any other ftp commands neccesary.
mput is multiple put.

Code: Select all

username
password
cd testScott
mput c:\upload\*.* /y
quit
copy and past these into a plain text editor and save with correct extension

remove the pause commands so you don't have to 'press any key to continue' bit.
thats about it, I plan to hook it up to the windows scheduler.

To make it more robust there should be error logging and the ability to make directories on the ftp site but that will have to come later.

Posted: Mon Feb 06, 2006 6:42 am
by JayBird
Thanks for that...im gonna use this for backing up data from one drive to and eternal drive

Posted: Mon Feb 06, 2006 7:43 am
by phpScott
yeah, solved, happy dance.

The problem is that there is not date manipulation in DOS or Batch programming, thank fully someone else had the same problem a little while ago and came up with a solution, fdate. A handy utility to do string manipulation based on your system clock.

There are still a few things that need to be done to the following code such as error logging and recreate directory structure on the host but that will take time and current client doesn't require it, but as it gets added so will this page.

leave the '\nul' bit on the 2nd and 3rd lines to check if a folder exists

***use at your own risk***

Code: Select all

REM created by phpScott@hotmail.com
@echo off
REM if directory upload exits delete it and all files
if exist C:\upload\nul rmdir C:\upload /s /Q

REM if upload directory doesn't exist then create it.
if not exist C:\upload\nul md C:\upload
REM fancy FDATE stuff http://www.ferg.org/fdate/
for /f "tokens=*" %%v in ('FDATE /Ff /C"~~M/~D/~Y"') do set date1=%%v

REM copy everything in the image directory based on sytem date.
REM if a one off or don't mind editing file daily the use the format of mm/dd/yyyy
xcopy c:\image\*.* c:\upload /d:%date1% /s/K/C

REM create FTP connection using second file
ftp -i -s:DosFtpFile.ftp 192.168.1.120

REM clean up temporary directory
rmdir C:\upload /s /Q
REM close the application
exit
DosFtpFile.ftp is the same as the one above, for completness here it is again.

Code: Select all

username
password
cd testScott
mput c:\upload\*.* /y
quit
no comments on this page as it would ruin the ftp commands.
mput is for multiple put (more then one file, can be a list,comma seperated, or wildcards are acceptable).

download the fdate zip file and copy the fdate.exe to the directory where you will be running the batch file from, otherwise you will have to modify your path variable in windows.

As usual don't sue me if it messes up your machine(s) as it works for me and that was what I need it for,
***use at your own risk***

Have fun.