How to end a batch file with an open command window at a certain folder location - windows

This seems like it should be ridiculously simple but I cannot find the answer online or in these forums.
I want to run a command from a batch file, leave the command window open and end at a particular file location.
I can't seem to get both to happen in the same window. This is for a script to run an automated task everytime and leave the window open to run a 2nd task that has a variable input.
start cmd /k c:\users\test\desktop\dmiwtool1.1\win64\dmiwtoolx64.exe & cd c:\users\test\desktop\dmiwtool1.1\win64\
If I run either by itself, they work (runs the exe, ends at /desktop prompt), but in this sequence only the first runs.

this works here:
#ECHO OFF
START /b "" "path\program.exe" "parameter"
CD %UserProfile%\Desktop
Do not use setlocal in your Batch or put endlocal in the line before the CD command.

This should work:
start cmd /k "c:\users\test\desktop\dmiwtool1.1\win64\dmiwtoolx64.exe & cd c:\users\test\desktop\dmiwtool1.1\win64\"
If you leave out the quote marks, the start command and the cd command are run separately.

Related

Cannot run python server using batch file

Here are the commands in batch:
D:
cd D:\Startup\venv\Scripts
activate
cd D:\Startup\
python manage.py runserver
Commands after "activate" are not executed for some reasons. I tried to put "cmd /k activate" instead "activate" but result is still the same except the command line is still open. What is wrong here
I suppose activate is a batch file and therefore needed is:
cd /D D:\Startup\venv\Scripts
call activate.bat
cd D:\Startup
python.exe manage.py runserver
Without command call the processing of a batch file continues on other batch file without returning which is the reason why the last two lines where never executed. Run in a command prompt window call /? and read output help and for more details see for example also answer on How to call a batch file in the parent folder of current batch file?
Command CD with parameter /D makes it possible to change directory and drive at the same time as cd /? executed in a command prompt window explains.
In batch files it is advisable to specify batch files and executables with file extension.

How do I minimize the command prompt from my bat file

I have this bat file and I want to minimize the cmd window when I run it:
#echo off
cd /d C:\leads\ssh
call C:\Ruby192\bin\setrbvars.bat
ruby C:\leads\ssh\put_leads.rb
I want the command window minimized immediately. Any ideas on how to do this?
There is a quite interesting way to execute script minimized by making him restart itself minimised. Here is the code to put in the beginning of your script:
if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
... script logic here ...
exit
How it works
When the script is being executed IS_MINIMIZED is not defined (if not DEFINED IS_MINIMIZED) so:
IS_MINIMIZED is set to 1: set IS_MINIMIZED=1.
Script starts a copy of itself using start command && start "" /min "%~dpnx0" %* where:
"" - empty title for the window.
/min - switch to run minimized.
"%~dpnx0" - full path to your script.
%* - passing through all your script's parameters.
Then initial script finishes its work: && exit.
For the started copy of the script variable IS_MINIMIZED is set by the original script so it just skips the execution of the first line and goes directly to the script logic.
Remarks
You have to reserve some variable name to use it as a flag.
The script should be ended with exit, otherwise the cmd window wouldn't be closed after the script execution.
If your script doesn't accept arguments you could use argument as a flag instead of variable:
if "%1" == "" start "" /min "%~dpnx0" MY_FLAG && exit
or shorter
if "%1" == "" start "" /min "%~f0" MY_FLAG && exit
Use the start command, with the /min switch to run minimized. For example:
start /min C:\Ruby192\bin\setrbvars.bat
Since you've specified a batch file as the argument, the command processor is run, passing the /k switch. This means that the window will remain on screen after the command has finished. You can alter that behavior by explicitly running cmd.exe yourself and passing the appropriate switches if necessary.
Alternatively, you can create a shortcut to the batch file (are PIF files still around), and then alter its properties so that it starts minimized.
The only way I know is by creating a Windows shortcut to the batch file and then changing its properties to run minimized by default.
Using PowerShell you can minimize from the same file without opening a new instance.
powershell -window minimized -command ""
Also -window hidden and -window normal is available to hide completely or restore.
source: https://stackoverflow.com/a/45061676/1178975
If you want to start the batch for Win-Run / autostart, I found I nice solution here https://www.computerhope.com/issues/ch000932.htm & https://superuser.com/questions/364799/how-to-run-the-command-prompt-minimized
cmd.exe /c start /min myfile.bat ^& exit
the cmd.exe is needed as start is no windows command that can be executed outside a batch
/c = exit after the start is finished
the ^& exit part ensures that the window closes even if the batch does not end with exit
However, the initial cmd is still not minimized.
One way to 'minimise' the cmd window is to reduce the size of the console using something like...
echo DO NOT CLOSE THIS WINDOW
MODE CON COLS=30 LINES=2
You can reduce the COLS to about 18 and the LINES to 1 if you wish.
The advantage is that it works under WinPE, 32-bit or 64-bit, and does not require any 3rd party utility.
If you type this text in your bat file:
start /min blah.exe
It will immediately minimize as soon as it opens the program. You will only see a brief flash of it and it will disappear.
You could try running a script as follows
var WindowStyle_Hidden = 0
var objShell = WScript.CreateObject("WScript.Shell")
var result = objShell.Run("cmd.exe /c setrbvars.bat", WindowStyle_Hidden)
save the file as filename.js
Yet another free 3rd party tool that is capable of minimizing the console window at any time (not only when starting the script) is Tcl with the TWAPI extension:
echo package require twapi;twapi::minimize_window [twapi::get_console_window] | tclkitsh -
here tclkitsh.exe is in the PATH and is one of the tclkit-cli-*-twapi-*.exe files downloadable from sourceforge.net/projects/twapi/files/Tcl binaries/Tclkits with TWAPI/. I prefer it to the much lighter min.exe mentioned in Bernard Chen's answer because I use TWAPI for countless other purposes already.
You can minimize the command prompt on during the run but you'll need two additional scripts: windowMode and getCmdPid.bat:
#echo off
call getCmdPid
call windowMode -pid %errorlevel% -mode minimized
cd /d C:\leads\ssh
call C:\Ruby192\bin\setrbvars.bat
ruby C:\leads\ssh\put_leads.rb
One option is to find one of the various utilities that can change the window state of the currently running console window and make a call to it from within the batch script.
You can run it as the first thing in your batch script. Here are two such tools:
min.exe
http://www.paulsadowski.com/wsh/cmdprogs.htm
cmdow
http://www.commandline.co.uk/cmdow/index.html
Another option that works fine for me is to use ConEmu, see http://conemu.github.io/en/ConEmuArgs.html
"C:\Program Files\ConEmu\ConEmu64.exe" -min -run myfile.bat
try these
CONSOLESTATE /Min
or:
SETCONSOLE /minimize
or:
TITLE MinimizeMePlease
FOR /F %%A IN ('CMDOW ˆ| FIND "MinimizeMePlease"') DO CMDOW %%A /MIN
http://conemu.github.io/en/ConEmuArgs.html download flagged by Virus Total.
May have Malware.

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

Pausing a batch file when double-clicked but not when run from a console window?

Is there a way for a batch file (in this case, running on Windows XP) to determine whether it was launched from a command line (i.e. inside a console window) or launched via the shell (e.g. by double-clicking)?
I have a script which I'd like to have pause at certain points when run via the shell, but not when run at a command line. I've seen a similar question on SO, but am unable to use the same solution for two reasons: first, whether or not it pauses needs to be dependent on multiple factors, only one of which is whether it was double-clicked. Second, I'll be distributing this script to others on my team and I can't realistically ask all of them to make registry changes which will affect all scripts.
Is this possible?
Found one :-) – After desperately thinking of what cmd might do when run interactively but not when launching a batch file directly ... I finally found one.
The pseudo-variable %cmdcmdline% contains the command line that was used to launch cmd. In case cmd was started normally this contains something akin to the following:
"C:\Windows\System32\cmd.exe"
However, when launching a batch file it looks like this:
cmd /c ""C:\Users\Me\test.cmd" "
Small demo:
#echo off
for %%x in (%cmdcmdline%) do if /i "%%~x"=="/c" set DOUBLECLICKED=1
if defined DOUBLECLICKED pause
This way of checking might not be the most robust, though, but /c should only be present as an argument if a batch file was launched directly.
Tested here on Windows 7 x64. It may or may not work, break, do something weird, eat children (might be a good thing) or bite you in the nose.
A consolidated answer, derived from much of the information found on this page (and some other stack overflow pages with similar questions). This one does not rely on detecting /c, but actually checks for the name of the script in the command line. As a result this solution will not pause if you double-clicked on another batch and then called this one; you had to double-click on this particular batch file.
:pauseIfDoubleClicked
setlocal enabledelayedexpansion
set testl=%cmdcmdline:"=%
set testr=!testl:%~nx0=!
if not "%testl%" == "%testr%" pause
The variable "testl" gets the full line of the cmd processor call, stripping out all of the pesky double quotes.
The variable "testr" takes "testl" and further strips outs the name of the current batch file name if present (which it will be if the batch file was invoked with a double-click).
The if statement sees if "testl" and "testr" are different. If yes, batch was double-clicked, so pause; if no, batch was typed in on command line (or called from another batch file), go on.
Edit: The same can be done in a single line:
echo %cmdcmdline% | findstr /i /c:"%~nx0" && set standalone=1
In plain English, this
pipes the value of %cmdcmdline% to findstr, which then searches for the current script name
%0 contains the current script name, of course only if shift has not been called beforehand
%~nx0 extracts file name and extension from %0
>NUL 2>&1 mutes findstr by redirecting any output to NUL
findstr sets a non-zero errorlevel if it can't find the substring in question
&& only executes if the preceding command returned without error
as a consequence, standalone will not be defined if the script was started from the command line
Later in the script we can do:
if defined standalone pause
One approach might be to create an autoexec.nt file in the root of c:\ that looks something like:
#set nested=%nested%Z
In your batch file, check if %nested% is "Z" - if it is "Z" then you've been double-clicked, so pause. If it's not "Z" - its going to be "ZZ" or "ZZZ" etc as CMD inherits the environment block of the parent process.
-Oisin
A little more information...
I start with a batch-file (test.cmd) that contains:
#echo %cmdcmdline%
If I double-click the "test.cmd" batch-file from within Windows Explorer, the display of echo %cmdcmdline% is:
cmd /c ""D:\Path\test.cmd" "
When executing the "test.cmd" batch-file from within a Command Prompt window, the display of
echo %cmdcmdline% depends on how the command window was started...
If I start "cmd.exe" by clicking the "Start-Orb" and "Command Prompt" or if I click "Start-Orb" and execute "cmd.exe" from the search/run box. Then I execute the "test.cmd" batch-file, the display of echo %cmdcmdline% is:
"C:\Windows\system32\cmd.exe"
Also, for me, if I click "Command Prompt" from the desktop shortcut, then execute the "test.cmd" batch-file, the display of echo %cmdcmdline% is also:
"C:\Windows\system32\cmd.exe"
But, if I "Right-Click" inside a Windows Explorer window and select "Open Command Prompt Here", then execute the "test.cmd" batch-file, the display of echo %cmdcmdline% is:
"C:\Windows\System32\cmd.exe" /k ver
So, just be careful, if you start "cmd.exe" from a shortcut that contains a "/c" in the "Target" field (unlikely), then the test in the previous example will fail to test this case properly.

how to minimize the command prompt when running a batch script?

anybody know how to minimize the command prompt when running a batch script in Windows 3.11?
in Windows XP, there have a command
start /min start.bat
to minimize the cmd prompt, is it have similiar command in MSDos 6.22?
This little batch code should do what you need.
http://zoombody.com/articles/run-a-batch-script-minimized
The relevant code then needs to be placed at the top of your script:
if not "%minimized%"=="" goto :minimized
set minimized=true
start /min cmd /C "%~dpnx0"
goto :EOF
:minimized
rem Anything after here will run in a minimized window
Here is more information about "%~dpnx0".
There isn't a command that you can use from a DOS prompt, however you can create a PIF file that points to your batch file, in which you can set the default window position (including minimized).
...I think. It's been a couple of decades.
How to minimize the current command prompt:
Purpose:
Close a command prompt after execution.
Open a new command prompt using the same path.
First open command prompt :
Hold down the [Windows] + [R] keys.
Go to this location:
C:\>cd Users\Admin\Desktop
Next excute:
C:\Users\Admin\Desktop>start /min && exit
Results:
A new minimized command prompt should open with the same path.
Alternatively:
Create a script named minimize.bat and type start /min && exit into the script and place it in your system32 folder. In order to have the ability to access it via the command prompt.
Windows 10 makes it very easy to start minimized. Simply create a shortcut and click properties. On the shortcut tab half way down is the "Run:" option witch lets you select how you want the command line window to start up. Just select minimized and your command window will remain minimized from the onset.
Windows10 properties window of a shortcut
Use WinExec to start your batch file and pass SW_SHOWMINIMIZED for the second parameter.
See also, How do I run a batch file with WinExec?

Resources