Assign command output to variable in batch file
A method has already been devised, however this way you don’t need a temp file. for /f “delims=” %%i in (‘command’) do set output=%%i However, I’m sure this has its own exceptions and limitations.
A method has already been devised, however this way you don’t need a temp file. for /f “delims=” %%i in (‘command’) do set output=%%i However, I’m sure this has its own exceptions and limitations.
From this news group posting by Mark Zbikowski himself: The differences between .CMD and .BAT as far as CMD.EXE is concerned are: With extensions enabled, PATH/APPEND/PROMPT/SET/ASSOC in .CMD files will set ERRORLEVEL regardless of error. .BAT sets ERRORLEVEL only on errors. In other words, if ERRORLEVEL is set to non-0 and then you run one … Read more
The space before the = is interpreted as part of the name, and the space after it (as well as the quotation marks) are interpreted as part of the value. So the variable you’ve created can be referenced with %location %. If that’s not what you want, remove the extra space(s) in the definition.
This lists all the files (and only the files) in the current directory: for /r %i in (*) do echo %i Also if you run that command in a batch file you need to double the % signs. for /r %%i in (*) do echo %%i (thanks @agnul)
Based on Andrew Lessard’s answer, here’s a function to run a command and return the output as a string – Public Function ShellRun(sCmd As String) As String ‘Run a shell command, returning the output as a string Dim oShell As Object Set oShell = CreateObject(“WScript.Shell”) ‘run command Dim oExec As Object Dim oOutput As Object … Read more
One way to run a process from a different directory to the working directory of your Java program is to change directory and then run the process in the same command line. You can do this by getting cmd.exe to run a command line such as cd some_directory && some_program. The following example changes to … Read more
Yes, you can use substitutions and check against the original string: if not x%str1:bcd=%==x%str1% echo It contains bcd The %str1:bcd=% bit will replace a bcd in str1 with an empty string, making it different from the original. If the original didn’t contain a bcd string in it, the modified version will be identical. Testing with … Read more
File Browser Update 2016.3.20: Since PowerShell is a native component of pretty much all modern Windows installations nowadays, I’m declaring the C# fallback as no longer necessary. If you still need it for Vista or XP compatibility, I moved it to a new answer. Starting with this edit, I’m rewriting the script as a Batch … Read more
One way is: application arg0 arg1 > temp.txt set /p VAR=<temp.txt Another is: for /f %%i in (‘application arg0 arg1’) do set VAR=%%i Note that the first % in %%i is used to escape the % after it and is needed when using the above code in a batch file rather than on the command … Read more
Use: call msbuild.bat call unit-tests.bat call deploy.bat When not using CALL, the current batch file stops and the called batch file starts executing. It’s a peculiar behavior dating back to the early MS-DOS days.