Batch script to open command prompt but not execute the command - windows

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.

Related

How to make a custom "Press any key to continue" message in batch

For an example "Press any key to return to command prompt"
what do I even put here lol.
There can be used in the batch file the command line:
set /P "=Press any key to return to command prompt... " <nul & pause >nul & echo/
The caret is blinking in this case on same line as the prompt and not on the next line as it is the case on using:
echo Press any key to return to command prompt...
pause >nul
But in most cases can be simply used just pause to halt the batch file execution until the user pressed a key which is often not necessary on user executed the batch file from within a command prompt window.
There can be used the following command line to halt the batch file execution only if cmd.exe was started with option /c as first argument as done when double clicking on a batch file.
setlocal EnableExtensions EnableDelayedExpansion & for /F "tokens=1,2" %%G in ("!CMDCMDLINE!") do endlocal & if /I "%%~nG" == "cmd" if /I "%%~H" == "/c" pause
This command line does not run pause if the batch file is executed from within an opened command prompt window by a user who typed the batch file name and pressed RETURN or ENTER to execute it. It is often annoying for a user of a batch file starting it from within a command prompt window to press a key to end its execution.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
cmd /?
echo /?
endlocal /?
for /?
help ... outputs an incomplete list of Windows commands with a brief description
pause /?
set /?
setlocal /?
See also:
A-Z index of Windows CMD commands
Single line with multiple commands using Windows batch file
Microsoft documentation about Using command redirection operators
DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/

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

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"

Why is the following multiple DOS commands in one line not working correctly? (Passing Values)

I am trying to create a text file via DOS commands. The commands asks one for the name of the file prior to creating it. I have looked here and here and elsewhere to get me started.
I would like the code to work in one line. So, I should be able to type the entire code in Windows Start > Run box.
This is what I have:
cmd /k #ECHO OFF & SET /P filename=What File name: & copy NUL %filename%.txt & :End
This however ignores the name of the file I gave when asked, and creates %filename%.txt.
I have tried changing the operator before the word copy to |, &&, and & but these don't even ask me for a file name and simply create %filename%.txt
Also, the cmd box stays open after the text file is created.
P.S. I know I can use /q before /k for echo off.
I look forward to your help.
This works here: delayed expansion is used in another cmd process
cmd /c #ECHO OFF & SET /P filename=What File name: & cmd /v /c copy NUL !filename!.txt & exit
The command line has no path defined for the directory and when using the RUN box it will probably be created in the c:\windows\system32 folder, except you will not have write access to that folder.

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

open command prompt window and change current working directory

I'm terribly new to scripting on windows. Using windows 7 64.
I'm trying to make a .bat file that I can double click, and have it open a command prompt and automatically cd me to a certain directory.
I tried making a .bat file with
#ECHO OFF
cmd "cd C:\my\destination"
Which opens what looks like a command prompt, but doesn't seem to let me type any commands.
I then tried:
#ECHO OFF
start cmd "cd C:\my\destination"
But this just sent me into a loop opening tons and tons of prompts until my computer crashed :) The .bat file was located in the destination directory if that matters.
This works for me:
#ECHO OFF
cmd.exe /K "cd C:\my\destination && C:"
The quoted string is actually two commands (separated by a double ampersand): The first command is to change to the specified directory, the second command is to change to the specified drive letter.
Put this in a batch (.BAT) file and when you execute it you should see a Command Prompt window at the specified directory.
Use the /K switch:
#ECHO OFF
start cmd.exe /K "cd C:\my\destination"
But IMHO, the most useful switch is /?.
Starts a new instance of the Windows XP command interpreter
CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
[[/S] [/C | /K] string]
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
/S Modifies the treatment of string after /C or /K (see below)
/Q Turns echo off
...
And only if it does not work, then Google it, as #Neeraj suggested :D
This could be done like that:
#ECHO OFF
cd /D "C:\my\destination"
cmd.exe
If you need to execute a file or command after you open the cmd you can just replace the last line with:
cmd.exe /k myCommand
#ECHO OFF
%comspec% /K "cd /D d:\somefolder"
The /D will change folder and drive and works on 2000+ (Not sure about NT4)
If you take a look at Vista's open command here, it uses cmd.exe /s /k pushd \"%V\" but I don't think %V is documented. Using pushd is a good idea if your path is UNC (\\server\share\folder) To get UNC current directory working, you might have to set the DisableUNCCheck registry entry...
Why so complicated? Just create an alias to cmd.exe, right click on the alias and navigate to its settings. Change the "execute in" to the path you want to have as standard path. It will always start in this path.
just open a text editor and type
start cmd.exe
cd C:\desired path
Then save it as a .bat file. Works for me.
You can create a batch file "go-to-folder.bat" with the following statements:
rem changes the current directory
cd "C:\my\destination"
rem changes the drive if necessary
c:
rem runs CMD
cmd

Resources