Using a Variable from inside a for statement

Microsoft Upg-vc msdos 6.22
October 20, 2011 at 23:39:48
Specs: Windows Vista, 7Gbyte Ram / Core 2 Duo 3Ghz
My problem is that %CompID1% and %CompID2% cannot be used outside the For Loops i've created.
Is there any way i can use them outside the For loop as well? I need to compare the 2 %CompID1% and %CompID2% results and appropriately set a status on whether they are the same or different.

set moar1=0
for /f %%a in (C:\Bulker\temp\TradeVolume%moar1%.txt) do (set CompID1%moar1%=%%a)

for /f %%b in (C:\Bulker\temp\FirstPumpVolume%moar1%.txt) do (set CompID2%moar1%=%%b)

if %CompID1% equ %CompID2% (set tradeid=OK) ELSE set tradeid=Different
echo %tradeid%


set /A moar=%moar1%+1


See More: Using a Variable from inside a for statement

Report •

#1
October 21, 2011 at 00:12:47
To expand environment variables inside a For loop enable delayed expansion e.g.

    Setlocal Enabledelayedexpansion
    set moar1=0

    for /f %%a in (C:\Bulker\temp\TradeVolume!moar1!.txt) do (set CompID1!moar1!=%%a)

    for /f %%b in (C:\Bulker\temp\FirstPumpVolume!moar1!.txt) do (set CompID2!moar1!=%%b)

    if %CompID1% equ %CompID2% (set tradeid=OK) ELSE set tradeid=Different
    echo %tradeid%


    set /A moar=%moar1%+1

But note that you cannot compare CompID1 with CompID2 as these environment variables have not been set. What have been set are CompID1%Moar1% (CompID10) and CompID2%Moar1% (CompID20)

Also note that the script as presented will only compare the first word in the final line of each input file, none of the others are compared. Hope this is what you want.

Good luck


Please come back & tell us if your problem is resolved.


Report •

#2
October 21, 2011 at 02:39:28
This doesn't work for what i'm trying to do...

Basically i have a server log with lots of the following lines:
trade|66|2|0 |EURUSD|1000|1.4000|171|6|0||$
trade|62|3|0 |USDJPY|100000|1.6000|176|3|0||$

I take tokens 5,6,7,8,9 (EURUSD, 1000, 1.4000, 171, 6) and put each one in it's own separate text file.
So i then have a a file called: Currency1.txt to store value EURUSD and nother called Currency 2.txt to store USDJPY and so on for all server log lines, i also have other text files with other names with incremental numbering for each server log line doing the same thing.

So now i'm trying to read Currency1.txt and Currency2.txt and compare their 2 values EURUSD and USDJPY and see whether they are the same or not.
Then i want to increment the filename so i can compare Currency2.txt and Currency3.txt and so on....

Understand my problem?


Report •

#3
October 21, 2011 at 20:22:12
Understand my problem? Perhaps, so here's my take on it. You have already created files Currency#.txt where # is a number beginning with 1 and incrementing by 1 to the max number of files. Each file contains just one line consisting of either EURUSD or USDJPY.

You wish to compare the content of each file with the content of the next file in sequence so that the content of file1 is compared to the content of file2, file2 is compared with file3 etc.... thru' the maximum number of files which is not determined.

Test this:

@echo off
cls
Setlocal 

pushd path\to\files\   &:: Change the path to suit your setup...

set a=1
set b=2

:loop
if not exist currency%b%.txt (echo end of files.)&exit /b
set /p inone=<currency%a%.txt
set /p intwo=<currency%b%.txt

if %inone% neq %intwo% (set Tradeid=Different
   ) else (
   set Tradeid=OK
)
echo %Tradeid%

set /a a+=1
set /a b+=1
goto loop


Please come back & tell us if your problem is resolved.


Report •
Related Solutions


Ask Question