Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check "IF" condition inside FOR loop (batch/cmd)

Tags:

batch-file

The code I need to implement in a Windows batch file is like this (it is currently in Perl):

while(<file>)
{
   if($_ =~ m/xxxx/)
   {
      print OUT "xxxx is found";
   }
   elsif($_ =~ m/yyyy/)
   {
      next;
   }
   else
   {
      ($a,$b) = split(/:/,$_);
      $array1[$count] = $a;
      $array2[$count] = $b;
      $count++;
   }
}

My questions are:

  1. Is this level of complexity possible in Windows batch files?
  2. If so, how can I put an If condition inside a for loop to read a text file?

Thanks for your attention. If you know the answers, or have any ideas/clues on how to reach the answer, please share them.

EDIT: I am working in Windows. I can use only whatever is provided with Windows by default and that means I cant use Unix utilities.

like image 312
Anna Avatar asked Jul 22 '09 08:07

Anna


People also ask

What is == in batch file?

[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.

How do I check the condition of a batch file?

To echo the value of the ERRORLEVEL environment variable after running a batch file, type the following lines in the batch file: goto answer%errorlevel% :answer1 echo The program returned error level 1 goto end :answer0 echo The program returned error level 0 goto end :end echo Done!

Can I use for loop in CMD?

FOR /F. Loop command: against the results of another command. FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'. The DO command is then executed with the parameter(s) set to the token(s) found.


2 Answers

Putting if into for in general is easy:

for ... do (
    if ... (
        ...
    ) else if ... (
        ...
    ) else (
        ...
    )
)

A for loop that iterates over lines can be written using /f switch:

for /f "delims=" %%s in (*.txt) do (
    ...
)

Regexps are provided by findstr. It will match against stdin if no input file is provided. You can redirect output to NUL so that it doesn't display the found string, and just use its errorlevel to see if it matched or not (0 means match, non-0 means it didn't). And you can split a string using /f again. So:

set count=0
for /f "delims=" %%s in (foo.txt) do (
    echo %%s | findstr /r xxxx > NUL
    if errorlevel 1 (
        rem ~~~ Didn't match xxxx ~~~
        echo %%s | findstr /r yyyy > NUL
        if errorlevel 1 (
            rem ~~~ Didn't match yyy ~~~
            for /f "delims=; tokens=1,*" %%a in ('echo %%s') do (
                 set array1[!count!]=%%a
                 set array2[!count!]=%%b
                 set /a count+=1
            )
        )
    ) else (
        echo XXX is found
    )
)
like image 194
Pavel Minaev Avatar answered Sep 28 '22 05:09

Pavel Minaev


I think this is too much of a pain to do in CMD.EXE, and even outright impossible, though I may be mistaken on the last one.

You'd better off using WSH (Windows Scripting Host), which allows you to use JScript or VbScript, and is present in pretty much every system. (and you can provide a redistributable if you want)

like image 20
EFraim Avatar answered Sep 28 '22 03:09

EFraim