Question : Copying a file and adding the date to the copied file using a batch script

Hi All,

I want to copy and move a database I have running on one computer to a location on my server. I also want the copied file to have the date added to the end of it, so that when the script copies the database over the next time, there will be none of this "This file already exists" nonsense. I want to be able to schedule this script to run and forget about it, knowing that I am getting my weekly backups done.

Answer : Copying a file and adding the date to the copied file using a batch script

At a simple level something like this...  looks more complicated than it needs by splitting it up -- could all be on one line pretty well.  Only problem with the date is, as you probably know, you can't have / characters in the filename so it repalces any / in the name with a - and removes and spaces while it is at it:

@echo off
set source="D:\logs\yourlog.txt"
if not exist "%source%" echo %date% %time% No file found at %source% >> "C:\logfile.txt" & goto :eof
REM remove /'s and replace with - in the date
set thedate=%date:/=-%
REM remove spaces from the date
set thedate=%date: =%
set dest="\\otherserver\share\logs\logfile-%thedate%.txt"
xcopy /y %source %dest%
if errorlevel 1 echo %date% %time% An error occured copying %source% to %dest% - error %errorlevel% >> "C:\logfile.txt" & goto :eof
echo %date% %time% Successful copy from %source% to %dest% >> "C:\logfile.txt"

Change the path on the source and dest line and the log file locations if you want it to record any problems running it

hth

Steve
Random Solutions  
 
programming4us programming4us