1

been working in this batch code for a couple of days and I cannot make it work right. To give you a perspective, this code is intended to read a file that contains some "selected" numbers which refer to other files contained in another folder. These files contain a lot of data arranged by rows and columns. The first rows are not important so the code is skipping 50 rows to save time, then I am interested in the 2nd and 7th columns so these are exported to variables (A,B) for each row. If there is a combination of values between these two variables (e.g. A>1000 & B>0.03) on the same row the code is supposed to export the file number where this happened. Problem: the IFs that compare these columns don't work as expected, the GTR/GEQ don't seem to respect the "more than" and the code exports file numbers even when there is no such combination in the file. I have tried two codes so far:

Code 1

@echo off
setlocal EnableDelayedExpansion
for /f "tokens=*" %%s in (selected.txt) do (
cd %~dp0\Melts\MeltNPTdis-8-%%s
    for /f "skip=50 tokens=2,7 delims= " %%A in (file.txt) do (
        if %%A GTR 1000 if %%B GTR 0.03 (
            cd %~dp0
            >>dummy.txt echo %%s
)))

Code 2, with check variables

@echo off
setlocal EnableDelayedExpansion
for /f "tokens=*" %%s in (selected.txt) do (
cd %~dp0\Melts\MeltNPTdis-8-%%s
    for /f "skip=50 tokens=2,7 delims= " %%A in (file.txt) do (
        set /a x=0
        set /a y=0
        if %%A GTR 999 (
            set /a x=1
            )
        if %%B GTR 0.03 (
            set /a y=1
            )
        set /a z= !x! + !y!
        if !z! EQU 2    (
        cd %~dp0
        >>dummy.txt echo %%s
)))

Can someone light me up on this one? I have isolated the problem to the IFs and have tried to define the variables differently like %%A or %%A%% not solving the issue. Thanks a lot!

2
  • 3
    Numerical operators like +, - but also GTR work only 32 bit signed integers. In your case 0.03 is treated as string and therefore compared with string rules Commented Dec 21, 2021 at 6:12
  • @jeb thank you! I didn't know about this, I guess I better use matlab instead Commented Dec 21, 2021 at 7:07

1 Answer 1

1

You would need to test both sides of the fractional numbers as cmd can only work with 32bit integers. You could however get a little help from powershell to overcome this. Note this is untested as I am on my mobile device and obviously do not have the environment you have to test:

@echo off
for /f "usebackq delims=" %%s in ("selected.txt") do (
(for /f "usebackq skip=50 tokens=2,7 delims= " %%A in ("%~dp0Melts\MeltNPTdis-8-%%s\file.txt") do powershell "if (%%A -gt 0.04) {if (%%A -gt 2) {Write-host "%%s"}}"
   )>"%~dp0dummy.txt"
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.