Windows batch files: .bat vs .cmd?

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

Capture output value from a shell command in VBA?

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

Run cmd commands through Java

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

Batch file: Find if substring is in string (not in a file)

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