Start a command and if it doesn't work start another - windows

I want to start acrobat from my software.
At this moment I'm using the following command:
cmd /c start AcroRd32.exe /t filename
But now acrobat sometimes updates to 64 bit version and then AcroRd32.exe doesn't exists anymore.
So we have to start acrobat.exe instead:
cmd /c start acrobat.exe /t filename
But we are working with a lot of clients with different computers and maybe different versions of acrobat. That is also why we don't specify an install path.
So is there a way to say in one command line entry (not a script) to start AcroRd32.exe and if this doesn't work start immediately acrobat.exe instead?
So something like:
cmd /c start AcroRd32.exe /t filename | if not 1 is ok then cmd /c start acrobat.exe /t filename

#ECHO OFF
SETLOCAL
FOR /f "delims=" %%b IN ('where autoruns64.exe autorunsc64.exe 2^>nul') DO CMD /c START "" "%%b" &GOTO done
ECHO NOT found!
:done
GOTO :eof
Note: START command : extra pair of quotes. This sets the title of the STARTed session, otherwise the first "quoted string" is used.
"%%b" is used in case the path to the executable (I used autoruns) contains a space.
The first argument to where is the preferred target.
where examines the path for filenames matching its arguments and lists them. The for /f reads the list and on the first match found, executes the START. If no match is found,an error message is produced by WHERE, which is suppressed by the 2^>nul.
Edit: one-line version
FOR /f "delims=" %b IN ('where autoruns64.exe autorunsc64.exe 2^>nul') DO CMD /c START "" "%b" &exit
Posted as a batch file because it's easier to re-run during testing.
WHERE locates files on the PATH and lists them
START locates files on the PATH and executes the first one found.
The PATH is the list of directories that will be examined by START, in the order in which they will be searched for the required executable, so WHERE examines that list of directories for the target executable, it does not search all of the drives in the hope of finding it.

Traditionally even on 64bit it was still Acrord32 for the free reader to avoid such dual name problems / complication and the 32bit or 64bit full product was start Acrobat.exe
However Adobe did not help by using version specifics within the registry so a query there would generally produce differing results ! Microsoft start adding enhancements as to where or how simple launchers may be deployed so the location now gets cloudier (pun :-)
However the traditional ask the registry works best except for my portable
version (where /r h:\ acrord32.exe is very slow on a TB drive and it was not on that one anyway, it was on a network drive!).
Simplest way I can think of via cmd to find an installed copy is:-
cmd /v:on /c "where acro*.exe>temp.txt&&set /p acroexe=<temp.txt&&if exist !acroexe! (!acroexe! /t %filename%)"
there is little use of errorlevel since the fail will simply report
> INFO: Could not find files for the given pattern(s).
Priority will be the first match within %path%
You also need to consider where temp.txt will be written perhaps "%tmp%\delme.txt"
Potential "Gotcha" is if first match finds something like AcrobatUpdater.exe
To avoid random acro's best use
cmd /v:on /c "where /f acrord32.exe acrobat.exe>temp.txt&&set /p acroexe=<temp.txt&&if exist !acroexe! (!acroexe! /t %filename%)"
actually since all you desire is start one or the other then
start "Reader" acrord32.exe /t "filename.pdf" "printer name"||start "Editor" acrobat.exe /t "filename.pdf" "printer name"
The downside to this last method is Windows will raise a GUI error if acrord32 cannot "start" Thus gui warning needs to be dismissed before the second option can be run and start. In that case START cannot be told to be silent on error.
Finally make your choice from above or use a hybrid approach
where /f acrord32.exe... || start acrobat.exe.. will not warn user on first fail but only on second if both missing.

Related

How to run all exe file from one folder/subdolders one by one

I am currently using the following command from this thread:
for /r "\VC++ Redist" %%a in (*.exe) do start "" "%%~fa"
and tried this command from this thread
for %%f in (*.exe) do start /wait %%f
but whenever I use the command, the error pops up.
Windows cannot find specified folder. Make sure you typed the name
correctly, and then try again.
EDIT:
I just solved my question using this command:
for /r "\VC++ Redist" %%a in (*.exe) do %%~fa
Just to add an answer so people knows the question is solved.
for /r "\VC++ Redist" %%a in (*.exe) do %%~fa
This works, but I'd prefer using start. Using start gives you the following abilities:
Window title
Window size
Process priority
Process affinity
WAIT function
NODE control
Another thing is quote - I'm not sure if your VC++ Redist's exe files come with space or not, so as a precaution, I would do:
"%%~fa"
This ensures safety to files with space(this is not the safest method, yet.)

How to find an MSI file and launch installation from command line?

Goal: I want to use CMD.EXE to find a single MSI, located in C:\ProgramData - not elsewhere - and then execute it.
My attempt: dir /s /b C:\programdata\*"my program"*.msi | explorer
Problem: Explorer opens but doesn't launch my MSI.
Constraints: I can't write a .BAT. So this must run on the command line.
Although that doesn't surprise me, I apparently don't understand CMD.EXE and piping well enough to do this. Any guidance?
A *.msi file is not an executable. It is a compiled installer script file which needs an interpreter for execution. The interpreter is msiexec.exe.
Searching for a file can be done with command DIR or with command FOR.
The better solution using command FOR:
for /R C:\ProgramData %# in ("my program*.msi") do %SystemRoot%\System32\msiexec.exe /i "%#"
The more complicated solution using the commands DIR and FOR:
for /F "delims=" %# in ('dir /A-D /B /S "C:\ProgramData\my program*.msi" 2^>nul') do %SystemRoot%\System32\msiexec.exe /i "%#"
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
for /?
msiexec /?
Note: %%# instead of %# would be needed if one of the two command lines is used within a batch file.

Execute Batch File With Different Process Name

I have 6 different batch scripts that I am running together at the same time. The problem is, it is difficult to differentiate between them in the Windows Task Manager because the process is always just cmd.exe I was wondering if there was a way to change the process name for a batch script to something else so that each script would be more identifiable.
I have done a lot of research on this topic so far, and the only lead that I have is creating a copy of cmd.exe in system32 that has a different name, one of my choosing. The problem is, I am not sure how I would get my bash script to use this new executable with a different name, rather than the default cmd.exe
Requirement: Must use only built in Windows functionality. I do not want to install any other programs if possible.
You can do it with something like the subroutine below. The reason for the first goto is so that you don't fall into the subroutine when you are done. I incorporate another FOR loop to iterate through a list of filenames to check. Let's get this working first.
Your existing bat file goes here
CALL :IsitRunning "SomeFileName"
The rest of your existing bat file goes here
GOTO :eof
:IsitRunning
REM 1=Filename
FOR /F "delims=" %%A in ('WMIC PROCESS WHERE NAME^='CMD.EXE' LIST FULL ^| FINDSTR /I "%~1" ^| FINDSTR /I /V WMIC') DO ECHO(%~1 is running
GOTO :eof
Or you can run this command from a CMD prompt.
wmic process WHERE NAME='cmd.exe' list full | findstr /i "SomeFileName.bat"
You can see command line in Task Manager, turn it on View menu - Choose Columns.
If you want to change process name you have to change the process. So your approach is only way.

Batch script, call cmd on servers

I am trying to call x.cmd from many servers in a list and basically x.cmd will return a log file so I will copy the output log files into a new folder.
the structure in that list is:
server name&folder path of the x.cmd on thet server&output folder name that I will create
when I run the script below, it doesn't return any error and return value is 0. but x.cmd just doesn't do anything when I check on the actual server. x.cmd working fine once I run it manually.
Please if possible point me out the mistake of my script. my other concern is the folder path being too long(but it is within the limits of Microsoft if I am right). :) Thanks for your time.
#echo on
cd /d %~dp0
setlocal EnableDelayedExpansion
set serverList=List.txt
for /f "usebackq tokens=1,2,3* delims=&" %%A in ("%serverList%") DO (
set remoteCmd=x.cmd
wmic /node:%%A process call create "%%B\remoteCmd"
pause
robocopy %%B\logs Output\%%C /e
)
endlocal
Pause
JS
The problem seems to be that you're not actually calling x.cmd on the remote servers, since the variable isn't substituted in the wmic call. Move the definition of remoteCmd out of the loop and enclose remoteCmd in the wmic call in percent signs:
set serverList=List.txt
set remoteCmd=x.cmd
for /f "usebackq tokens=1,2,3* delims=&" %%A in ("%serverList%") DO (
wmic /node:%%A process call create "%%B\%remoteCmd%"
pause
robocopy %%B\logs Output\%%C /e
)
Update 1: Since you're getting a response on the shell with a processID and Error level=0, it's fairly safe to say that x.cmd was in fact started on the remote server. The fact that the logfile does not turn up in the same directory where x.cmd resides in, is very probably because the logfile is not given an absolute path in x.cmd. That means it will be created in the directory on the remote machine where the cmd.exe is located, which is executing x.cmd. In most systems this will be %WINDIR%\system32, i.e. c:\windows\system32.
Update 2: If modifying x.cmd is no option, then you need to change the path to it's directory before calling it. You can do it like this:
wmic /node:%%A process call create "cmd /c cd /d %%B & x.cmd"
Note: The cd /d allows you to change directories even across different drives.
Update 3: Since there are spaces in the directory names you'll need to enclose them with quotes - which will have to be escaped, since there already outer quotes enclosing the whole remote command. So that means:
wmic /node:%%A process call create "cmd /c \"cd /d %%B\" & x.cmd"
Note: A possible alternative to the inner quotes would be to use 8.3-filenames of the involved directories. You can view those with dir /x on the command line.

Running Multiple Installations with System-Reboot Inbetween them

Currently I have a set of software-Installations(and their paths) that i have to install on my Windows Machine.
What I do now is -- Hit RUN every time and type in the software installation path into it..
What I want is to design a Batch file which would install all the applications and REBOOT my system after every successful installation and then continue with the NEXT item in the list..
Is it possible using a .bat file ??
This really isn't something batch was designed for, so this will be a bit hacky. It's not elegant by any means but give it a shot, it might work for you.
for /f %%a in (C:\files.txt) do (
start /wait %%a
exit /b
)
for /f "skip=1" %%b in ("C:\files.txt) do (
echo %%b >>C:\newfiles.txt
)
xcopy C:\newfiles.txt C:\files.txt /y
del C:\newfiles.txt /f /q
shutdown /r /t 0 /f
The idea being that you have a text file with the paths of the executables that you want to install. It will go through and execute the first file in the list, wait for it to complete, then re-write the list without the file it just installed.
This is dependend on the setup file having no user interaction and exiting by itself, or maybe it's just to make things easier - in which case just go through each install yourself, and when it finishes the batch file will do the rest.
On the note of rebooting and continuing you will either need to run the batch file again yourself or put it in the registry to start up itself, the latter command being
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v "MyBatchInstaller" /d "C:\MyBatchFile.bat" /f
Hope this helps

Resources