?
Solved

Batch (CMD) file issue within expanded IF construct

Posted on 2013-12-05
2
Medium Priority
?
411 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Sometimes people don't understand why download speed shows differently for Windows than Linux.Specially, this article covers and shows the solution for throughput difference for Windows than a Linux machine. For this, I arranged a test scenario.I…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Suggested Courses

718 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