I'm writing a Windows batch script. By default, the pause command will pause the script and display the text "Press any key to continue...".
How do I modify this text to display my own text to the user?
You could hide the text from the pause command by using this:
pause >nul
Then you could echo your own message to tell the user it has paused:
echo The batch file has paused
So the full script might look like this:
#echo off
echo Hello World!
echo The batch file has paused
pause >nul
Here's a one-liner
pause>nul|set/p =any key to exit ...
It's slightly shorter (one less 'nul')
than Aacini's solution:
set/p<nul =any key to exit ...&pause>nul
and, with the 'pause' first, I think that
it's a little clearer what the intent is.
With both
cursor stays on the same line
ANY key works, not just 'enter'
Neither are as good, however, as the hypothetical
pause/t any key to exit ...
It's hard to believe that 'pause' has survived
35 years without this ability ;-)
The solutions with 'echo' have the possibly undesirable
trailing new-line,
but do provide for multiple lines of text:
Pause>nul|(echo All your bases &echo are belong to us &echo Press any key to die...)
bv
"Not really what I was looking for, I was wondering whether there was a way of actually changing the output text of the Pause command, not just a workaround to it." – Hashim
Yes, you can! But you must be aware that PAUSE is an internal command of CMD.EXE program, so, to modify the message that PAUSE show, you must modify CMD.EXE file. To do that, you must use an editor that may modify binary files. I used XVI32 program via these steps:
1- Copy CMD.EXE file to a new folder created for this purpose: COPY %COMSPEC%
2- Edit the copy of CMD.EXE with XVI32.EXE program:
2.1- Locate the message you want. Messages are stored in 16-bits elements with the high byte equal zero. To locate a message:
2.1.1- In Search> Find> Text string> enter the message you want.
2.1.2- Convert Text -> Hex
2.1.3- Insert a zero after each letter-value
2.1.4- Press Ok
2.2- Modify the message for the new one. Modify existent letters only and keep zeros in place. Note that you can NOT extend any message.
2.3- End the edition and save the modified file.
You may now run CMD.EXE to get the modified PAUSE message. I made a test of this procedure:
C:\DOCUME~1\Antonio\MYDOCU~1\My Webs\XVI32 Hex File Editor
>pause
Press any key to continue . . .
C:\DOCUME~1\Antonio\MYDOCU~1\My Webs\XVI32 Hex File Editor
>cmd
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\DOCUME~1\Antonio\MYDOCU~1\My Webs\XVI32 Hex File Editor
>pause
Oprime una tecla para seguir. .
Notes to everyone that read this answer:
NOTE 1: Please, don't post that comment saying that modifying CMD.EXE file must NEVER be done! I think the same. I just posted this answer so the OP realized what he really asked for...
NOTE 2: The reviewing of CMD.EXE file with a text editor, like Notepad, is very interesting. You may see all the internal commands, interesting details (like =ExitCode and =ExitCodeAscii variables), all the error messages, etc. For example, these are the internal commands:
C O L O R T I T L E C H D I R C L S C M D E X T V E R S I O N
D E F I N E D C O P Y P A T H P R O M P T P U S H D P O P D
A S S O C F T Y P E D A T E D E L D I R E C H O E N D L O C A L
E R A S E E R R O R L E V E L E X I T E X I S T B R E A K F O R
G O T O I F K E Y S M K D I R M D N O T P A U S E R D
R E M M O V E R E N A M E R E N R M D I R S E T S E T L O C A L
S H I F T S T A R T T I M E T Y P E V E R I F Y V E R V O L = , ; + / [ ] " : . \
P A T H E X T P A T H P R O M P T
F O R / ? I F / ? R E M / ?
% s % s
% s
/ A / P : E O F
f d p n x s a t z D O
/ L / D / F / R I N E L S E ( % s ) % s % s % s % s
% c % c % s % s & ( ) [ ] { } ^ = ; ! % ' + , ` ~
There is no way to change the text of the pause command. However you might want to look at the choice command. You can change the text it prints. The only downside is that you need to provide a list of acceptable characters.
Already many solutions. Another variation that makes some sense:
echo Hit any key to continue...&pause>nul
Another dirty solution would be something like this,
SET /P =Press enter to return to the menu . . .
GOTO :menu
The benefit of this is that the cursor stays on the same line as the message, just like with the PAUSE command.
The downside is that it only listens to the enter key.
Here’s another trick
Pause. >nul | echo. Press something to continue
Starting from Windows 2000 (so, not XP) you can use the choice command which is far more powerful than either pause or set /p.
Besides being able to specify your own prompt text, you can also require the user to type a specific key, and only that key, instead of being limited to, and requiring, [Enter] to be pressed.
Also, you can prompt the user to press a number of different keys and take action based on which key was pressed.
Type choice /? for help, or read more about it here: Wikipedia: choice (command)
You could do it like this!
#echo off
echo Hello World!
echo:
echo Press 1 to continue
set /p letter=
if %letter% == 1 goto a ;or instead of goto you could write start and the file or website you want to start
pause >nul
:a
cls
echo BYE NOW!
pause >nul
pause|echo text you want displayed
Since I dont see it suggested and every dog on the internet has had a go, but me.
This may not have worked under XP, as required by OP, but is valid for any newer system like my current Win7 :-)
Dirty version (no message) |keys ON does nothing, but block the press any key ...
timeout -1|Keys ON
can be used for message after
#timeout -1|Keys ON&echo I'm back
This one will pause and show a response until accepted then return to prompt/next cmd line
#timeout -1|Echo Hello Padawan %userprofile:~9%, Press a key, you can!
:on any keys
#echo Welcome to the dark side
Related
I have the .txt file encoded as UTF-8, I imported the package "\usepackage{listings}", I referenced it properly but yet it wont show up as the code but only prints out the
"\ l s t i n p u t l i s t i n g { f i g u r e s / C o d e S t a t e m a c h i n e . t x t }" part. What am I doing wrong here?
\begin{lstlisting}[language=Matlab, caption={Code Statemachine}, label={lst:CodeStatemachine}, captionpos=b]
\lstinputlisting{figures/CodeStatemachine.txt}
\end{lstlisting}
I also tried just pasting the Code itself, however it just cuts off after it fills the A4 page..
The correct syntax is
\begin{lstlisting}[language=Matlab, caption={Code Statemachine}, label={lst:CodeStatemachine}, captionpos=b]
Code goes here
\end{lstlisting}
or
\lstinputlisting[language=Matlab, caption={Code Statemachine}, label={lst:CodeStatemachine}, captionpos=b]{test.txt}
but not both at the same time.
I am having some trouble figuring out how to iterate over space separated words/characters in a shell script. For instance I would like to iterate over a variable containing the characters in the alphabet separated by a space.
NOTE: The result should be the same even if the alphabet variable contained space separated strings instead of characters, i.e "aa bb cc ..." instead of "a b c .."
I have tried a lot of the alternatives provided from:
How to split a line into words separated by one or more spaces in bash?
Example:
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local index="0"
for character in $alphabet; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
Expected/Desired output:
1. a
2. b
3. c
and so on..
Result:
1. a b c d e f g h i j k l m n o p q r s t u v w x y z
Additional tests(without success):
####################################################################
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local index="0"
for character in ${alphabet[#]}; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
####################################################################
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local alphabetArray=( ${alphabet} )
local index="0"
for character in "${alphabetArray[#]}"; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
####################################################################
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local alphabetArray=( ${alphabet} )
local index="0"
for character in ${alphabetArray}; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
Could someone provide a solution on how to solve this(I would prefer a solution that iterates the alphabet variable without explicitly using an index variable, i.e $alphabet[index] )?
Thanks for your help. I discovered the error thanks to your feedback.
I thought that it was irrelevant when I posted this question but I was experimenting with functions in my .zshrc file. Hence I was using (just my assumption) the zsh interpreter and not the sh or bash interpreter.
By realizing that this could be a potential problem, I googled and found the following How to iterate through string one word at a time in zsh
So I tested the following and it works as expected:
setopt shwordsplit
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local index="0"
for character in $alphabet; do
index=$(($index+1))
echo "$index. $character"
# Possibility to do some more stuff
done
unsetopt shwordsplit
NOTE:
index=$((++$index))
and/or
index=$(($index++))
Doesn't seem to work as I expected in zsh.
... The little gritty details, I should have used:
((++index))
or
((index++))
instead of
index=$((++$index))
Try this
IFS=$' \t\n'
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local index="0"
for character in $alphabet; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
Hope it helps
Currently I have this:
for %%d in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
if exist "%%d\Program Files (x86)\Folder": (
cd "%%d\Program Files (x86)\Folder\subfolder\file"
)
)
But it doesn't seem to be working. I want the batch script to search the drives for the existence of a certain program's subfolder, then cd to the location of a .ini file if it does. The file may also be in a location other than /program files/, i.e. it might be on driveletter:\Folder.
Any help is much appreciated!
You have a few problems with your code:
The colon belongs after the drive letter.
You must use the /D option if your CD command is changing the active drive.
Your logic will fail if "folder" turns out to be a file name instead of a folder name
You never bother to verify that subfolder exists
You cannot CD to a file as your pseudo code implies
The following changes should work:
for %%d in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
if exist "%%d:\Program Files (x86)\Folder\subfolder\" (
cd /d "%%d:\Program Files (x86)\Folder\subfolder"
)
)
But there is a better way - you can simply attempt to CD without verifying the existence of the folder, and redirect any error message to null.
for %%d in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
cd /d "%%d:\Program Files (x86)\Folder\subfolder" 2>nul
)
If you want to detect whether the CD was ultimately successful, then you can use && to conditionally break out of the loop, so that the ERRORLEVEL will be 0 upon success, or 1 upon failure.
for %%d in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
cd /d "%%d:\Program Files (x86)\Folder\subfolder" 2>nul && goto :break
)
:break
if %errorlevel% equ 0 (
echo SUCCESS
) else (
echo FAILURE
)
Besides detecting succuess/error, there is another difference with the last option if the subfolder exists on two different drive. The first two options ultimately CD to the last found subfolder. The last option does a CD to the first found subfolder.
if exist "%programfiles(x86)%\Folder\subfolder\file" (
echo it's alive!
)
is this working? Why do you traverse all these letters?
In Windows you should use environment variables to access the program files folder, so you don't have to check for the existing drives.
Take a look at http://ss64.com/nt/syntax-variables.html or enter set in the command prompt.
In your case you should use %ProgramFiles(x86)%
I'm new to Haskell and trying to set it up on a windows machine. I get it to compile but when I run the .exe file, the program runs and exits right away, barely showing the output.
Below is the sample code I created
module Main where
m = 2 * n
n = f 4
--functions
f::Int -> Int
f x = 2 * x
g::Int -> Int -> Int
g x y = 2 * x * y
main = do
x <-getChar --used to pause program
print m
print $ f m
print $ g m n
I used the getChar to pause the program until a key is pressed. I've been trying to figure out how to do something like exit on a key stroke after the print statements. How can I do this and could there be a better way?
If you run your program in Windows by clicking on it, then it gets a terminal window of its own which itself exits once the program does. I guess that's what you did, or similar.
But if you instead start a terminal window with a DOS prompt, and run the program from there, then that window will survive after the program ends and you can see your output.
You could also run the program from inside GHCi or WinGHCi with the :main command.
Lastly, your x <- getChar could almost work at the end of main, but there's a rule that the last statement in a do block must be an expression, so it cannot bind a variable. But you are not using x for anything, so you can just remove the x <- part:
main = do
print m
print $ f m
print $ g m n
getChar --used to pause program
I have some files in a folder and would like to uppercase the first letter of all the filenames with a certain extension using a batch script in windows.
example cap only *.m
before:
foo.m
bar.m
picture.jpg
after:
Foo.m
Bar.m
picture.jpg
for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
ren %%a*.m %%a* >nul 2>&1
)
Check also this -> https://superuser.com/questions/475874/how-does-the-windows-rename-command-interpret-wildcards