• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

VBS or Batch file to copy files

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

tunnel

Member
Joined
Feb 17, 2005
Location
Newcastle
Hi,

Once again i rely on the expertise knowledge of the ocforum to help me. Here is the Secnario below.

At work we have a web system called 9iLite which our surveyors use to submit there reports after they have surveyed a property. Everyday a member of my team has to RDP onto the 9iLite Server (NT40) and clear all the log files from the previous day. I was wondering if it's possible to create a batch file to copy all files with the .log extension to a network resource for backup and then delete them from NT40. Or if this could be done using cut instead of copy and delete. I would be very grateful for any ideas or suggestions you have.

Regards,
Greg
 
a simple batch script like this should work:

Code:
@echo off
c:
cd \temp\temp_logs
move /y *.log \\<hostname>\<sharename>

just add it as a scheduled task on the box to run at whatever time you want daily. just so you know, the "/y" flag on move is to suppress the prompt to overwrite files of the same name in the destination. let me know if you have any questions.
 
cd start path here
xcopy /S *.log setcopytolocationhere
del *.log /S /Q

To see how this would work, let's assume a couple of things. All .log files on D: are to be copied to a shared location called LOG_BACKUP on SERVER1

Code:
D:
xcopy /S *.log \\SERVER1\LOG_BACKUP
del *.log /S /Q

This will work so long as the NT server has XCOPY on it (easy to get if not) and is run from the server. If you want to run it remotely, I would look into grabbing PSEXEC from Sysinternals. If you need to run it remotely, post back and I can show you quickly how to use PSEXEC to kick it off.

EDIT:

Above is pretty much same thing except moving instead of copy then delete and it's not recursive (but you may not need or want it to be) :)
 
jon's solution would work better if there are multiple directories under the log directory since it recursively copies.
 
Cheers for the reply guys, how can i get it to do it for files older then today? As there needs to be log files from today that need to be kept, they will then be backed up and deleted tomorrow.
 
well you could either run it in the middle of the night or you would probably have to go with something like xxcopy as seen here:

http://groups.google.com/group/microsoft.public.win2000.cmdprompt.admin/browse_thread/thread/ebdf5aae75e9800e said:
Since the built-in DEL (or ERASE) command does not allows you
to specify the age of the file, you need to find something else.
Since there is no direct support by the batch file itself
you need to find some tool which provides such a function.

XXCOPY is one solution which provide a one-line command which
is more than capable of finding and deleting files by age.

XXCOPY is an enhanced XCOPY with rich set of command witches.
It is available as freeware for personal use at

http://www.xxcopy.com

Here are some basic command switches

/L // list-only (no copy or delete)
/RS // remove (delete) from the source
/DB#<n> // select files that are n days or older
/DA#<n> // select files that are n days or younger

Here's a few examples:

xxcopy c:\mydir\* /L /DB#10 // list files 10 days or older
xxcopy c:\mydir\*.DOC /L /DB#7 // list *.DOC files, >= 10 days old
xxcopy c:\mydir\* /RS /DB10 // delete files 10 days or older
xxcopy c:\mydir\* /RS /DB#10 /DA#30 // files 10-30 day sold
xxcopy c:\mydir\* /RS /DB#1 // files made yesterday or earlier
xxcopy c:\mydir\* /RS /DA#0 // files made today (since midnight)
xxcopy c:\mydir\* /RS /DA#10H // files made within 10 hours
xxcopy c:\mydir\* /RS /DA#30M // files made within 30 minutes
xxcopy c:\mydir\* /RS /DA#90S // files made within 90 seconds
xxcopy c:\mydir\* /RS /DB#365 /S // include subdirectories

You may use an absolute value (/DA:yyyy-mm-dd , /DB:yyyy-mm-dd)

xxcopy c:\mydir\* /RS /DB:2002-10-31 // files made on or before
xxcopy c:\mydir\* /RS /DA:2001-11-01 // files made on or after
xxcopy c:\mydir\* /RS /DO:2000-01-01 // files made on the day
xxcopy c:\mydir\* /RS /DA:2001-01 // on or after 2001-01-01
xxcopy c:\mydir\* /RS /DA:2001 // on or after 2001-01-01
xxcopy c:\mydir\* /RS /DB:2001-12 // on or before 2001-12-31
xxcopy c:\mydir\* /RS /DB:2001 // on or before 2001-12-31
xxcopy c:\mydir\* /RS /DO:2001-12 // files made in 2001-12
xxcopy c:\mydir\* /RS /DO:2001 // files made during year 2001

I believe the above examples are sufficient to run the /RS (remove from
source) command in conjunction with /DA and /DB switches.

More common usage of XXCOPY is to copy files as the name implies.
In general, its command syntax is compatible with XCOPY. These are
a small fraction of what XXCOPY can do for you in variety of file
management operations.

XXCOPY is compatible with Windows 95/98/ME/NT/2K/XP. It comes with
XXCOPY16 with essentially the same set of switches for DOS environment.
One of the problems using a batch file is that it is difficult and messy
to write a good script that works well in all Windows versions.

Kan Yabumoto
The Author of XXCopy
 
You will want to use Robocopy instead of XCOPY in that matter. There are several different ways you could then go about it (copy then delete, move, etc.), so you may want to take a look at it and if you're having trouble post back and we can help then.

To get you started, you may want to pay close attention to the /MOV and /MINAGE switches as those will probably be what you want to work with.

EDIT:

I'm too slow again lol
 
Hi All,

I have managed to do this using the script below. Thanks for all your help.

Code:
@echo off

set loglocation=\\helpdeskdev01\c$\Documents and Settings\All Users\Desktop\NT40 Backups
set olderthan=1
set source=C:\Ora9iLite\Mobile\Server\bin
set destination=\\helpdeskdev01\c$\Documents and Settings\All Users\Desktop\NT40 Backups\Backups
set extension=log

if exist %loglocation% del %loglocation%

echo Copying and deleting files older than %olderthan% days with the file extension *.%extension%.
echo Copying and deleting files older than %olderthan% days with the file extension *.%extension%. >> %loglocation%
echo Copying from %source% to %destination% and deleting from %source%.
echo Copying from %source% to %destination% and deleting from %source%. >> %loglocation%
echo This file may take an extremely long time to run while it looks unresponsive.
echo Check %loglocation% for copy progress.

cd %source%

echo List of files to be copied and removed: >> %loglocation%
echo List of files to be copied and removed:

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C Echo "@FILE"
FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C Echo "@FILE" >> %loglocation%

echo Starting copy...
echo Starting copy... >> %loglocation%
date /t >> %loglocation%
time /t >> %loglocation%

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C XCOPY %source%\@FILE %destination%"

echo Starting delete...
echo Starting delete... >> %loglocation%
date /t >> %loglocation%
time /t >> %loglocation%

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C if exist %destination%\@FILE del %source%\@FILE /Q & echo @FILE Deleted." >> %loglocation%

echo Log file located at %loglocation%

echo Copy and Delete finished... Sending Report.
echo Copy and Delete finished... >> %loglocation%

echo Source Directory Contents: >> %loglocation%
echo. >> %loglocation%
dir %source% >> %loglocation%

echo. >> %loglocation%
echo. >> %loglocation%
echo. >> %loglocation%

echo Destination Directory Contents: >> %loglocation%
echo. >> %loglocation%
dir %destination% >> %loglocation%

date /t >> %loglocation%
time /t >> %loglocation%

set server=%computername%
%blat% %loglocation% -t %email% -s "%server% %subject%" -server %relayserver% -f do-not-reply@%server%.copyout.bat

echo Report sent!
 
Back