cmd.exe /C unable to run the command - windows

I'm running below command which is working successfully if I run it manually via command prompt
SET filename=testfile_26032021.txt && SET newfilename=%filename:~9,8% && copy C:\test\updatedtestfile_%newfilename%.txt C:\test\updatedtestfile_%newfilename%.txt.temp
But when I run this through an external call I get an error
The system cannot find the file specified.
Here's the command I'm running
cmd.exe /C SET filename=testfile_26032021.txt && SET newfilename=%filename:~9,8% && copy C:\test\updatedtestfile_%newfilename%.txt C:\test\updatedtestfile_%newfilename%.txt.temp
I caught the error by changing the flag from /C to /K.
Any idea what is wrong with this command?

You can't do like this SET filename=testfile_26032021.txt && SET newfilename=%filename:~9,8% because %filename% is expanded at parsing time where it's not available yet. You must enable delayed expansion and tell cmd to expand the command later with !variable_name!. Besides && ends the previous command so your whole thing is parsed as 3 commands:
cmd.exe /C SET filename=testfile_26032021.txt
SET newfilename=%filename:~9,8%
copy C:\test\updatedtestfile_%newfilename%.txt C:\test\updatedtestfile_%newfilename%.txt.temp
So you must quote the command like this
cmd.exe /V:on /C "SET "filename=testfile_26032021.txt" && SET "newfilename=!filename:~9,8!" && copy C:\test\updatedtestfile_!newfilename!.txt C:\test\updatedtestfile_!newfilename!.txt.temp"
or
cmd.exe /C "setlocal enabledelayedexpansion && SET filename=testfile_26032021.txt && SET newfilename=!filename:~9,8! && copy C:\test\updatedtestfile_!newfilename!.txt C:\test\updatedtestfile_!newfilename!.txt.temp"
See also
Windows command prompt: Using a variable set in the same line of a one-liner
How set a variable and then use it in the same line in command prompt
Setting and using variable within same command line in Windows cmd.exe

Related

How to set and echo a variable in git bash using cmd.exe

Using cmd.exe, I can run SET "var=TEST" and then I can run echo %TEST and get the result back TEST. How can I do this in a single command using git bash? I would think it would be cmd.exe /c set "var=TEST" && echo %var% but that doesn't seem to work.

How to exit 'git-cmd.exe' after running a command via it in a batch file?

I'm writing a batch file that runs a command via git-cmd.exe but it doesn't run the command(s) after it.
I've tried to use CALL, START /WAIT, and START /B /WAIT. All have the same behavior. Maybe there is a parameter should be sent to git-cmd.exe to execute the command and exit but I didn't find any guide explaining how to use git-cmd.exe.
This is a sample batch file:
#ECHO OFF
SET "PATH=C:\Ruby26-x64\bin;C:\Program Files\nodejs;%PATH%"
SET "CurrentDirectory=%CD%"
CD /D "%UserProfile%\AppData\Local\GitHub\PortableGit_*\"
SET "GitDirectory=%CD%"
CD /D "%CurrentDirectory%"
"%GitDirectory%/git-cmd.exe" CALL rake build
PAUSE
The command passed to git-cmd.exe is executed but the PAUSE command doesn't execute until I type EXIT command manually in the 'Command Prompt' window.
I've also tried a simple DIR command instead of rake build but the same issue still occurs:
"%GitDirectory%/git-cmd.exe" DIR
PAUSE
Thanks to the suggestions, the issue was resolved by passing EXIT command to git-cmd.exe as follows:
#ECHO OFF
SET "PATH=C:\Ruby26-x64\bin;C:\Program Files\nodejs;%PATH%"
SET "CurrentDirectory=%CD%"
CD /D "%UserProfile%\AppData\Local\GitHub\PortableGit_*\"
SET "GitDirectory=%CD%"
CD /D "%CurrentDirectory%"
"%GitDirectory%/git-cmd.exe" "CALL rake build & EXIT"
PAUSE
There was a useful conversation between me and someone else (I think his name was mony) but it was deleted, don't know why?!
He sent me this link with the difference between & and && between commands:
https://stackoverflow.com/a/25344009/9586127
The & before the EXIT command in order to execute both commands independent on result of the first one. If && is used, the EXIT command will be executed only if the first command succeeded (exit code 0).
In addition, he told me a way to replace the lines 3:6 by one line to be:
FOR /D %%I IN ("%LocalAppData%\GitHub\PortableGit_*") DO SET "GitDirectory=%%I"

How to run commands on another cmd

I want to run commands on 2 cmds while opening the same file, like if I open a .bat file It opens 2 cmd and they run 2 differents commands (1 each). It's possible to do that?
If I got you right this is what you want to do, note it's a batch file:
#echo off
start cmd /c "echo 1st command && pause"
start cmd /c "echo 2nd command && pause"
Read about cmd here and about start here. The following switches of the cmd command can be considered:
/c: Carries out the command specified by string and then stops.
/k: Carries out the command specified by string and continues.
Instead of using /k I used /c with a pause command to show the concatenation of 2 commands here.
To concate 2 commands use commandA && commandB which is described here at ss64 which is a great site when it comes to batch scripting:
commandA && commandB: Run commandA, if it succeeds then run commandB
As requested another example with cd, dir and pause could look like:
#echo off
start cmd /c "cd C:\Users\ && dir && pause"
start cmd /c "cd C:\ && dir && pause"
It changes the directory, prints the directory list and wait for use input.

batch windows command written in prompt but no execution

To avoid to repeat a task too often, I am setting up a batch file (in WINDOWS 10). It opens several CMD PROMPT to a specific Directory and launch a command.
For one case, I want the CMD PROMPT to open, to go to the specific directory and to set the COMMAND in the PROMPT without launching it. Then I'd just have to click on ENTER to launch that command whenever I want later on.
Here is my code:
setlocal ENABLEEXTENSIONS
set CordovaProjPath="C:\MyPath\"
start cmd /k "cd /d %CordovaProjPath% && cordova build android"
With this code it launches the command "cordova build android".
If I go with start cmd /k "cd /d %JCACordovaProjPath% instead of start cmd /k "cd /d %JCACordovaProjPath% && cordova build android" it gives me the PROMPT with: "C:\MyPath>", I'd like to write: "cordova build android" behind it without launching the command.
Any idea?
To provide repeatable execution (as mentioned in comments) you can put the relevant commands in a loop with a "quit" option:
#Echo Off
setlocal
Set "CordovaProjPath=C:\MyPath"
Set "CommandToRun=cordova build android"
:loop
Cd /D %CordovaProjPath%
Echo %CommandToRun%
set QUIT=
set /p QUIT=Press ENTER to run command or 'Q' to quit:
if /i "%QUIT%" == "Q" goto :eof
%CommandToRun%
goto :loop
Unlike the original, this runs the target command in the same command-window as the repeating loop. Depending on what the command in question does, this may be more attractive (less windows popping-up). However, some commands may cause the main window to close; if this is the case, you can revert to running the command in its own window in one of two different ways. In each case, replace the line:
...
%CommandToRun%
...
Run in own window and remain open
...
start "%CommandToRun%" /wait cmd /k %CommandToRun%
...
Using /k will leave the command-prompt window open after the target command has run -- this may be appropriate if you need to see the output of the command and it does not have its own pause.
Run in own window then close
...
start "%CommandToRun%" /wait cmd /c %CommandToRun%
...
Using /c will mean the command-prompt will close after the target command has run. This may be appropriate if you do not need to see the output of the command, or if it has its own pause.
Would something like this do you:
#Echo Off
Set "CordovaProjPath=C:\MyPath"
Set "CommandToRun=cordova build android"
Start "%CommandToRun%" Cmd /K "Cd /D %CordovaProjPath%&Echo %CommandToRun%&Pause>Nul&%CommandToRun%"
Below is an alternative which may allow for your alternative double-quoting method:
#Echo Off
Set CordovaProjPath="C:\MyPath"
Set CommandToRun="cordova build android"
Start %CommandToRun% Cmd /K "(Cd /D %CordovaProjPath%)&(Echo %CommandToRun%)&(Pause>Nul)&(%CommandToRun%)"

%ERRORLEVEL% is not working incase multiple commands on a Single line

I am trying the below commands and want to get exit of my process(%ERRORLEVEL%). But it is returning previous(last) executed exit code result of sample.exe. I want to get exit code of current command. My requirement is to execute multiple commands in single line*(not batch script)*.
Command:
cmd /c sample.bat "test" > c:\ouput.log & echo %ERRORLEVEL% > c:\returnCode.log
I even tried using "setlocal enableDelayedExpansion" like below. Still It is not returning the exit code of current command
cmd /c setlocal enableDelayedExpansion & sample.bat "test" > c:\ouput.log & echo %ERRORLEVEL% > c:\returnCode.log
Please let me know the way to get current command's exit code.
This should work:
cmd /V:ON /c sample.exe "test" > c:\ouput.log ^& echo !ERRORLEVEL! ^> c:\returnCode.log
/V:ON switch have the same effect of setlocal EnableDelayedExpansion. For further details, type: cmd /?
EDIT: Small error fixed, the & character must be escaped with ^, otherwise, the echo !ERRORLEVEL! command is not executed in the cmd /V:ON !!!
EDIT: Escaping the echo redirection via ^> causes just that echo to be piped into the log. If you do not escape that, the entire command is piped there, i.e. including the stdout stream from "sample.exe".
cmd /c sample.exe "test" > c:\ouput.log & call echo %%ERRORLEVEL%% > c:\returnCode.log
Should work for you. See endless SO items related to delayedexpansion
Thank for your response. I am able to get the exit code of current executed command with below command, only when I run through WMI class(Win32_Process). Using WMI client, I am executing the below command on Remote machine and it is working fine i.e. able to write exit code in Retrun.txt
Command:
cmd /V:ON /c sample.bat "test" > c:\Output.txt & echo !ERRORLEVEL! > c:\Return.txt
But if I run the same command in command prompt of the same remote machine, it is printing "!ERRORLVEL!" in Return.txt instead of "sample.bat" exit code.
I am curious to know why it is not working if I run from Command prompt of the same machine locally.

Resources