Check Installed program with batch script - batch-file

I hope someone can help me with this.
Basically I want to add this to my login script. The batch script needs to check if McAfee agent5 is installed. If its not exit. If it is installed to run the force install form the network share.
I have used the location where agent5 gets installed. I am not sure if there is a way to check the installed version instead?
The reason I cannot do this from EPO is I cant downgrade? Agent5 blocks it, unfortunately.
This is what I have but it does not work? What am I doing wrong here?
IF EXIST "C:\Program Files (x86)\McAfee\Common Framework\CmdAgent.exe"
GOTO :eof
ELSE msiexec /i "\\192.168.55.125\McAfee Agent\FramePkg4.8.exe /install=agent /forceinstall" /s
:eof
END && EXIT

set filepath=%~f1
set file=%filepath:\=\\%
wmic datafile where name^="%file%" get version|findstr /i /v /c:"version"
File paths must be \\ not \ as normal. The second line doubles backslashes.
Use a for loop to get in a variable.
for /f "skip=1" %A in ('wmic datafile where name^="C:\\windows\\notepad.exe" get version') do Echo %A

Related

Am I the only one who have this problem: While running something like .bat, the "X:\..\..path" often becomes ""X:\..\path and producing errors?

While running something like .bat, the "X:\..\..path" often becomes ""X:\..\path and producing errors. For example, I was installing apktool, then it just appeared this:
'""C:\Program' is not recognized as an internal or external command,
operable program or batch file.
I then copy the command and put one of the double quote to the end, which is like this: "C:\Program"
And everything just went smoothly, installation was successful. Then I tried to decode an apk, and the exactly same problem occurred: '""C:\Program' is not recognized as an internal or external command, operable program or batch file. This time I have no idea how to fix it, it's not like the .bat now, I cannot get the #echo on and copy the last command and edit it. So I am here to ask: If I am the only one who met this? Any way to fix this? Thank you.
My command:
apktool d test.apk
Image of running a decode command : 1
apktool.bat content:
#echo off
setlocal
set BASENAME=apktool_
chcp 65001 2>nul >nul
set java_exe=java.exe
if defined JAVA_HOME (
set java_exe="%JAVA_HOME%\bin\java.exe"
)
rem Find the highest version .jar available in the same directory as the script
setlocal EnableDelayedExpansion
pushd "%~dp0"
if exist apktool.jar (
set BASENAME=apktool
goto skipversioned
)
set max=0
for /f "tokens=1* delims=-_.0" %%A in ('dir /b /a-d %BASENAME%*.jar') do if %%~B gtr !max! set max=%%~nB
:skipversioned
popd
setlocal DisableDelayedExpansion
rem Find out if the commandline is a parameterless .jar or directory, for fast unpack/repack
if "%~1"=="" goto load
if not "%~2"=="" goto load
set ATTR=%~a1
if "%ATTR:~0,1%"=="d" (
rem Directory, rebuild
set fastCommand=b
)
if "%ATTR:~0,1%"=="-" if "%~x1"==".apk" (
rem APK file, unpack
set fastCommand=d
)
:load
"%java_exe%" -jar -Duser.language=en -Dfile.encoding=UTF8 "%~dp0%BASENAME%%max%.jar" %fastCommand% %*
rem Pause when ran non interactively
for /f "tokens=2" %%# in ("%cmdcmdline%") do if /i "%%#" equ "/c" pause
Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign a terminal \, Space or " - build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntax set var="value" is used, then the quotes become part of the value assigned.
set java_exe="%JAVA_HOME%\bin\java.exe"
Should be
set "java_exe=%JAVA_HOME%\bin\java.exe"
(apply this principle throughout your code)
Then, if you require " anywhere, insert it where it's needed - don't try to include it as part of a variable's value.
This should clean up at least some of your problems.

Windows FOR command not expanding system environment variables

I have been using this site for sometime and have found it quite valuable in finding solutions to scripting issues. Today I resolve to ask a question as I have not found my solution after exhausting 3 days here, on Superuser, and Googling. So much for all about me, now my point...
I have several installs of a program, each slightly different version or flavor. Each install has a text file defining variables for its use. One of these variables defines a folder for user specific settings, USERSET=%APPDATA%\foo1\foo2. Overtime these user settings can become corrupt. I want to be able to delete these folders with a batch file or script. In short, I want to search a directory for text files that contain a specific string which sets a variable that points to a folder, get the value of that variable and use it to remove a directory.
This is what I have so far:
for /f "tokens=2 delims==" %%G in ('dir /b /s c:\foo\*.txt ^| findstr /i /f:/ userset') do rd /s /q %%G
The output looks like this:
H:\>rd /s /q %appdata%\foo1\foo2\USERSET
The system cannot find the file specified.
Apparently %appdata% does not expand?
Of course if I just type rd /s /q %appdata%\foo1\foo2\USERSET at the command prompt, it works.
I'm open to other languages, I am mostly familiar with Windows command line.
This is because environment variable expansion is done at parse time of a statement. While your for loop is running and %%G actually gets a value it's too late; variables have already been expanded.
You can work around this with a subroutine, I guess:
pushd C:\foo
for /f ... %%G in (...) do call :process "%%G"
popd
goto :eof
:process
call set "X=%~1"
rd /s /q "%X%"
goto :eof
I'd simply insert a CALL before the RD
....CALL RD ...
although I'd also try
....CALL ECHO(RD ...
first to verify - just for safety's sake.

Locate/Find .exe file (javaw.exe) then use elevate.exe and run jar

I tried to make the title as descriptive as possible, but im basically trying to locate javaw.exe because when I launch my JAR cmd only finds java.exe but the prompt window is ugly and I don't want it there.
What works:
java -jar myJar.jar
What I'm trying to do
javaw -jar myJar.jar
What also works:
JAVAWPATH\javaw.exe -jar myJar.jar
I'm trying to make the program adapt to any computer automatically, since I don't know if javaw will always be in the same location.
So I have managed to locate a bunch of "javaw.exe" files using
WHERE /R c:\ *javaw.exe
I want to select one of the returned paths and set some kind of string variable with which I can then: elevate MYstringPATHtoJAVAW -jar myJar.jar.
I've written a batch script which locates the java instalation folder through the registry:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#ECHO OFF
:: Export java settings from registry to a temporary file
START /W REGEDIT /E %Temp%.\java.reg "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft"
:: Find java location
FOR /F "tokens=1* delims==" %%A IN ('TYPE %Temp%.\java.reg ^| FIND "INSTALLDIR"') DO SET JAVA_HOME=%%B
SET JAVA_HOME=%JAVA_HOME:"=%
SET JAVA_HOME=%JAVA_HOME:\\=\%
SET JAVA_HOME
:: Get java version
FOR /F "tokens=1* delims==" %%A IN ('TYPE %Temp%.\java.reg ^| FIND "CurrentVersion"') DO SET JAVA_VERSION=%%B
SET JAVA_VERSION=%JAVA_VERSION:"=%
SET JAVA_VERSION
SET JAVA_VERSION=%JAVA_VERSION:.=%
SET JAVA_VERSION=%JAVA_VERSION:_=%
SET /A JAVA_VERSION=%JAVA_VERSION%
:: Delete temp file
#DEL %Temp%.\java.reg /S /Q > NUL 2>&1
:: Check java version compatibility
IF %JAVA_VERSION% LSS 16020 (
ECHO.
ECHO YOU NEED AT LEAST JAVA WITH VERSION 1.6.0_20 -- this is just an example echo.
GOTO :EOF
)
PAUSE
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
I've used this as a base.But it might not work properly if the installed java is a 64bit (yes, I must update this script...) , but it can be updated easy.Hope this will help you.

Return Error Message from START /WAIT Command calling DTUTIL

All,
I am using a batch file to automate the deployment of SSIS packages out to MSDB. I am using a START /WAIT command in a FOR loop to iterate through all files in a folder and execute the DTUTIL command.
While I know I can return the %ERRORLEVEL% and use it in the main batch process, what I am losing is the specific error message that DTUTIL outputs.
I want to be able to capture that message output, but I have yet to find a way to. I tried chaining commands by using the &&, || and & concatenation commands. And I also tried to use > and 2> to put the output into a file. Nothing has given me the desires output.
And to be specific, I'm mainly concerned with when DTUTIL ERRORS and the message is more relevant than the error code alone.
Any help or recommendations would be appreciated. Here's the code:
#echo off
SET /P SOURCEFOLDER=Enter the Source Folder where the Packages are located:
SET /P DESTSERVER=Enter the Destination Server:
SET /P DESTLOCATION=Enter Destination Folder (Leave blank for root):
IF NOT EXIST "%SOURCEFOLDER%\*.dtsx" GOTO NOFILES
FOR /F "usebackq delims==" %%i in (`dir /b "%SOURCEFOLDER%\*.dtsx"`) do (
ECHO.
ECHO ^>^>^>^>^>^> Copying %%i to %DESTSERVER%\MSDB\%DESTLOCATION% ^<^<^<^<^<^<^<
ECHO.
START /WAIT dtutil /FILE "%SOURCEFOLDER%\%%i" /DESTSERVER %DESTSERVER% /COPY SQL;"%DESTLOCATION%\%%~ni" /QUIET
ECHO %ERRORLEVEL%
ECHO.
)
GOTO COMPLETE
:NOFILES
ECHO.
ECHO *** No SSIS Packages were found. Please verify the folder location:
ECHO %SOURCEFOLDER%
ECHO.
GOTO END
:COMPLETE
ECHO.
ECHO All Packages have been Processed.
ECHO Please verify that no Error Messages or Warnings were returned.
ECHO.
GOTO END
:END
PAUSE
Simplest would be to drop start and just issue Dtutil (since you wait till it finishes I cannot see a difference). Another two possibilites
start /B dtutil >result.txt
or
start cmd /c dtutil ^>result.txt
(^ is used to escape special chars, here redirection symbol >) (and you already use it...)
Redirect stderr would be 2>result.txt, both stdout and strerr >result.txt 2>&1 (escaped if used with cmd /c: ^>result.txt 2^>^&1)

How to format my batch file correctly

I've been at this for a few hours now. I'm finally giving in and asking for help. What I'm trying to do is use a batch file to get my Truecrypt volume mounted, then open Dropbox. When both of these things are done, executing the batch file the second time will exit Dropbox, then dismount the Truecrypt volume. Everything works correctly except for the exiting of Dropbox. I can't for the life of me, get it to exit Dropbox then dismount the volume. It skips right over the taskkill and just dismounts the volume. I've tried it a variety of different ways which should be working, but for some reason are not. The command window is saying taskkill isn't recognized as a command, but that's obviously bull. =\
#echo off
Set Drive=Z
Set Path="C:\Users\DK\AppData\Roaming\Dropbox.bak"
Set TrueCrypt="C:\Program Files\TrueCrypt\TrueCrypt.exe"
IF EXIST %Drive%: GoTo Dismount
GOTO Mount
:Dismount
taskkill /F /IM "dropbox.exe"
%TrueCrypt% /d %Drive% /f /w /q /s
exit
:Mount
%TrueCrypt% /v /l %Drive% %Path% /q
echo Waiting for volume...
:keepwaiting
ping -n 1 127.0.0.1 > nul
if not exist Z:\ goto keepwaiting
start "Dropbox" "C:\Users\DK\AppData\Roaming\Dropbox\bin\Dropbox.exe"
exit
Never use Path as your own variable! Change Set Path="C:\Users\DK\AppData\Roaming\Dropbox.bak" to something like Set DropboxPath="C:\Users\DK\AppData\Roaming\Dropbox.bak"
Taskkill.exe is in system32 but windows can only find it if it is listed in %Path%.
Tips:
It is probably a good idea to call setlocal as the second line in your script so your variables don't leak out of the batch file.
Never set path, pathext, temp, tmp, windir, SystemRoot, SystemDrive, OS, PROCESSOR_* or NUMBER_OF_* unless you really know what you are doing.
Use exit /B or goto :EOF instead of exit if you don't want to close a console window while you are testing...

Resources