I have a quite extensive batch file for swapping project files around and includes a few menu's and clearscreen commands. I have included the command - #Echo off to avoid all commands being output. The batch file works fine except it outputs the statement "Echo is off" when I refresh the screen for a new menu. Is there any way to stop the "Echo is off" statement?
Look for a line that has echo and either nothing after it or a variable that is empty.
This is good to echo a blank line
echo(
and similarly this will echo a blank line and not a "echo is off" error when the variable is empty.
echo(%variable%
The echo statement is question is attempting to echo a variable which is not set.
The easy way is to replace echo %var% with echo(%var%
Related
What function does the open parenthesis serve in this command?
Why is there no closed parenthesis?
>> datatest.csv.TEMP echo(!modified!
This is the result of a discussion on DosTips about nine years ago.
Your code will redirect the value of !modified! to datatest.csv.TEMP or it will print a blank line in that file if the variable is empty. According to echo /?, the official way to display a blank line is to use echo.. However, if cmd.exe is somehow able to find an executable with no extension called echo (for example, a file in the same directory as the script), that file gets used instead of the regular echo command.
A few alternatives to echo. were considered, including echo\, echo:, echo/ and echo(. echo\ was ruled out in case of a situation where there was a folder called echo that contained a file with the same name as whatever was being echoed. echo/ was ruled out in situations where the string to be displayed started with a ?, because in the case the help was displayed. echo: was ruled out for extremely rare situations where string substitution was being utilized.
Ultimately, echo( was ended up with simply because nobody could find a situation where it didn't work. (Later on, there was some speculation that echo=, echo,, and echo; are all safe to use without weird side effects or edge cases. However, if you are trying to display the literal string /?, the help for echo will be displayed instead.)
The ) is not included because it will get displayed.
I know > and >> redirect a command to a file, but how can I get every line of data from the batch file? I have many commands that echo stuff, but I want just 1 that will echo every single command that's been used in the window to a text document.
Batch file:
#echo off
Choice /n /c 12
If %errorlevel%==1 echo hi
Etc..
You know what works perfectly? Right click > edit > select all. HOW THE HELL DO I DO THAT IN CODE
Say your batch script is called myScript.bat, then redirect when you call it:
myScript >log.txt
You will want to add CALL if used from within another batch script.
You can do the redirection from within your script if you CALL a main routine:
#echo off
call :main >log.txt
exit /b
:main
rem rest of your code goes here.
You probably looking for the tee command. It allows for writing to both STDOUT and a textfile at the same time.
More info here : http://linux.101hacks.com/unix/tee-command-examples/
**Batch file **I am writing batch file that takes in command line parameters and do some stuff if the parameters match. But when the run the batch file again with the new parameters, the file still runs with the old parameters.
first run : file.bat -name hello -w 400 -r 320
second run : file.bat -name hello -w 400.
When i do the second run of the file, it still performs the first run, i mean to say it still considers the parameters from the first run. Does anybody know why this is the case?
Thanks in advance.
Sorry for not posting the batch file code.
below mentioned is the code.
set filename=
set rate=
set hidden=
set rate=
:recheck
if "%1"=="-help" goto :help
if "%1"=="-name" goto :filename
if "%1"=="-h" goto :hidden
if "%1"=="-w" goto :weight
if "%1"=="-r" goto :rate
if "%~1"=="" goto :endofcommandline
:filename
shift
set filename =%1
shift
goto :recheck
For the other part of the file, I use the variable "filename" to do other stuff. I also use setlocal and endlocal at the beginning and the end of the file.
I have other corresponding labels for the each if statement and set different variables to the values passed to them if any by the user.
Sometimes, I also get echo is off when I try to print out the value of some variable.
All the variables which I use are cleared in the beginning of the file as mentioned above.
There are a couple points here.
"I also use setlocal and endlocal at the beginning and the end of the file."
"All the variables which I use are cleared in the beginning of the file as mentioned above."
Setlocal command does NOT clear the variables, it preserve current variables from posterior modification. This mean that if in a previous execution of your Batch file, or even via typed SET commands, filename variable was defined, it value remains until it is deleted or modified.
"Ok!", you said, "filename variable is modified the second time the Batch file run, Right?"
Well, no... The following line:
set filename =%1
does NOT modify "filename" variable, but "filename " instead (filename and a space). Try this:
set filename=No space
echo %filename%
set filename =Bad name with trailing space
echo %filename %
echo %filename%
You must carefully check your program to avoid this type of errors.
How can I run a command in my dos script from an input argument ?
Simplified script bla.bat:
CALL %1
Call it:
bla.bat "echo 'hello'" (or bla.bat "git status")
Error:
'"git status"' is not recognized as an internal or external command,
operable program or batch file.
It works if I do "CALL git status".
The important thing to remember is that the expanded text must look exactly like it would if you were to simply key in the command from the command line. (actually there are a few exceptions, but this is a good starting point).
To debug your script, simply put an echo before your call: #echo call %1. Now try running as you did earlier: blah.bat "echo 'hello'" produces call "echo 'hello'". Try running that from the command line - it doesn't work. You want call echo 'hello'.
One fix would be to change your script slightly: The ~ modifier strips enclosing quotes from the argument
#echo off
call %~1
Or you might be able to ditch the call and simply use the following (as long as you are not calling another batch file from which you want to return)
#echo off
%~1
If there are no other arguments on the command line, you might be better off using %* which expands to all the arguments
#echo off
%*
REM or call %*
Now you can call your batch like so
blah.bat echo "hello"
Be aware that batch has all kinds of special case weirdness that will likely require extra or different coding to work around. Too many to list - just expect the unexpected.
It looks like the problem may be that you have surrounding quotes in your input, which you'll need to stop it being broken into the different %n arguments, but try:
%~1 Which will strip any surrounding quotes from the input.
%~$PATH:1 which will strip any surrounding quotes then search within the $PATH env-var for the first match, then expand the string to include the full path to the command, which won't work for git using the windows distro because git is a batch file, and cmd would look for git status.bat
If its to be used with git, you may as well use %~1 and %~2 to call git then provide the argument to the git batch file, or call git.exe directly by modifying your $PATH. But remember that git.bat does some enviroment setup of its own before calling git itself.
I think you'll need %1% to echo the parameters. Here's my lame script which I think does what you want, works with your echo test:
bla echo hello
Gives:
C:\tmp>echo bla
bla
C:\tmp>echo echo
echo
C:\tmp>CALL echo hello
hello
echo %0%
echo %1%
CALL %*
If you want to parse through the command line arguments, let me know.
The problem is the spaces between the parameters are throwing you off (which is why you were using the quotes around git status).
Modify your bla.bat to iterate through your command line paremeters. This works for me:
SETLOCAL ENABLEDELAYEDEXPANSION
SET VAR1=
FOR %%A IN (%*) DO (
SET VAR1=!VAR1! %%A
)
call %VAR1%
ENDLOCAL
Then, run your bla.bat without the quotes around git status.
bla git status
Essentially, what this does is iterate through your command line parameters, and then executes them all as one command. The challenge comes from FOR loops in DOS not allowing you to use a variable that you're setting within itself, so you need to enable "delayed expansion" of variables. Then, the variable that you're setting needs to be encapsulated in exclamation points (not %'s). And of course, the space between !VAR1! and %%A keeps the parameters from running together in the CALL.
I got a batchfile with a length of 4776 lines. Now i want the batchfile to show the commands and text from the batchfile itself I've written.
Anticipated thanks
VVW
I like adding this at the beginning of my batch scripts:
#ECHO OFF
IF /i {%1}=={ECHO} ECHO ON&SHIFT
This checks if the first argument is "ECHO" (case-insensitive). If it is, it turns ECHO ON, and then shifts the arguments such that %2 becomes %1, %3 becomes %2.
This way the script can behave as though you ran it normally, but with ECHO ON.
Remove the below line from the batch file.
#echo off
Or make it on by doing
#echo on
Try putting your code in a variable, then print it.
mycode = %Code Code Code%
echo %mycode%
Try putting this ain your batch file.
#type %0
Maybe I am misunderstanding things but it seems like everyone is misunderstanding where the "#" symbol is necessary. If you have this:
#echo off
Then you run commands like this:
#call mycommand.bat
But, if you have this at the top of your file:
echo off
Then, your commands are silenced like so:
call mycommand.bat
You could just open the command line and type this
type yourbatchfilename.bat >> code.txt
This would write your whole thing again in a txt file