if Find higher Number then write it to file B

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

if Find higher Number then write it to file B

#1 Post by mohdfraz » 18 May 2012 09:28

Hi,

I have a abc txt file this file is updated ever second with a number like -10000 or 876374

I want a batch file to monitor this file and if it found a new higher number in abc.txt file as compare to xyz.txt file then write that new higher number to xyz.txt file and if again found higher number then rewrite the xyz.txt file with higher number and keep monitoring the file until batch run is stopped.

How I can do that.

Thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: if Find higher Number then write it to file B

#2 Post by foxidrive » 18 May 2012 09:46

Untested: The number has to be less than 2^31 -1

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (' type "xyz.txt"') do set num=%%a

:loop
for /f "delims=" %%a in (' type "abc.txt"') do (
if %%a GTR !num! (
echo new number %%a
set num=%%a
>"xyz.txt" echo %%a
)
)
goto :loop

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: if Find higher Number then write it to file B

#3 Post by Squashman » 18 May 2012 11:11

Would there be any drawback to just using SET /P redirection?

Code: Select all

@echo off
:loop
set /p abc=<abc.txt
set /p xyz=<xyz.txt

IF %abc% GTR %xyz% >xyz.txt echo %abc%
goto :loop

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: if Find higher Number then write it to file B

#4 Post by foxidrive » 18 May 2012 21:12

That would work fine too. :thumbsup:

mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

Re: if Find higher Number then write it to file B

#5 Post by mohdfraz » 19 May 2012 05:45

foxidrive wrote:Untested: The number has to be less than 2^31 -1

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (' type "xyz.txt"') do set num=%%a

:loop
for /f "delims=" %%a in (' type "abc.txt"') do (
if %%a GTR !num! (
echo new number %%a
set num=%%a
>"xyz.txt" echo %%a
)
)
goto :loop



Foxidrive, Many thanks it worked like a charm. Thank you very much.





Would there be any drawback to just using SET /P redirection?

Code: Select all

@echo off
:loop
set /p abc=<abc.txt
set /p xyz=<xyz.txt

IF %abc% GTR %xyz% >xyz.txt echo %abc%
goto :loop


Squashman, I tried ur code first but it did not worked. But thanks for your time and reply.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: if Find higher Number then write it to file B

#6 Post by Squashman » 19 May 2012 10:00

Well I am guilty for not testing it. Most of the time I am posting code from my phone. But the foundation of the code should work.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: if Find higher Number then write it to file B

#7 Post by foxidrive » 19 May 2012 10:13

It does work, if abc.txt and xyz.txt exist with numbers in them.

This is the same code with a small modification to print to the screen.

Code: Select all

@echo off
:loop
set /p abc=<abc.txt
set /p xyz=<xyz.txt
IF %abc% GTR %xyz% (
echo new number %abc%
>xyz.txt echo %abc%
)
goto :loop

mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

Re: if Find higher Number then write it to file B

#8 Post by mohdfraz » 19 May 2012 13:25

foxidrive wrote:It does work, if abc.txt and xyz.txt exist with numbers in them.

This is the same code with a small modification to print to the screen.

Code: Select all

@echo off
:loop
set /p abc=<abc.txt
set /p xyz=<xyz.txt
IF %abc% GTR %xyz% (
echo new number %abc%
>xyz.txt echo %abc%
)
goto :loop


Yes, it is working as well thanks. I found the issue the abc.txt file receive enteries all the time so ur first code work in that situation. This code works if there is one entry. So yes code works as well



Squashman wrote:Well I am guilty for not testing it. Most of the time I am posting code from my phone. But the foundation of the code should work.

As i described above, I found ur code works as well if the abc.txt file got 1 entry but my since my file kept receiving enteries so the foxidrive code works great. I m very surprise to hear that u send the code from mobile. U must be specialiest in this field. Great full for both you. thank you once again.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: if Find higher Number then write it to file B

#9 Post by Squashman » 19 May 2012 16:25

So you are basically saying that abc.txt is getting the next number appended to the end of the file? If that is the case then yes you would need to use a for loop like Foxidrive posted.

But eventually your abc.txt file is going to get quite large if it is being updated every few seconds and that is going to slow down your batch file throughout the day.

mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

Re: if Find higher Number then write it to file B

#10 Post by mohdfraz » 20 May 2012 01:03

Squashman wrote:So you are basically saying that abc.txt is getting the next number appended to the end of the file? If that is the case then yes you would need to use a for loop like Foxidrive posted.

But eventually your abc.txt file is going to get quite large if it is being updated every few seconds and that is going to slow down your batch file throughout the day.


Yes.

yes. I m thinking to add a command in foxidrive to delete and re-make new abc.txt file once it reach 1000 lines.

Post Reply