?
Solved

Do something only if the variable is set

Posted on 2013-05-23
4
Medium Priority
?
300 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 2000 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

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

752 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