DOSKEY alias does not work in batch script (Windows 7) - windows

I added a DOSKEY alias via batch script (script1.bat) and try to call it in another batch script. It doesn't work.
script1.bat:
set USER_SETTINGS=%DRIVE%\programme\settings.xml
DOSKEY mvn=mvn --settings %USER_SETTINGS% -X $*
script2.bat:
mvn clean install
When I call mvn clean install from the console, it works. The debug output is forthcoming. When I call script2.bat from the same console, no debug output is coming.
Can anyone help?

If you show the doskey help via doskey /? you get something like: "Recall and edit commands at the DOS prompt, and create macros". A Batch file is not the DOS prompt: the DOSKEY command works with keys pressed as input, like arrows or F7 keys.
For this reason, the next code should work:
script2.bat:
#if (#CodeSection == #Batch) #then
#echo off
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem Send the keys with the DOSKEY macro name:
%SendKeys% "mvn clean install{ENTER}"
goto :EOF
#end
// JScript section
WshShell.SendKeys(WScript.CreateObject("WScript.Shell").Arguments(0));
Further details at Press Keyboard keys using a batch file

doskey DOES work in batch files
Maybe your doskey string does not work
For example, try running this file, junk.bat :
doskey m=echo hi
cmd /k "echo try typing m now"

Related

DOSKEY macros break prompts (SET /P) of batch scripts, when run from within same Windows CMD shell

Yes, it's 2021 and I'm still using the CMD shell and batch scripts for simple tasks.
I just recently tried to enhance my workflow by auto-running my own init-script for (each) CMD prompt session, which also includes setting a bunch of DOSKEY macros.
So this is a similar setup to what others have done. [1]
After having a weird issue with a new script, I came to realize, that DOSKEY macros can break interactive prompts in a batch script using the "SET /P"-command, when executed from such a modified shell with active macros.
Try something like this (enter text shown between [] at prompt + press <enter>):
#ECHO OFF
DOSKEY print=ECHO $*
SET /P var=Enter [print Hallo]:
ECHO var: %var%
EXIT /B 0
You will see, that the prompt variable holds the command prompt created by the DOSKEY macro ('ECHO Hallo').
Normally DOSKEY is not supposed to work/be available from within a batch file [2][3], as it only works on interactive prompts [4].
But "SET /P" seems to count as an interactive shell so DOSKEY replaces everything entered, that begins with a defined macro keyword.
Well, this seems like a bug to me (DOSKEY is now 30 years old btw [6]), but I couldn't find anything on the web about this issue.
So, I was thinking how to workaround this.
As macros cannot be easily deactivated/removed [5], it would be cumbersome to fix this from within any batch file, that does prompts.
My init-script also only runs for interactive sessions, so it isn't a problem, when you run a batch-script directly (on its own).
Tested on a machine with 'Windows 7 Home Premium SP1'.
Sources:
https://gist.github.com/steve-jansen/5118149
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/doskey
https://ss64.com/nt/doskey.html
https://www.dostips.com/forum/viewtopic.php?t=1937#p8379
How to remove a doskey macro?
https://en.wikipedia.org/wiki/DOSKEY
I cannot see a perfect solution, but as DOSKEY is for "interactive shells" the simplest solution is also interactive:
Just clear all macros manually via ALT+F10 [2][3] before running any batch, that uses prompting.
You can also backup and restore the current set of defined macros, e.g. if you have added some during the session.
DOSKEY /macros >%TEMP%\doskey-macros.txt
[Pressing ALT+F10]
batch_that_does_prompts.cmd
DOSKEY /macrofile=%TEMP%\doskey-macros.txt
You can use another window for the set /p, there is no doskey loaded:
#echo off
FOR /F "tokens=3 delims=:" %%# in ("%~0") do goto :%%#
doskey x=XXXXXXXXXXX
echo Test1: Shows the problem
call :subTest
echo Test2: No problem
start "" /wait cmd /c %~d0\:subTest:\..\%~pnx0 pause
echo end
exit /b
:subTest
set "var=empty"
set /p var=[Please enter a single 'x' and press enter]:
set var
%~1
exit /b
Or you "unload" all macros
#echo off
doskey x=XXXXXXXXXXX
echo Test1: Shows the problem
call :subTest
echo Test2: No problem
REM Save macros
DOSKEY /macros >%TEMP%\doskey-macros.txt
call :unload_macros
call :subTest
REM Restore macros
DOSKEY /macrofile=%TEMP%\doskey-macros.txt
echo end
exit /b
:unload_macros
for /F "delims==" %%M in ('doskey /macros') do (
doskey %%M=
)
exit /b

Create alias for a specific command in cmd

Is there anyway to create an alias to cd PATH command in cmd?
For example, instead of typing cd C:\John\Pictures I'd like to type just pictures in cmd and press Enter and it should simply take me to C:\John\Pictures.
Is that possible and how?
Here is an alternative for Windows 10:
1. Create a file called init.cmd and save it to your user folder
C:\Users\YourName\init.cmd
#echo off
doskey c=cls
doskey d=cd %USERPROFILE%\Desktop
doskey e=explorer $*
doskey jp=cd C:\John\Pictures
doskey l=dir /a $*
2. Register it to be applied automatically whenever cmd.exe is executed
In the Command Prompt, run:
reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_EXPAND_SZ /d "%"USERPROFILE"%\init.cmd" /f
3. Restart the Command Prompt
Now close/open the Command Prompt and the aliases will be available.
To unregister, run:
reg delete "HKCU\Software\Microsoft\Command Processor" /v AutoRun
You will need to use the doskey command which creates aliases. For example:
doskey note = "C:\Windows\System32\notepad.exe"
note
creates a macro to open Notepad, then calls it. The macro (note in the above example) must be valid (e.g. no spaces are allowed).
You can also use parameters:
doskey note = "C:\Windows\System32\notepad.exe" $1
note "C:\Users\....\textfile.txt"
By default, doskey macros are only saved for the current session. You can work around this limitation in two ways:
Save the macros in a text file, then load them each time you need them:
A command like:
doskey /macros > %TEMP%\doskey-macros.txt
Will save all the current macro definitions into a text file.
Then to restore all the macros at a later date:
doskey /macrofile=%TEMP%\doskey-macros.txt
After saving the macros in a text file as shown above, instead of loading them every time, run:
reg add "HKCU\Software\Microsoft\Command Processor" /v Autorun /d "doskey /macrofile=\"%TEMP%\doskey-macros.txt\"" /f
so that the macros are set each time you open the cmd.
See this SuperUser answer for more information.
Note: You cannot use command aliases in a batch file.

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%)"

Batch script to open command prompt but not execute the command

Can anyone pls suggest me how to create a batch file that would open up the command window but just type the command I have specified. It should get executed only when I hit enter manually. eg., I created the below .bat file:
test.bat
cmd.exe /K "cd D:\Code_Home && D: && cls && dir"
Now the problem is that it opens up the command window and lists the directory contents, whereas I just want the dir command to stay there and not list me the directory contents until I manually hit enter. Pls suggest if there is a way to do that
I copied the accepted answer at How can I run cmd from batch file and add text to line without executing? and slightly adjust it in order to solve this question:
#if (#CodeSection == #Batch) #then
#echo off
rem Enter the prefill value in the keyboard buffer
CScript //nologo //E:JScript "%~F0" "cd D:\Code_Home & D: & cls & dir"
cmd.exe
goto :EOF
#end
WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));
Further details at the given link.

Running vbscript from batch file

I just need to write a simple batch file just to run a vbscript. Both the vbscript and the batch file are in the same folder and is in the SysWOW64 directory as the vbscript can only be execute in that directory. Currently my batch file is as follows:
#echo off
%WINDIR%\SysWOW64\cmd.exe
cscript necdaily.vbs
But the vbscript wasn't executed and just the command prompt is open. Can anyone tell me how can i execute the vbscript when i run this batch file?
You can use %~dp0 to get the path of the currently running batch file.
Edited to change directory to the VBS location before running
If you want the VBS to synchronously run in the same window, then
#echo off
pushd %~dp0
cscript necdaily.vbs
If you want the VBS to synchronously run in a new window, then
#echo off
pushd %~dp0
start /wait "" cmd /c cscript necdaily.vbs
If you want the VBS to asynchronously run in the same window, then
#echo off
pushd %~dp0
start /b "" cscript necdaily.vbs
If you want the VBS to asynchronously run in a new window, then
#echo off
pushd %~dp0
start "" cmd /c cscript necdaily.vbs
This is the command for the batch file and it can run the vbscript.
C:\Windows\SysWOW64\cmd.exe /c cscript C:\Windows\SysWOW64\...\necdaily.vbs
Just try this code:
start "" "C:\Users\DiPesh\Desktop\vbscript\welcome.vbs"
and save as .bat, it works for me
Batch files are processed row by row and terminate whenever you call an executable directly.
- To make the batch file wait for the process to terminate and continue, put call in front of it.
- To make the batch file continue without waiting, put start "" in front of it.
I recommend using this single line script to accomplish your goal:
#call cscript "%~dp0necdaily.vbs"
(because this is a single line, you can use # instead of #echo off)
If you believe your script can only be called from the SysWOW64 versions of cmd.exe, you might try:
#%WINDIR%\SysWOW64\cmd.exe /c call cscript "%~dp0necdaily.vbs"
If you need the window to remain, you can replace /c with /k
Well i am trying to open a .vbs within a batch file without having to click open but the answer to this question is ...
SET APPDATA=%CD%
start (your file here without the brackets with a .vbs if it is a vbd file)
You should put your .bat file in the same folder as your .vbs file and call the following code inside the .bat file.
start cscript C:\filePath\File.vbs

Resources