Computer Hope

Microsoft => Microsoft DOS => Topic started by: Blisk on April 17, 2014, 01:01:52 AM

Title: Batch for comparing values
Post by: Blisk on April 17, 2014, 01:01:52 AM
Hi people. I need some help if someone can help me?
I have a txt log file from where I need to take a value and compare it with value from another txt file.
If value from another file is lower than value from first file than it must take an action.

Belov is a log from first txt file. I need to take out value from last line (avg):140.5Kh/s. In second txt file I write my value, like 100

Code: [Select]
[2014-04-17 07:13:00] Accepted 5d447098 Diff 65/32 GPU 0                     
(5s):140.8K (avg):140.4Kh/s | A:32  R:0  HW:0  U:4.9/m  WU:156.9/m          (5s):140.8K (avg):140.4Kh/s | A:32  R:0  HW:0  U:4.8/m  WU:154.9/m           [2014-04-17 07:13:12] Accepted e7790f5c Diff 42/32 GPU 0                     
(5s):140.9K (avg):140.4Kh/s | A:33  R:0  HW:0  U:4.9/m  WU:157.7/m           [2014-04-17 07:13:15] Accepted 79988ae3 Diff 45/32 GPU 0                     
 [2014-04-17 07:13:16] Accepted d68372c1 Diff 74/32 GPU 0                     
(5s):140.9K (avg):140.5Kh/s | A:35  R:0  HW:0  U:5.2/m  WU:165.2/m         
(5s):140.9K (avg):140.5Kh/s | A:35  R:0  HW:0  U:5.1/m  WU:163.2/m          (5s):141.0K (avg):140.5Kh/s | A:35  R:0  HW:0  U:5.0/m  WU:161.3/m     
Title: Re: Batch for comparing values
Post by: foxidrive on April 17, 2014, 01:42:52 AM
Please attach a sample of your text file to a post, and describe the filenames and what you need to do. 

The file data you pasted in looks like it was corrupted.
Title: Re: Batch for comparing values
Post by: Blisk on April 17, 2014, 01:53:51 AM
No it is not corrupted it is that way.


[recovering disk space, attachment deleted by admin]
Title: Re: Batch for comparing values
Post by: foxidrive on April 17, 2014, 02:17:36 AM
Are you sure? The mylog.txt is nothing like what you posted above.

If the figure following (avg) on the last line is what you need to compare, does it have to take the figures after the decimal point into account
or can it just compare the figures to the left of the decimal point?
Title: Re: Batch for comparing values
Post by: Blisk on April 17, 2014, 02:29:54 AM
Yes I am soure that's the way it is :)
yes the figure after avg and it is not important which from last line it can be first or last.
And decimal points also are not important, just the whole number, from (avg):140.5Kh/s just a 140.
and in next file I will write 100 and if value after avg drops under 100 it will run some application
Title: Re: Batch for comparing values
Post by: foxidrive on April 17, 2014, 02:42:54 AM
compare.txt should only contain a whole number
and the code will compare the avg figure in the last line of myfile.log with that number.

If the avg figure is less than the number then it will launch the program listed.

Code: [Select]
@echo off
for /f "usebackq tokens=5  delims=:. " %%a in ("myfile.log") do set avg=%%a
echo avg="%avg%"
for /f "usebackq delims=" %%a in ("compare.txt") do if %avg% LSS %%a start "" "c:\program files\folder\myapp.exe"
pause
Title: Re: Batch for comparing values
Post by: Blisk on April 17, 2014, 03:03:22 AM
wau, this is so great it works :)
it is so simple but so complicated for me because I don 't know how to program.

Need just one more thing here if you are willing to help.

one more file where it is written how many time is runned myapp.exe, so everytime is runned it add 1 more if it is runned 1 time than in file is 1 if it is runned 3 times than it is 3 in that file, etc.

If batch file found out that it is runned for example more than 3 times it runs nextmyapp.exe and not myapp.exe and delete that counter file to zero.

If nextmyapp.exe is runned more than 3 times it stops to run the whole batch file so it won't happend a loop.

Title: Re: Batch for comparing values
Post by: foxidrive on April 17, 2014, 05:05:45 AM
Need just one more thing here if you are willing to help.

I'm glad it helps.

When you ask for free help then you should write the whole task out with as much information that you can provide.

Asking for more changes once you get some tested code that works means even more work for the people helping you,
as they have to change the code that is already written and tested, and then test it again.

Title: Re: Batch for comparing values
Post by: Blisk on April 17, 2014, 05:28:12 AM
sorry about that you are right.
But that crossed my mind after I tested this. I didn't think about the whole process. Sorry again
Title: Re: Batch for comparing values
Post by: Blisk on April 17, 2014, 07:55:53 AM
Can I still hope for help for that?
Title: Re: Batch for comparing values
Post by: foxidrive on April 17, 2014, 08:21:23 AM
This runs myapp 3 times, then runs mynextapp 3 times, then relaunches the batch file.

It only launches the apps if the average figure is less than the figure in compare.txt


Code: [Select]
@echo off
:: initialise the app counter files
if not exist "app1.txt" type nul >"app1.txt"
if not exist "app2.txt" type nul >"app2.txt"
if not exist "compare.txt" echo compare.txt is missing&pause&goto :EOF


:: If app1 has run 3 times then set the variable to app2 (otherwise set it to app1)
find /c "run" <"app1.txt" | find "3" >nul && (set app=app2) || (set app=app1)

:: If app2 has run 3 times then remove the counter files and relaunch the batch file
find /c "run" <"app2.txt" | find "3" >nul && (
   del "app1.txt" "app2.txt"
   %0
)

:: get the average figure from the last line in the file
for /f "usebackq tokens=5  delims=:. " %%a in ("myfile.log") do set avg=%%a
echo avg="%avg%"

:: get the compare number from the file and if avg is lower,
:: increment the counter file and run the app - using the app as defined above.
for /f "usebackq delims=" %%a in ("compare.txt") do if %avg% LSS %%a (
   if "%app%"=="app1" >>"app1.txt" echo run& start "" "c:\program files\folder\myapp.exe"
   if "%app%"=="app2" >>"app2.txt" echo run& start "" "c:\program files\folder\mynextapp.exe"
)
Title: Re: Batch for comparing values
Post by: Blisk on April 17, 2014, 08:29:34 AM
Thank you, you really made my day today, finally works all like I wanted.
Thank you :D
Title: Re: Batch for comparing values
Post by: Blisk on April 18, 2014, 12:22:11 AM
If I can ask for more help here not to opening new topic?

I need one mora batch file for copying the newest file from folder
login to folder test\new
is it possible to have just a part of path in batch not full path?
because I need to make bach fule for each user if it is not possible just part of path.
because full path is
c:\users\mike\software\screener\database\database2014\office\login

c:\users\mike\software\screener\database\database2014\office\test\new
Title: Re: Batch for comparing values
Post by: foxidrive on April 18, 2014, 12:52:09 AM
Open a new topic for different questions.  They are free. :)

If this is run from the login folder then it will copy the newest file to the new folder shown in your path.

Code: [Select]
@echo off
for /f "delims=" %%a in ('dir /b /od /a-d') do set "lastest_file=%%a"
copy "%lastest_file%" "..\test\new"
Title: Re: Batch for comparing values
Post by: Blisk on April 18, 2014, 01:06:55 AM
Didn't know about opening new topic because on some forums doesn't like to have every question new topic.

This batch not working it copy only itself to folder test\new
that also is not ok because there must be only one file

I runned this from \login folder where batch file should be
Title: Re: Batch for comparing values
Post by: foxidrive on April 18, 2014, 01:12:47 AM
This batch not working it copy only itself to folder test\new
that also is not ok because there must be only one file

I runned this from \login folder where batch file should be

hehe  It's working fine.  The batch file is the newest file. :D

Try this:

Code: [Select]
@echo off
for /f "delims=" %%a in ('dir /b /od /a-d ^|find /v /i "%~nx0" ') do set "lastest_file=%%a"
copy "%lastest_file%" "..\test\new"
Title: Re: Batch for comparing values
Post by: Blisk on April 22, 2014, 12:49:26 AM
no still not working I think problem is because bat file is in another parallel folder of test folder
and still copys bat file within this bat file not a log file in paralel folder.
Title: Re: Batch for comparing values
Post by: Squashman on April 22, 2014, 04:40:40 AM
are you using this code within some larger batch file?
Title: Re: Batch for comparing values
Post by: Blisk on April 22, 2014, 04:47:20 AM
are you using this code within some larger batch file?

no 2 batch files are in that folder.
one is above second is this

Code: [Select]
robocopy \\myserver\myfodler\Drivers\conf\log \\myserver\myfodler\Drivers\conf\log\chk /MIR /W:5
exit
Title: Re: Batch for comparing values
Post by: Squashman on April 22, 2014, 06:36:41 AM
I would have assumed as Foxidrive has that you were combining all the code into one batch file.  So basically again, one of your batch files is your newest file so that is the file it is using.  You will probably have to change the code to something like this.

Code: [Select]
@echo off
for /f "delims=" %%a in ('dir /b /od /a-d ^|findstr /V /I  /C:"*.bat" ') do set "lastest_file=%%a"
copy "%lastest_file%" "..\test\new"
Title: Re: Batch for comparing values
Post by: foxidrive on April 22, 2014, 10:36:57 AM
That should work without the *

I just amused myself with the switches in this one:

Code: [Select]
@echo off
for /f "delims=" %%a in ('dir /b /od /a-d ^|findstr /e /v /i /L ".bat" ') do set "lastest_file=%%a"
copy "%lastest_file%" "..\test\new"
Title: Re: Batch for comparing values
Post by: Squashman on April 22, 2014, 11:03:59 AM
I just amused myself with the switches in this one:
  :rofl: >>:
Title: Re: Batch for comparing values
Post by: Blisk on April 23, 2014, 12:36:18 AM
It works but only if I run from the same folder where that newest file is, if I run from this folder where batch should be, than it not working
c:\users\mike\software\screener\database\database2014\office\login

I also notice that sometimes it copy temporary file tmpcopy.log which its maded in some errors, how to exclude to exclude that two files which start with tmp?
tmpcopy.log and tmpfile.txt
Title: Re: Batch for comparing values
Post by: foxidrive on April 23, 2014, 01:34:26 AM
This is a situation where better information about the task would have helped.

As it stands you got code that gives you the newest file, and that is all you asked for.
What files are you checking, for example?
Title: Re: Batch for comparing values
Post by: Blisk on April 23, 2014, 01:45:13 AM
they are log files of succesfully copied files drom disk A o disk B.
If error happends than two extra log files are maded, I forgot for those two as it happends really rare.

I need latest log file to be copied to folder new so some people can check that log file if they need it, which ahve almost none of compuer skills
Title: Re: Batch for comparing values
Post by: foxidrive on April 23, 2014, 02:21:43 AM
Add *.log after the dir command and it will only process .log files.

You can also use the pushd command to make the log folder the working directory.

Code: [Select]
@echo off
pushd "folder\with\log\files"
for /f "delims=" %%a in ('dir *.log /b /od /a-d ') do set "lastest_file=%%a"
copy "%lastest_file%" "..\test\new"
popd
Title: Re: Batch for comparing values
Post by: Blisk on April 23, 2014, 04:15:16 AM
if it is possible withouth that full path of log files I really appreciate that.
Because I need to make differend batch file for every user I have
fro example what i mean.

c:\users\MIKE\software\screener\database\database2014\office\logfiles\monday.txt
Title: Re: Batch for comparing values
Post by: foxidrive on April 23, 2014, 05:23:16 AM
With this batch in the login folder then you can use a relative path to the logfiles folder.


Code: [Select]
@echo off
pushd "..\logfiles"
for /f "delims=" %%a in ('dir /b /od /a-d ') do set "lastest_file=%%a"
copy "%lastest_file%" "..\test\new"
popd
Title: Re: Batch for comparing values
Post by: Salmon Trout on April 23, 2014, 05:47:02 AM
if it is possible withouth that full path of log files I really appreciate that.
Because I need to make differend batch file for every user I have
fro example what i mean.

c:\users\MIKE\software\screener\database\database2014\office\logfiles\monday.txt

Check out the %username% system variable

Title: Re: Batch for comparing values
Post by: Blisk on April 23, 2014, 05:48:23 AM
 foxidrive
you are the best, this really works, thank you. :D



Salmon Trout
I know about  %username%  but it doesn't work if user is not logged in.
Title: Re: Batch for comparing values
Post by: Squashman on April 23, 2014, 06:26:26 AM
foxidrive
you are the best, this really works, thank you. :D



Salmon Trout
I know about  %username%  but it doesn't work if user is not logged in.
Umm, well then they wouldn't be running the batch file then.
Title: Re: Batch for comparing values
Post by: Blisk on April 23, 2014, 06:30:26 AM
system runs batch file and people check log files :)