Can you put multiple commands in a If command?
What I have is looking to see if something is installed and if it is, send text to a file and change a variable.
My example is lets say you want to see if edge is installed....
Set Edge=N
If exist "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" (
echo Edge is installed >> C:\Temp\Message.txt
) Else (
echo Edge is Not installed >> C:\Temp\Message.txt
)
Where would I need to put the Set Edge=Y if it is installed?
If exist "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" (
echo Edge is installed >> C:\Temp\Message.txt
SET "EDGE=Y"
) Else (
SET "EDGE=N"&echo Edge is Not installed >> C:\Temp\Message.txt
)
Two ways.
Within a parenthesised block, you can place any number of statements you like, all on separate lines.
OR, you can use & to place a number of commands on one line.
Related
I am trying to create a for loop with two list files that will basically echo the script to change the dbowner of multiple databases. The list files contain multiple servers and the login name list contain multiple login names. But they are line separated in order to match each database with the login name.
This is what I have so far but it is obviously taking the first server name and looping it through each login name and then moves onto the next server name.
for servername in $(cat servername.list); do
for loginname in $(cat loginname.list); do
echo "USE $servername"
echo "go"
echo "EXEC sp_changedbowner '$loginname'"
echo "go"
echo "USE master"
echo "go"
echo ""
done
done
I want the output to be this:
USE server1
go
EXEC sp_changedbowner 'login1'
go
USE master
go
USE server2
go
EXEC sp_changedbowner 'login2'
go
USE master
go
You can do it like this:
while read -r; do
server="$REPLY"
read -r <&3 || break
login="$REPLY"
echo \
"USE $server
go
EXEC sp_changedbowner '$login'
go
USE master
go
"
done <servername.list 3<loginname.list
[Live demo]
Using input redirection you can open both files at the same time for the while loop. File descriptor 0 (standard input) is available, but 1 and 2 (standard output / standard error, respectively) are taken. The next free file descriptor is 3.
The loop then first reads a line from 0 (standard input, now connected to servername.list), then a line from 3 (now connected to loginname.list). read places the input in a variable called REPLY, which we copy over to server and login, respectively. These variables are used in the echo string to produce the desired output.
This repeats until one of the files runs out of lines.
By the way, you should not attempt to read lines with for.
So I finally decided to bite the bullet and rewrite my company's terrible Trace32 scripts. I'm trying to use a data file to save the pertinent information between runs so we can just run a Redo script to repeat any action without having to navigate a mess of dialog boxes and select our workspace files after every power cycle. In order to do this, I will need to save the user defined variables from the previous run (including file locations) to file so I have them in the next run. For reference, here is a part of my menu script.
; File: Do.cmm
GLOBAL &WORKSPACE
GLOBAL &FILETOLOAD
GLOBAL &TARGETSELVAL
&WORKSPACE="//tsclient\Z\Product_trunk_MS" ; Not the ideal solution
PRINT "Workspace is &WORKSPACE"
DIALOG
(
HEADER "Do one of the following"
POS 0. 0. 23.
COMMAND.PREVIOUS: CHOOSEBOX "Repeat Last Command" ""
POS 24. 0. 23.
COMMAND.WORKSPACE: CHOOSEBOX "Change Workspace Location" ""
POS 0. 3. 25.
TEXT "Connect To an R7 Proc:"
POS 0. 4. 6.
COMMAND.IP0: CHOOSEBOX "IP0" ""
POS 8. 4. 7.
COMMAND.IP1: CHOOSEBOX "IP1" ""
; And a lot more of the same...
POS 17. 16. 15.
DEFBUTTON "OK" "continue"
)
DIALOG.SET COMMAND.PREVIOUS
STOP
IF DIALOG.BOOLEAN(COMMAND.IP0)
(
&TARGETSELVAL=0x00030000
&FILETOLOAD="&WORKSPACE\CPUs\IP0\build\output\IP0.axf"
)
IF DIALOG.BOOLEAN(COMMAND.IP1)
(
&TARGETSELVAL=0x0003001
&FILETOLOAD=&WORKSPACE\CPUs\IP1\build\output\IP1.axf
)
... And so on
The problem with this is that I have to edit the script every time I change workspaces. I want to be able to set this in the script above by selecting COMMAND.PREVIOUS and then selecting my new workspace root with the Windows selection dialog. I don't have a working implementation for that function, but I want it to look something like this:
IF DIALOG.BOOLEAN(COMMAND.WORKSPACE)
(
PRINT "Select the new root directory that you would like to work out of."
OPEN #1 workspace.dat /Create
&WORKSPACE= C/*/ ; I don't know how to do this here.
WRITE #1 &WORKSPACE
CLOSE #1
ENDDO
)
Obviously, Data.load.binary "*.bin" is able to load a file to memory, but I don't need the file loaded yet, I just want the path. I did find that symbol.SOURCEPATH.SETBASEDIR c:\* will open a dialog box, but I am still having trouble getting at that information.
Additional Info
I have read through a lot of this and this while trying to find a solution. If there is a built in path variable that I should be using (like the aforementioned SOURCEPATH.SETBASEDIR, I wouldn't mind doing that instead. This is my first day writing Trace32 scripts, so I apologize in advance for my naivete. If it matters, I am using Trace32 Powerview for ARM Release Feb 2017 SP2 (32 bit) (So, the latest)
TRACE32 has the concept of a working directory. The command ChDir can be used to change the current directory:
ChDir <path>
The current working directory can be retrieved with the PRACTICE function OS.PWD():
&WORKSPACE=OS.PWD()
The script example above could be extended like this:
IF DIALOG.BOOLEAN(COMMAND.WORKSPACE)
(
PRIVATE &old_directory
&old_directory=OS.PWD() // Save current directory
ChDir * // Open directory selection dialog
&WORKSPACE=OS.PWD() // Update working directory
OPEN #1 workspace.dat /Create
WRITE #1 &WORKSPACE
CLOSE #1
ChDir &old_directory // Restore previous directory selection
)
I ended up finding a solution to my problem that was a little different than xasc's, so I thought I'd share it.
IF DIALOG.BOOLEAN(COMMAND.WORKSPACE)
(
PRINT "Select the new root directory that you would like to work out of."
DIALOG.DIR *
ENTRY %LINE &WORKSPACE
OPEN #1 workspace.dat /Create
WRITE #1 "&WORKSPACE"
CLOSE #1
)
This was a little cleaner for my purposes because it didn't require me to change the working directory.
DIALOG seems to be the interface I was looking for, with the ability to open files, directories, and more and save them into variables.
I am trying to find out if an OS is 32 bit or 64 bit using a batch file and then running a program based on the output. Here is what I have so far
if (systeminfo | findstr = based) == "x64-based PC" run 64-bit Program
else run 32-bit program
I keep getting "| is unexpected at this time". I have tried using a hat ^ and without parenthesis but I can't seem to get passed that error. Is there something I am missing or another way to do this. The batch file and programs will be on a flash drive and be used on multiple windows pc's. The systeminfo command is the only way I know of to get the bit result I need and I know that command works, but I need the program to look at the results and make a decision. Any thoughts would be greatly appreciated! Thanks in advance
Unfortunately, you cannot contain (possible) multi-line output systeminfo | findstr = based within an if statement.
You have to capture the output like this:
for /f "tokens=3" %%A in ('systeminfo ^| findstr based') do (
if /i "%%~A"=="x64-based" (
:: Run 64-bit Program
) else (
:: Run 32-bit Program
)
)
However, I would recommend just using the PROCESSOR_ARCHITECTURE.
reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v PROCESSOR_ARCHITECTURE
https://stackoverflow.com/a/1739055/891976
http://support.microsoft.com/kb/556009
This is from a routine by #Aacini
if exist "%SYSTEMDRIVE%\Program Files (x86)" (
echo Type=64 bit
) else (
echo Type=32 bit
)
Intended software: windows command line processor (version 6.1.7601.17514)
Hi,
I've been trying to build a multiple-statement command line that runs within a short-cut. My goal is to be able to click one short-cut that checks if my hosted network is started or not, and then takes appropriate action based on the check. The code that starts and stops the hosted network is fine, and for the most part, the logic works, but I notice odd behavior when I check the outputs of the logic. I suspect that my problem has to do with the way I structured the statements, but I'm having difficulty properly interpreting the built-in documentation and the documentation I can find in the MSDN library. If it's possible, I want to avoid using batch files for this solution.
To keep things simple, I've substituted my lengthy "netsh" commands with "echo" commands that show the errorcode. The code below is what I'm using to test my logic:
Test Code
netsh wlan show hostednetwork | find "Not" && echo found %errorlevel% || echo lost %errorlevel%
Currently, the way I'm reading this is:
Show me hostednetwork's status and send the output to input
Attempt to find the string "Not" in the input
If the attempt succeeds, output "found" and the errorcode to the screen
If the attempt fails, then output "lost" and the errorcode to the screen
Notice that I'm not using any flags on the find command. I'm doing this because I want to reduce the chance of finding a false match. To clarify what I mean, I'll show the output if I just put in
netsh wlan show hostednetwork:
Sample Output of Hostednetwork Status
C:\Windows\system32>netsh wlan show hostednetwork
Hosted network settings
-----------------------
Mode : Allowed
SSID name : "TestHost"
Max number of clients : 100
Authentication : WPA2-Personal
Cipher : CCMP
Hosted network status
---------------------
Status : Not started
If I search for the string "Not", then that's sufficient to tell me that the hosteadnetwork is not started, because when the hosteadnetwork is started, the output shows "Started".
The way I'm simulating the conditions of the hostednetwork is with the following commands:
netsh wlan start hostednetwork
netsh wlan stop hostednetwork
I expect that when I open a command prompt (as an administrator):
If the hostednetwork is not started, I should see a "found 0" in the output, meaning that the string was found and that there were no errors.
If the hostednetwork is started, I should see a "lost 1" in the output, meaning that the string was not found and that there was an error.
Case #1 works, but case #2 doesn't work on the first try. Here's my output when the hostednetwork is already started:
Output With Hostednetwork Started
C:\Windows\system32>netsh wlan start hostednetwork
The hosted network started.
C:\Windows\system32>netsh wlan show hostednetwork | find "Not" && echo found %er
rorlevel% || echo lost %errorlevel%
lost 0
C:\Windows\system32>netsh wlan show hostednetwork | find "Not" && echo found %er
rorlevel% || echo lost %errorlevel%
lost 1
Other Attempted Solutions
The way I've written the test code is the best I could come up with so far. In previous attempts, I've tried:
Setting a custom variable instead of using the errorlevel variable, but I get the same output on case #2.
Changing the code into an if else equivalent, but that didn't pan out very well.
Wrapping the conditional statements in brackets "()" after the pipe and using different combinations of the special symbols "&" and "|".
Other Questions
This question is related to another that I've been trying to figure out. If I wanted to search for three different strings in a command's output and exit on a different error code for each string, how can I do this? The syntax below is my starting point:
myCommand [/options] | ((find "string1" && exit /b 2 || ver>nul) &&
(find "string2" && exit /b 3 || ver>nul) && (find "string3" && exit /b 4 || ver>nul))
For the same reasons above, I didn't use any flags on the "find" commands. Also, I used "ver>nul" in an attempt to keep the syntax correct since I know the "ver" operation succeeds.
Any assistance is appreciated.
I don't understand why you want to avoid use of a batch script. Your shortcut can simply point to a small batch script, and life will be much easier.
But it is possible to do what you want. The value of %errolevel% is determined during parsing, and the entire shortcut is parsed in one pass, so you get the value that existed prior to execution of your FIND commands. You need delayed expansion !errorlevel! to get your desired results.
In batch you use setlocal enableDelayedExpansion, but that does not work from the command line (or a shortcut). Instead you must instantiate an extra CMD.EXE with the /V:ON option.
netsh wlan show hostednetwork | cmd /v:on /c "find "Not" && echo found !errorlevel! || echo lost !errorlevel!"
There are multiple levels of quoting going on, and that can sometimes cause problems. You can eliminate the quotes enclosing the command if you escape the special characters.
netsh wlan show hostednetwork | cmd /v:on /c find "Not" ^&^& echo found !errorlevel! ^|^| echo lost !errorlevel!
Regarding your 2nd question, I see 2 problems.
1) I don't understand the point of having a shortcut designed to exit with different error codes. How can you possibly make use of the returned error code?
2) You cannot pipe content into multiple FIND commands. The first FIND command will consume all the content and close the pipe, and then subsequent FIND commands will wait indefinitely for content from the keyboard.
You would have to redirect your command output to a temp file, and then redirect input of each FIND command to the temp file.
You cannot evaluate a variable in the same line. It needs delayed expansion and !errorlevel! to be used.
Do it in a batch file and you won't have a problem using delayed expansion.
In a DOS batch file we can only have 1 line if statement body? I think I found somewhere that I could use () for an if block just like the {} used in C-like programming languages, but it is not executing the statements when I try this. No error message either. This my code:
if %GPMANAGER_FOUND%==true(echo GP Manager is up
goto Continue7
)
echo GP Manager is down
:Continue7
Strangely neither "GP Manager is up" nor "GP Manager is down" gets printed when I run the batch file.
You can indeed place create a block of statements to execute after a conditional. But you have the syntax wrong. The parentheses must be used exactly as shown:
if <statement> (
do something
) else (
do something else
)
However, I do not believe that there is any built-in syntax for else-if statements. You will unfortunately need to create nested blocks of if statements to handle that.
Secondly, that %GPMANAGER_FOUND% == true test looks mighty suspicious to me. I don't know what the environment variable is set to or how you're setting it, but I very much doubt that the code you've shown will produce the result you're looking for.
The following sample code works fine for me:
#echo off
if ERRORLEVEL == 0 (
echo GP Manager is up
goto Continue7
)
echo GP Manager is down
:Continue7
Please note a few specific details about my sample code:
The space added between the end of the conditional statement, and the opening parenthesis.
I am setting #echo off to keep from seeing all of the statements printed to the console as they execute, and instead just see the output of those that specifically begin with echo.
I'm using the built-in ERRORLEVEL variable just as a test. Read more here
Logically, Cody's answer should work. However I don't think the command prompt handles a code block logically. For the life of me I can't get that to work properly with any more than a single command within the block. In my case, extensive testing revealed that all of the commands within the block are being cached, and executed simultaneously at the end of the block. This of course doesn't yield the expected results. Here is an oversimplified example:
if %ERRORLEVEL%==0 (
set var1=blue
set var2=cheese
set var3=%var1%_%var2%
)
This should provide var3 with the following value:
blue_cheese
but instead yields:
_
because all 3 commands are cached and executed simultaneously upon exiting the code block.
I was able to overcome this problem by re-writing the if block to only execute one command - goto - and adding a few labels. Its clunky, and I don't much like it, but at least it works.
if %ERRORLEVEL%==0 goto :error0
goto :endif
:error0
set var1=blue
set var2=cheese
set var3=%var1%_%var2%
:endif
Instead of this goto mess, try using the ampersand & or double ampersand && (conditional to errorlevel 0) as command separators.
I fixed a script snippet with this trick, to summarize, I have three batch files, one which calls the other two after having found which letters the external backup drives have been assigned. I leave the first file on the primary external drive so the calls to its backup routine worked fine, but the calls to the second one required an active drive change. The code below shows how I fixed it:
for %%b in (d e f g h i j k l m n o p q r s t u v w x y z) DO (
if exist "%%b:\Backup.cmd" %%b: & CALL "%%b:\Backup.cmd"
)
I ran across this article in the results returned by a search related to the IF command in a batch file, and I couldn't resist the opportunity to correct the misconception that IF blocks are limited to single commands. Following is a portion of a production Windows NT command script that runs daily on the machine on which I am composing this reply.
if "%COPYTOOL%" equ "R" (
WWLOGGER.exe "%APPDATA%\WizardWrx\%~n0.LOG" "Using RoboCopy to make a backup of %USERPROFILE%\My Documents\Outlook Files\*"
%TOOLPATH% %SRCEPATH% %DESTPATH% /copyall %RCLOGSTR% /m /np /r:0 /tee
C:\BIN\ExitCodeMapper.exe C:\BIN\ExitCodeMapper.INI[Robocopy] %TEMP%\%~n0.TMP %ERRORLEVEL%
) else (
WWLOGGER.exe "%APPDATA%\WizardWrx\%~n0.LOG" "Using XCopy to make a backup of %USERPROFILE%\My Documents\Outlook Files\*"
call %TOOLPATH% "%USERPROFILE%\My Documents\Outlook Files\*" "%USERPROFILE%\My Documents\Outlook Files\_backups" /f /m /v /y
C:\BIN\ExitCodeMapper.exe C:\BIN\ExitCodeMapper.INI[Xcopy] %TEMP%\%~n0.TMP %ERRORLEVEL%
)
Perhaps blocks of two or more lines applies exclusively to Windows NT command scripts (.CMD files), because a search of the production scripts directory of an application that is restricted to old school batch (.BAT) files, revealed only one-command blocks. Since the application has gone into extended maintenance (meaning that I am not actively involved in supporting it), I can't say whether that is because I didn't need more than one line, or that I couldn't make them work.
Regardless, if the latter is true, there is a simple workaround; move the multiple lines into either a separate batch file or a batch file subroutine. I know that the latter works in both kinds of scripts.
Maybe a bit late, but hope it hellps:
#echo off
if %ERRORLEVEL% == 0 (
msg * 1st line WORKS FINE rem You can relpace msg * with any othe operation...
goto Continue1
)
:Continue1
If exist "C:\Python31" (
msg * 2nd line WORKS FINE rem You can relpace msg * with any othe operation...
goto Continue2
)
:Continue2
If exist "C:\Python31\Lib\site-packages\PyQt4" (
msg * 3th line WORKS FINE rem You can relpace msg * with any othe operation...
goto Continue3
)
:Continue3
msg * 4th line WORKS FINE rem You can relpace msg * with any othe operation...
goto Continue4
)
:Continue4
msg * "Tutto a posto" rem You can relpace msg * with any othe operation...
pause