if Find higher Number then write it to file B
Moderator: DosItHelp
if Find higher Number then write it to file B
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
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
Re: if Find higher Number then write it to file B
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
Re: if Find higher Number then write it to file B
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
Re: if Find higher Number then write it to file B
That would work fine too. :thumbsup:
Re: if Find higher Number then write it to file B
foxidrive wrote:Untested: The number has to be less than 2^31 -1Code: 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.
Re: if Find higher Number then write it to file B
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.
Re: if Find higher Number then write it to file B
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.
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
Re: if Find higher Number then write it to file B
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.
Re: if Find higher Number then write it to file B
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.
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.
Re: if Find higher Number then write it to file B
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.