Solved

Do something only if the variable is set

Posted on 2013-05-23
4
261 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.
  • 3
4 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
Comment Utility
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
Comment Utility
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.
Comment Utility
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
Comment Utility
No problem, glad it helped.

Steve
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
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 gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

7 Experts available now in Live!

Get 1:1 Help Now