Solved

Do something only if the variable is set

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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.

821 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