Solved

Batch (CMD) file issue within expanded IF construct

Posted on 2013-12-05
2
403 Views
Last Modified: 2013-12-05
Hi,

I have the following method of checking a file version working:
filever.exe /B /A /D "C:\Program Files\Folder\File.exe" | find "1.1.1.1" >nul
If '%errorlevel%'=='0' Goto DoSomething

Open in new window

But it doesn't work if I do it inside an expanded IF block like this:
if exist "C:\Program Files\Folder\File.exe" (
   filever.exe /B /A /D "C:\Program Files\Folder\File.exe" | find "1.1.1.1" >nul
   if '%errorlevel%'=='0' (
      rem Something here
   ) else (
      rem Something here
   )
) else (
   rem Something here
)

Open in new window

It no longer does the version match correctly.  The %errorlevel% is always 0.  
I tried redirecting the output of the filever.exe (without the pipe) to a text file and it was as expected.
I then tried redirecting the output of the find.exe to a text file and there was no output.

It's as though the piping to the FIND command no longer works inside the IF construct.  Any idea why?  Thanks!
0
Comment
Question by:cantoris
2 Comments
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
ID: 39698231
You need to enable "delayed expansion" for that (enter "help set" in a command prompt for details) and then use "!" to expand the variables inside the loop:
setlocal enabledelayedexpansion
if exist "C:\Program Files\Folder\File.exe" (
   filever.exe /B /A /D "C:\Program Files\Folder\File.exe" | find "1.1.1.1" >nul
   if '!errorlevel!'=='0' (
      rem Something here
   ) else (
      rem Something here
   )
) else (
   rem Something here
)

Open in new window

0
 
LVL 16

Author Closing Comment

by:cantoris
ID: 39698368
Perfect, thanks.  Not seen that feature before!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now