Solved

Do something only if the variable is set

Posted on 2013-05-23
4
295 Views
Last Modified: 2013-05-23
I'm running this code in a batch file, and trying to do an inventory.

for %%a in ( C D E F ) do if exist "%%a\Program Files\App Path\MyApp.exe" set drive1=%%a
if not [%drive1%]==[%drive1%] echo MyApp is installed ~ %computername% ~ %DATE:~4,10% ~  %time:~0,8%  > "\\192.168.x.xxx\ShareName\%COMPUTERNAME%_MyAppInstalled.txt"

Open in new window


The app I'm looking for could be on any of 4 drives so what I'm trying to do is see if the app exists. Then if it does I just want to write that to a file. If it doesn't I don't want anything written. I actually don't care about the drive it is on.

The problem is that if the %drive1% it doesn't compare correctly.
0
Comment
Question by:Jim P.
[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
  • 3
4 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 39192763
You can use IF DEFINED to check if a variable exists, or check if it is blank:

IF DEFINED drive1 echo MyApp is installed ~ %computername% ~ %DATE:~4,10% ~  %time:~0,8%  > "\\192.168.x.xxx\ShareName\%COMPUTERNAME%_MyAppInstalled.txt"

or

IF NOT [%drive1%]==[] echo MyApp is installed ~ %computername% ~ %DATE:~4,10% ~  %time:~0,8%  > "\\192.168.x.xxx\ShareName\%COMPUTERNAME%_MyAppInstalled.txt"

hth

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39192771
You could also just do it in your loop through drives rather than setting a variable.  If it was on more than one drive it would write the file each time but that doesn't matter.  If you want to, add %%a on your echo line to get the drive letter it is installed on if you want.

for %%a in ( C D E F ) do if exist "%%a\Program Files\App Path\MyApp.exe" (
  echo MyApp is installed on %%a ~ %computername% ~ %DATE:~4,10% ~  %time:~0,8%  > "\\192.168.x.xxx\ShareName\%COMPUTERNAME%_MyAppInstalled.txt"
)
Steve
0
 
LVL 38

Author Closing Comment

by:Jim P.
ID: 39192815
Just what I needed.

These servers are going away in less than a year and just need an ad hoc inventory for some stuff until then.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39192830
No problem, glad it helped.

Steve
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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