Solved

Batch (CMD) file issue within expanded IF construct

Posted on 2013-12-05
2
407 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 83

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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