Tanmay Bhattacharya
Tanmay Bhattacharya

Reputation: 551

Reading Data from a text file using Batch

I am trying to read a text file which contains an integer value [it might be anything between 0-20] , in case the value is 10 ,I want to execute some commands .

  FOR /L %%A IN (1,1,3) DO (
     Set /P Counter <"D:\WeeklyRun\Flag.txt" 
     if%Counter%==10(Run some command)
     if%Counter%!=10(timeout 20)
     )

I tried writing the following code ,but it did not work for me Can someone please tell me what is wrong with the code ?

Upvotes: 1

Views: 74

Answers (1)

peter
peter

Reputation: 42182

You need a space after te if's and before the ( and an = before the redirect < sign, also the != must be

not expression ==

The " around the path must be removed otherwise the counter is not set which causes the error ( is unexpected, in order to cope with that you need to put ' aroung both sides of your camparison.

Here your reworked sample

FOR /L %%A IN (1,1,3) DO (
  Set /p Counter=< c:\WeeklyRun\Flag.txt
  if "%Counter%"=="10" (
    echo Run some command
  ) 
  if not "%Counter%"=="10" (echo timeout 20)
)

Upvotes: 2

Related Questions