Writing >Nul into a file [duplicate] - batch-file

This question already has an answer here:
Writing special characters from one batch file to another
(1 answer)
Closed 3 years ago.
I am currently very new to .bat and I was messing around trying to make one batch file write another one. So far I have been fine, but I have run into the error of not being able to put >Nul into the other batch file.
I'm trying to add a simple invisible delay, for example:
ECHO PING localhost -n 4 >NUl >> Test.bat
In that text, it usually treats the >NUL as part of the code and not something that will be written into the next file.
Sorry if my wording is confusing, I am just new and do not know the best way to explain this sort of thing.

The caret ^ is the Windows escape character so you would use:
>>test.bat echo ping localhost -n 4 ^>nul
Note that I've moved the actual redirection up to the front. This isn't technically necessary but it prevents extraneous spaces from appearing at the start or end of the line in your file (all spaces before and after the >>test.bat are placed in the file if it's not before the echo).

Related

how do i get echo to type out a % in a batch file [duplicate]

This question already has answers here:
BATCH- echo % (print an percent sign) [duplicate]
(1 answer)
Escape percent in bat file
(1 answer)
Ignore percent sign in batch file
(6 answers)
How does the Windows Command Interpreter (CMD.EXE) parse scripts?
(8 answers)
What is the difference between % and %% in a cmd file?
(2 answers)
Closed 6 days ago.
I want the code below to work or something that does the same thing.
#echo off
echo 100%
pause
When you open the .bat file it shuld look like this.
100%
Press any key to continue . . .
So i want the echo to read out the % and not to be a part of a code.
I want it to work the same as for example
#echo off
echo hi
pause
I have only tried echo 100% because i am a noob at coding so i dont know what else to try.
The percent sign has special meaning in (Windows) batch files, it is used to indicate a variable. For instance, echo %username% will print the contents of the username variable.
To echo a literal % sign, you need to double it, i.e. echo 100%%

Timeout command in batch

I want to create a batch file with a timer in it, using another batch file. In the first one I wrote
type NUL > File2.bat
echo Timeout /T 5 /NOBREAK>NUL>> "File1.bat"
A batch file called File1.bat is made, but the "> NUL" is missing. It only contains.
Timeout /T 5 /NOBREAK
Can someone please assist me?
Insert a caret character (^) before the >. This "escapes" the special meaning of the >.
Best to use a text editor to create batch files. I use editplus ut there are many others, including notepad++.
Don't use a word-processor, and be very careful if you use notepad (as supplied with windows) as these are aimed more at word-processing and automatically format the file created as a document rather than a simple text file.

Echo command with parenthesis in batch file

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.

Works in Command Prompt but not in BAT-file or CMD-file [duplicate]

This question already has answers here:
Why does FOR loop command not work in a batch file which works on Windows command prompt?
(3 answers)
Closed 4 years ago.
Due to problem with WINDOWS DEFENDER I need do the following:
for /r "%appdata%\..\Local\MyProg\2.0" %D IN ("*MyProgram.exe") DO "%~fD
It works perfect in COMMAND PROMPT but not in batch-file or cmd-file - why?
How do I make in a excutable file as a Batch-file or ".cmd"?
When running for loops in a batch file, you need to use an additional % in predefined variables. So it should be:
for /r "%appdata%\..\Local\MyProg\2.0" %%D IN ("*MyProgram.exe") DO "%%~fD
I suggest you read up using the well documented help by running for /? from cmdline. You will benefit from it, guaranteed!
A recursive For loop already returns the full path, additionally there's already a system variable for the path %AppData%\..\Local.
For /R "%LocalAppData%\MyProg\2.0" %%A In ("*MyProgram.exe") Do "%%A"
Depending upon your needs, it may be worth checking out the Start command usage too, Start /?. You may find that Do Start "" "%%A" is what you needed.

redirecting a stream for the entire .bat file inside itself

I've recently started working with .bat files, and I'm trying to redirect the output to a file.
I've found 2 options, so far:
echo aaa > out.txt - which sends the output of the single echo command to the specified file (can also be appended using >>)
calling the entire file from the cmd using somefile.bat > out.txt (which is actually similar to number 1, as it sends the output of the single command somefile.bat to out.txt)
What I'm looking for is something else - I'm trying to have a line in my file that sends all the output from that point forth to the file.
Thanks!
echo this goes to screen
(
echo this line goes to the file
echo also this line and the ping-output
ping www.stackoverflow.com
echo and this
)>file.txt
echo this goes to screen again
Note:
all inside the block (between ( and )) is parsed at once. If you use variables inside the block, you may need delayed expansion.
There is no universal solution. It depends of the batch file requirements.
For a lot of batch files, the answer from Stephan will work without problems, taking in consideration what he pointed: all the code is inside a block and any variable management inside it may require delayed expansion.
Other alternative is to move the code under a subroutine, calling it with the redirection
#echo off
call :mainProcess %* > outputFile
exit /b
:mainProcess
:: here the batch file begins
echo %1 %2 %3

Resources