Command works in Command Prompt but not in a batch file - windows

Trying to print out the 8dot3 name of a file, it does works when I paste the line in the Command Prompt but not when I run it from a batch file.
Result when pasted :
D:\tmp>cmd /e:on /c for %A in (8088_othello.com) do #echo %~nxsA
8088_O~1.COM
Result from batch file :
D:\tmp>lfn.bat
D:\tmp>cmd /e:on /c for ~nxsA
~nxsA was unexpected at this time.
What else is needed to make it work inside a batch file ?

you need to escape % is batch files
just type
cmd /e:on /c for %%A in (8088_othello.com) do #echo %%~nxsA

Related

Bash's "for" for windows

How to can I to write this linux-like loop in the Windows 7 command line?
for docker_path in `ls | grep "docker$"`
do
cd $docker_path
mvn -B -f pom.xml clean deploy -Pdocker
cd ..
done
I need found all *docker/ directory, exec there mvn-command and return into patern directory, but for Windows7 system.
On Windows command line use:
for /D %I in (*docker) do #pushd "%I" & mvn.exe -B -f pom.xml clean deploy -Pdocker & popd
Use the following command line if mvn is not an executable, but a *.bat or *.cmd file:
for /D %I in (*docker) do #pushd "%I" & call mvn.bat -B -f pom.xml clean deploy -Pdocker & popd
Well, on command prompt it is not really necessary to use command CALL to run a batch file in a loop together with two other commands as done here.
The first command line for usage in a batch file:
#for /D %%I in (*docker) do #pushd "%%I" & mvn.exe -B -f pom.xml clean deploy -Pdocker & popd
In a batch file the loop variable I must be referenced with two percent signs instead of just one as on command prompt. The loop variable can be only a single character.
The second command line for usage in a batch file:
for /D %%I in (*docker) do #pushd "%%I" & call mvn.bat -B -f pom.xml clean deploy -Pdocker & popd
In a batch file it is necessary to use command CALL to call another batch file and continue with next command respectively command line in current batch file after execution of the other batch file finished.
Windows command processor cmd.exe continues execution of current batch file on the other batch file on not using command CALL and exits batch file processing once the execution of other batch file finished without further processing command lines in current batch file.
See answer on How to call a batch file that is one level up from the current directory? for details on the existing methods to run a batch file from within a batch file.
The single command line can be also coded with multiple lines:
#echo off
for /D %%I in (*docker) do (
pushd "%%I"
mvn.exe -B -f pom.xml clean deploy -Pdocker
popd
)
And the same multi-line batch file solution in case of mvn is a batch file.
#echo off
for /D %%I in (*docker) do (
pushd "%%I"
call mvn.bat -B -f pom.xml clean deploy -Pdocker
popd
)
Command FOR with option /D searches with wildcard pattern *docker for non hidden directories in current directory of which directory name ends with the string docker.
It is advisable on Windows to reference a file to execute with complete name of file, i.e. file name + file extension. This makes it clear for Windows command processor as well as every reader of the code if the executed file is an executable or a script file which makes a difference as it can be seen here.
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.
call /?
echo /?
for /?
popd /?
pushd /?
See also:
Single line with multiple commands using Windows batch file
Microsoft's command-line reference
SS64.com - A-Z index of the Windows CMD command line

get the console output and print sametime in a batch file

i want to print console output and get at the sametime.
i tried to this with below code to get and printing the result but it's not work.
how can i do this?
code:
set myvar="C:\Program Files (x86)\Inno Setup 5\iscc.exe" asd.iss /SSign_PATH="%cd%\DigitalSign\signtool.exe sign $p"
for /f "delims=" %%i in ('%myvar%') DO (
echo %%i
set OUTPATH=%%i )
result:
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
Your error message is arising from the FOR /F command.
Solution: A small change to the FOR /F statement
for /f "delims=" %%i in ('^"%myvar%^"') DO (
Explanation:
When executing a command via something like FOR /F .... IN ('command') DO ..., the command is executed in a new cmd.exe process via a command that looks like the following:
C:\Windows\system32\cmd.exe /c command
Your command requires quotes around the executable path because of the spaces. So your command, as currently written, becomes:
C:\Windows\system32\cmd.exe /c "C:\Program Files (x86)\Inno Setup 5\iscc.exe" asd.iss /SSign_PATH="%cd%\DigitalSign\signtool.exe sign $p"
But CMD.EXE has an unfortunate design that if the first character of the command is a quote, then it normally strips the outer most quotes from the command string before it is executed. So the command becomes
C:\Program Files (x86)\Inno Setup 5\iscc.exe" asd.iss /SSign_PATH="%cd%\DigitalSign\signtool.exe sign $p
Looking at the above, you should be able to see why you are getting the error message.
The solution is to add an extra set of quotes around the entire command that will get stripped. You want the command to look like:
C:\Windows\system32\cmd.exe /c ""C:\Program Files (x86)\Inno Setup 5\iscc.exe" asd.iss /SSign_PATH="%cd%\DigitalSign\signtool.exe sign $p""
But you don't want the extra set of quotes to throw off the batch parser, so they should be escaped within the IN() clause.

How do you test if a window (by title) is already open from the command prompt?

In a batch file, I'm opening a window with a specific name like this:
#echo off
start "my log" /D \logs\ /I powershell -nologo -noexit -command "$host.ui.RawUI.WindowTitle = 'My log'"; get-content logfile.log -wait
So the window title is "My log".
How do you run this command only if the window is not already open. Is there a bat file command to test for this? I'd rather not use a program or a powershell command, just a simple bat file cmd if possible.
Something like this:
#echo off
if EXISTS window("My log") goto skip
start "my log" /D \logs\ /I powershell -nologo -noexit -command "$host.ui.RawUI.WindowTitle = 'My log'"; get-content logfile.log -wait
:skip
#For /f "Delims=:" %A in ('tasklist /v /fi "WINDOWTITLE eq New Folder"') do #if %A==INFO echo Prog not running
More info on batch. Also see for /?.
& seperates commands on a line.
&& executes this command only if previous command's errorlevel is 0.
|| (not used above) executes this command only if previous command's errorlevel is NOT 0
> output to a file
>> append output to a file
< input from a file
| output of one command into the input of another command
^ escapes any of the above, including itself, if needed to be passed to a program
" parameters with spaces must be enclosed in quotes
+ used with copy to concatinate files. E.G. copy file1+file2 newfile
, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,
%variablename% a inbuilt or user set environmental variable
!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command
%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.
%* (%*) the entire command line.
%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.
.
--
Always use tasklist, here is an example:
#echo off
for /f "tokens=*" %%a in ('tasklist /fi "WINDOWTITLE eq My log"') do if "%%a" == "INFO: No tasks are running which match the specified criteria." start "my log" /D \logs\ /I powershell -nologo -noexit -command "$host.ui.RawUI.WindowTitle = 'My log'"; get-content logfile.log -wait
That's right, you can do it on just 2 lines.

Create a batch file to show all file content with file parameters

I would like to create a batch script which will give me parameters like #fname,#fdate,#fsize
i have already used the below
#forfiles /p path /m A*.bak /c "cmd /c echo #fsize #fname #fdate,#ftime"
but the #fsize gives only 10 digit size, not beyond that hence loosing correct information
i have also tried with
for %I in (E:\*.txt) do #echo %~znI
but it doesn't work in the bat file.
Is there any other option to get only the output not other comments along with it like DIR command. Thank you in advance for your help.
This will do a similar job from the command line. Double all % to %% to use it in a batch file.
for %a in (A*.bak) do #echo %~za "%~na" %~ta

How to write a windows batch file

I have a executable "myfile.exe" file in location "C:\Users\me\Desktop" I want to execute a command "myfile.exe <firstArg> <secondArg>" at C:\Users\me\Desktop through batch file,
In command line:
C:\Users\me\Desktop>myfile.exe <firstArg> <secondArg>
How to write the batch script for the above command in windows
With argument variables the contents of you batchFile.bat would be:
#echo off
C:\Users\me\Desktop\myfile.exe %1 %2
or with fixed arguments:
#echo off
C:\Users\me\Desktop\myfile.exe <firstArg> <secondArg>
Just add the exact command you are executing from command line to .bat file.
Change working directory first if necessary.
cd C:\Users\me\Desktop
myfile.exe <firstArg> <secondArg>
Your batch file:
C:\Users\me\Desktop\myfile.exe %1 %2
Then you can call your batch file:
mybatchfile.bat <firstArg> <secondArg>
Also, you can use %* to pass all parameters rather than specifying each parameter.

Resources