Page 1 of 1
copy most recent file to remote pc and clean up old files.
Posted: 09 Apr 2010 10:52
by bobby999
i want to create a simple script to transfer latest file in a folder to a remote server folder:
COPY 'most recent file' FROM "C:\Source folder" TO "\\remote server\destination folder."
Delete files older than one week in "C:\Source folder"
Delete files older than one week in "\\remote server\destination folder."
I then plan to put this script in windows task scheduler to run every day
Any help would be great
Can i use Xcopy?
can i just map the drive of the remote server for it to work?
Will this work even if i am not logged in? ( aslong as the account is in windows tasck scheduler and has access to the other server?)
Re: copy most recent file to remote pc and clean up old file
Posted: 09 Apr 2010 13:11
by jeb
Hi bobby999,
I have not a solution, only some ideas.
can i just map the drive of the remote server for it to work?
This can be complicated, because network drives (with letter) does exists under the account, not for the machine.
Can i use Xcopy?
I assume not, try to use "dir" with order the files with /od.
Try also "dir" or "for" to get the files for deletion.
jeb
Re: copy most recent file to remote pc and clean up old file
Posted: 15 Apr 2010 07:03
by bobby999
no else advise?
Re: copy most recent file to remote pc and clean up old file
Posted: 15 Apr 2010 09:56
by avery_larry
I have a code that relies on a 3rd party program called "isdate.com" (I'm sure you could google it and download):
Code: Select all
@echo off
if 1%1==1 goto :help
setlocal enabledelayedexpansion
cls
if 2%2==2 (
echo.
echo The current folder is:
echo !cd!
echo Are you sure you want to delete all files
echo in the current folder that are older than
echo %1 days? This is your only warning.
echo.
choice /t:N,30
if errorlevel 2 goto :eof
) else (
if /i 2%2==2/y (
echo.
echo The current folder is:
echo !cd!
echo In 10 seconds, all files in this folder
echo older than %1 days will be deleted.
echo Press N to stop or Y to skip the 10 second
echo wait.
echo.
choice /t:Y,10
if errorlevel 2 goto :eof
) else (
goto :help
)
)
set ndel=0
cls
echo Processing . . .
for /f %%a in ('dir /b /a-d /od') do (
isdate %%a +%1 before TODAY
if errorlevel 1 (
echo Files deleted: !ndel!
exit /b
)
set /a ndel+=1
del %%a
)
goto :eof
:help
cls
echo.
echo The correct format is:
echo.
echo deldate N [/y]
echo.
echo where N is the number of days you wish
echo to keep and /y option allows you to
echo bypass the failsafe "Are you sure?"
echo question.
echo.
goto :eof
I won't take the time, but you can use
http://www.dostips.com/DtTipsDateTime.php to convert file dates to julian and then do the math yourself (this is the part that isdate.com does).
For the "most recent file", do this:
*untested*
Code: Select all
@echo off
set "sourcedir=c:\source folder"
set "destdir=x:\destination folder"
net use x: "\\remote server\share"
for /f %%a in ('dir /b /a-d /o-d "%sourcedir%"') do (
copy /y "%sourcedir%\%%~a" "%destdir%"
goto :continue
)
:continue