Help in subtracting time in batch file

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Help in subtracting time in batch file

Post by MigrationUser »

06 Jun 2011 06:14
karnash


Hi,

I have a batchfile where I copy few files from one server to another.

The files range from 1M to 10G and

Code: Select all

echo "Now starting to copy 10M file from source" >> C:\scripts\logs\network_performance_test_%date%.txt
echo "======================================================================" >> C:\scripts\logs\network_performance_test_%date%.txt
echo "Starting time is " %time%  >> C:\scripts\logs\network_performance_test_%date%.txt
robocopy  \\source\network_test_files\10M E:\network_perf_test\copy_from_source_%date%\10M /ETA /TEE /LOG+:C:\scripts\logs\copy_10M_file_%date%.txt
echo "Finishing time is" %time% >> C:\scripts\logs\network_performance_test_%date%.txt
echo "======================================================================" >> C:\scripts\logs\network_performance_test_%date%.txt

echo "Now starting to copy 50M file from source" >> C:\scripts\logs\network_performance_test_%date%.txt
echo "======================================================================" >> C:\scripts\logs\network_performance_test_%date%.txt
echo "Starting time is " %time%  >> C:\scripts\logs\network_performance_test_%date%.txt
robocopy  \\source\network_test_files\50M E:\network_perf_test\copy_from_source_%date%\50M /ETA /TEE /LOG+:C:\scripts\logs\copy_50M_file_%date%.txt
echo "Finishing time is" %time% >> C:\scripts\logs\network_performance_test_%date%.txt
echo "======================================================================" >> C:\scripts\logs\network_performance_test_%date%.txt

echo "Now starting to copy 500M file from source" >> C:\scripts\logs\network_performance_test_%date%.txt
echo "======================================================================" >> C:\scripts\logs\network_performance_test_%date%.txt
echo "Starting time is " %time%  >> C:\scripts\logs\network_performance_test_%date%.txt
robocopy  \\source\network_test_files\500M E:\network_perf_test\copy_from_source_%date%\500M /ETA /TEE /LOG+:C:\scripts\logs\copy_500M_file_%date%.txt
echo "Finishing time is" %time% >> C:\scripts\logs\network_performance_test_%date%.txt
echo "======================================================================" >> C:\scripts\logs\network_performance_test_%date%.txt
so the output will be
"======================================================================"
"Now starting to copy 50M file from source"
"======================================================================"
"Starting time is " 12:14:09.89
"Finishing time is" 12:14:37.45
"======================================================================"
"Now starting to copy 500M file from source"
"======================================================================"
"Starting time is " 12:14:37.48
"======================================================================"
"Now starting to copy 1G file from source"
"======================================================================"
"Starting time is " 12:19:12.91
"Finishing time is" 12:28:43.96
"======================================================================"
I set the variables in the batch file like this

set var1=%time:~0,8%
echo %var1%

set var2=%time:~0,8%
echo %var2%

echo %var1%,%var2%
set /a total_time - = %var2% - %var1%

but I get Missing operand. I need help to calculate the time taken to copy the file. so all I want is finishing_time-starting_time in seconds.

can someone please help !!! many thanks

----------------------------

#2 06 Jun 2011 13:28
bluesxman


The error you see if because your syntax is wrong ("set /a total_time - = %var2% - %var1%" should read "set /a total_time=%var2% - %var1%").

But even with that corrected, your fatal flaw is assuming that CMD knows how to perform calculations on time values (IE in hh:mm:ss format) -- it does not -- so it still won't work.

The best bet would be to convert the two times into seconds, do the subtraction and convert back to hh:mm:ss. You also need to take into account when tasks cross midnight, as that will give you erroneous values.

Last edited by bluesxman (06 Jun 2011 13:30)

cmd | *sh | ruby | chef

----------------------------

#3 07 Jun 2011 05:16
karnash


ok I think its working now...many many thanks...

----------------------------

#4 07 Jun 2011 06:29
karnash


I did as you suggested, changed everything to seconds and then did the math

----------------------------

#5 13 Jul 2011 17:46
flabdablet


Be very careful when changing the time to seconds: if all you're doing is chopping a HH:MM:SS value into separate HH MM and SS strings and then trying to do arithmetic on them, you will run into a leading-zero gotcha. When cmd interprets strings as numbers, it does so with C syntax: numbers with a leading 0x are hexadecimal, and numbers with just a leading 0 are octal. HH, MM or SS values from 00 through 07 will work fine, but 08 and 09 are illegal octal values and cmd will complain about them.

So when you're chopping up your time components, you actually need to test for leading zeroes and remove them, or test explicitly for values 08 and 09 and replace those with 8 and 9 respectively.

Yes, this does suck.

----------------------------

#6 13 Jul 2011 19:05
Simon Sheppard


This script may help:
https://ss64.com/nt/syntax-tdiff.html
Post Reply