0

I tried Robocopy and a few others, but they only look at the date, but not time stamp of a file.

I found a few scripts that compare timestamps on files before copy, but not calling a .bat on time settings.

I have an executable that needs to be compressed and mailed to a usergroup, ONLY if it is newer than say 15 minutes. If older than 15 minutes, the script should just exit, and do nothing.

The Copy/RAR/MailTo function batch files are working, it is the time comparison that I need help with.

ADDED:

First bat file:

cd
cd c:\lighthouse\
mv production.exe production_old.exe /Y
cd\
cd c:\production\backup\
cd
xcopy "c:\production\backup\production.exe" "c:\lighthouse\production.exe" /Y
xcopy "c:\production\backup\production.exe" "i:\production.exe" /Y
xcopy "c:\production\backup\production.exe" "c:\RAR_and_Mail\production.exe" /Y
cd

Second bat file: This needs to ONLY run IF file production.exe is NEWER than 15 minutes, else EXIT.

cd\
cd c:\RAR_and_Mail\
xcopy "c:\RAR_and_Mail\*.rar" "c:\RAR_and_Mail\old\*.rar" /Y
del c:\RAR_and_Mail\*.rar
set MyDate=%date:/=.%.%
set WINRAR=C:\Program Files\WinRAR\rar.exe
set production=c:\RAR_and_mail\
cd /D %production%
"%WINRAR%" a -r -s -m5 /Y /R "%production%\%MyDate%_production.rar" "*.exe"
cd
call c:\batches\SendMail_Executable.bat

the third .bat being called:

echo off
set MyDate=%date:/=.%.%
c:\sendmail\sendEmail -o tls=no -f [email protected] -t [email protected] -s 254.20.10.100:25 -u "Latest work Update" -a "c:\RAR_and_Mail\%MyDate%_production.rar" -m "'Save As' the attachement, go to folder where it was saved, and 'extract here', overwriting existing production.exe."
9
  • Can you post your code? What OS are you using?
    – Burgi
    Apr 21, 2017 at 8:14
  • Hi, thanks for the grammer. MS Server2003 DOS first Batch file : cd cd c:\TheWork\ move RunThis.exe RunThis_old.exe cd\ cd c:\work\backup\ cd xcopy "c:\work\backup\RunThis.exe" "c:\TheWork*.exe" /Y xcopy "c:\work\backup\RunThis.exe" "i:*.exe" /Y cd Apr 21, 2017 at 8:55
  • You should edit your question to include the code. Also I need to point out that Server 2003 is no longer supported by Microsoft and if you are in a corporate environment you should consider an upgrade path to Server 2012 or 2016.
    – Burgi
    Apr 21, 2017 at 9:00
  • 2nd Batch file, runs 5 minutes after first, but should EXIT and do nothing if runthis.exe is newer than 15 min: cd\ cd c:\RAR_and_Mail\ xcopy "c:\RAR_and_Mail*.rar" "c:\RAR_and_Mail\old*.rar" /Y del c:\RAR_and_Mail*.rar set MyDate=%date:/=.%.% set WINRAR=C:\Program Files\WinRAR\rar.exe set tallship=c:\RAR_and_mail\ cd /D %tallship% "%WINRAR%" a -r -s -m5 /Y /R "%tallship%\%MyDate%_runthis.rar" ".exe" rem "%WINRAR%" a -r -s -m5 /Y /R "%tallship%\runthis.rar" ".exe" cd call c:\batches\SendMail_Executable.bat Apr 21, 2017 at 9:09
  • Batch file being called: echo off set MyDate=%date:/=.%.% c:\sendmail\sendEmail -o tls=no -f [email protected] -t [email protected] -s 200.2.1.10:25 -u "Latest tallship Update" -a "c:\RAR_and_Mail\%MyDate%_runthis.rar" -m "'save As' attachement, go to folder, and 'extract here', overwriting existing runthis.exe." Apr 21, 2017 at 9:09

2 Answers 2

0

In powershell

$FilePath = 'production.exe'
$lastWrite = (get-item $FilePath).LastWriteTime
$timespan = new-timespan -minutes 15

if (((get-date) - $lastWrite) -lt $timespan) {
    # do this if file newer
} else {
    # do this if file older
}

"Windows PowerShell® is a task-based command-line shell and scripting language designed especially for system administration. Built on the .NET Framework, Windows PowerShell helps IT professionals and power users control and automate the administration of the Windows operating system and applications that run on Windows.

Windows PowerShell commands, called cmdlets, let you manage the computers from the command line. Windows PowerShell providers let you access data stores, such as the registry and certificate store, as easily as you access the file system. In addition, Windows PowerShell has a rich expression parser and a fully developed scripting language."

Run a command prompt (Start, Run, cmd , then OK) type powershell then hit return. You should then get the PowerShell PS prompt

Powershell is an add on for MS Server 2003 If it hasn't already been installed on your sever and you want to know how to install there is a MS TechNet blog

How to Install PowerShell on Windows Server 2003

save PowerShell scripts with a .ps1 extension

you can call a powershell script from Batch

PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& 'powershellscriptname.ps1'"

and similarly call batch from within Powershell

start-process C:\batch.bat

Giving you a .ps1 file to call something like this

Move-Item c:\lighthouse\production.exe c:\lighthouse\production_old.exe -force -ErrorAction SilentlyContinue
$files = @("c:\lighthouse\production.exe","c:\RAR_and_Mail\production.exe")#,"i:\production.exe")
foreach ($file in $files){
Copy-item c:\production\backup\production.exe $file -force
}
Start-Sleep -s 300
$FilePath = 'c:\lighthouse\production.exe'
$lastWrite = (get-item $FilePath).LastWriteTime
$timespan = new-timespan -minutes 15

if (((get-date) - $lastWrite) -lt $timespan) {
        # do this if file newer
    Copy-item "c:\RAR_and_Mail\*.rar" "c:\RAR_and_Mail\old\*.rar" -force -ErrorAction SilentlyContinue
    Remove-Item "c:\RAR_and_Mail\*.rar"
    $MyDate = get-date
    $Rarfile = "c:\RAR_and_mail\" + "$MyDate" + "_production.rar"
    & "C:\Program Files\WinRAR\rar" a r "$Rarfile" "c:\RAR_and_mail\*.exe"
    start-process c:\batches\SendMail_Executable.bat
    } else {
        # do Nothing, file older.
    }

Or simply add and run this instead

start-process "insert 1st_bat_file path here"
$FilePath = 'production.exe'
$lastWrite = (get-item $FilePath).LastWriteTime
$timespan = new-timespan -minutes 15

if (((get-date) - $lastWrite) -lt $timespan) {
    # do this if file newer
start-process "insert 2nd_bat_file path here"
start-process c:\batches\SendMail_Executable.bat
} else {
    # do this if file older
}
0
  1. BATCH file Run by Scheduler every 15min:

cd cd c:\work\ move workfile.exe workfile_old.exe cd\ cd c:\Snowflake\backup\ cd xcopy "c:\Snowflake\backup\workfile.exe" "c:\work*.exe" /Y xcopy "c:\Snowflake\backup\workfile.exe" "i:*.exe" /Y xcopy "c:\Snowflake\backup\workfile.exe" "c:\RarAndMail*.exe" /Y cd PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& 'c:\batches\workfileAutoMail\Distribute.ps1'"

  1. distribute.ps1 detail

$FilePath = 'c:\RarAndMail\workfile.exe' $lastWrite = (get-item $FilePath).LastWriteTime $timespan = new-timespan -minutes 15

if (((get-date) - $lastWrite) -lt $timespan) { # do this if file newer start-process c:\batches\workfileAutoMail\Mailworkfile.bat } else { # do this if file older }

  1. detail of Mailworkfile.bat

cd\ cd c:\RarAndMail\ xcopy "c:\RarAndMail*.rar" "c:\RarAndMail\old*.rar" /Y set MyDate=%date:/=.%.% set WINRAR=C:\Program Files\WinRAR\rar.exe set Snowflake=c:\RarAndMail\ cd /D %Snowflake% "%WINRAR%" a -r -s -m5 /Y /R "%Snowflake%\%MyDate%_workfile.rar" "*.exe" cd c:\RarAndMail\ set MyDate=%date:/=.%.% c:\sendmail\sendEmail -o tls=no -f [email protected] -t [email protected] -s 205.20.10.100:25 -u "Latest Snowflake Update" -a "c:\RarAndMail\%MyDate%_workfile.rar" -m "'Save As' the attachement, go to folder where it was saved, and 'extract here', overwriting existing workfile.exe." cd\ xcopy "c:\RarAndMail*.rar" "c:\RarAndMail\old*.rar" /Y del c:\RarAndMail*.rar

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .