Solved

Do something only if the variable is set

Posted on 2013-05-23
4
298 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

696 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