So, when I'm echoing text from a batch file to a text file, I can escape the special characters without issue. However, when I echo a certain character combination, I can't seem to find a way around the fact that it is just echoing to the screen and not to the file.
Here's what works...
echo firsttest^=uncpath>>test.txt
echo [secondtest]>>test.txt
Here's what doesn't work...
echo thirdtest^=1>>test.txt
echo fourthtest^=2>>test.txt
I've tried escaping one and both of the arrow characters, but still no-go.
Any advice? I'm sure it has to do with the standard redirection of console output using the "1>" and the "2>", but just don't know how to get around that.
Thanx.
Your analysis is correct. There are two main concepts to work around it:
(echo thirdtest=1)>>test.txt
(echo fourthtest=2)>>test.txt
and
>>test.txt echo thirdtest=1
>>test.txt echo fourthtest=2
(Note: = isn't one of the special characters that need escaping)
Where in the line you put your redirection doesn't matter, as running with echo on shows. (if you enjoy to get confused, enter echo hello>file.txt world, followed by type file.txt)
Another note: if you echo several lines, it's faster to:
(
echo first line
echo second line
echo third line
)>>test.txt
(reason: needs only one disk access (read/modify/write) instead of doing the same for each single line) You won't notice it with just three lines, but think of writing hundreds of lines (for example in a loop). Time savings will be huge.
I'm looking over some batch files written by a coworker, and I ran into some weird syntax where he calls redirection operator at the beginning of his command.
This:
> output.txt ECHO something like this
rather than:
ECHO something like this > output.txt
It threw me off for a second. It works, so I assume its valid. I went to look for an official reference to confirm, but could not find one.
I did find this reference on the usage of the operator itself.
I found this bash version of what I'm looking for linked to here: POSIX sh EBNF grammar
What is the correct syntax for the use of the > and >> operators in batch?
Edit:
To clarify, I'm not looking for examples as documentation. I would like to see documentation explaining how cmd.exe commands and .bat files are parsed specifically in regards to redirection operators.
The official documentation for command redirection operators demonstrates their use at the end of the line. But putting the redirection at the beginning of the line is no less correct (piped output notwithstanding).
The benefit of putting the redirect at the beginning of an echo line is that you avoid echoing a trailing space into the file.
echo Hello world! > out.txt
15 bytes, includes a trailing space.
> out.txt echo Hello world!
14 bytes, does not include a trailing space
You can also use parentheses to avoid the trailing space.
(echo Hello world!) > out.txt
14 bytes
Parentheses can also be used to group the output of several commands without having to close and reopen the file handle for each line.
rem // option 1
> out.txt (
echo Hello world!
echo Another line
)
rem // option 2
(
echo Hello world!
echo Another line
) > out.txt
Both of those parenthetical code blocks perform the exact same action. And both are more efficient than this:
rem // option 3
>out.txt echo Hello world!
>>out.txt echo Another line
The parenthetical examples in options 1 and 2 above open, write, and close "out.txt" only one time; whereas the option 3 example opens, writes, closes, opens for appending, writes, and closes. When dumping a lot of data to a text file, such optimizations can make a difference.
I was wondering: is it possible to write the content of a variable (in my case, the last search) to a file with a command?
I tried the following:
:echo #/ >> /tmp/foo.txt
:#/w /tmp/foo.txt
But that didn't work. Any idea on what is the correct way to do this?
An alternative solution to romain’s proposal is to use the redir command which redirects messages to a file. As described in :help redir,
The messages which are the output of commands are written to that file,
until redirection ends.
To append the contents of the search register, run the following sequence of commands:
redir >> /tmp/foo.txt
echo #/
redir END
This sequence could be turned into a function and/or used as a key mapping.
I want pass specific parameters(aaa|bbb|ccc) but it's not working.
Here's what the command line looks like:
test.bat aaa|bbb|ccc
and test.bat looks like:
echo %1
but its not working cuz test.bat receive only 'aaa'.
how can I pass whole parameter 'aaa|bbb|ccc'?
ps. I must use this format : 'aaa|bbb|ccc'
there is no option to change like aaa_bbb_ccc.. etc.
Using the "|" means you use the pipe function.
http://ss64.com/nt/syntax-redirection.html
You should use "aaa|bbb|ccc" so that it will read everything between the quotation marks.
I hope it helps
The VERTICAL BAR is the pipe character to command.com and cmd.exe. If you want to use is literally, it must be escaped using a CARET.
K:>echo aaa^|bbb^|ccc
aaa|bbb|ccc
Actually, I think I might prefer the quoting suggestion as it appears to work under bash and perhaps other shells.
$ echo "aaa|bbb|ccc"
aaa|bbb|ccc
$ echo 'aaa|bbb|ccc'
aaa|bbb|ccc
Can somebody remember what was the command to create an empty file in MSDOS using BAT file?
copy NUL EmptyFile.txt
DOS has a few special files (devices, actually) that exist in every directory, NUL being the equivalent of UNIX's /dev/null: it's a magic file that's always empty and throws away anything you write to it. Here's a list of some others; CON is occasionally useful as well.
To avoid having any output at all, you can use
copy /y NUL EmptyFile.txt >NUL
/y prevents copy from asking a question you can't see when output goes to NUL.
echo. 2>EmptyFile.txt
This redirects output stream 2 (stderr) to a file. The command echo doesn't output anything to stderr, so the file becomes empty.
Plain echo would work too, but echo. is better because it doesn't print the useless and potentially confusing message ECHO is on.
type NUL > EmptyFile.txt
After reading the previous two posts, this blend of the two is what I came up with. It seems a little cleaner. There is no need to worry about redirecting the "1 file(s) copied." message to NUL, like the previous post does, and it looks nice next to the ECHO OutputLineFromLoop >> Emptyfile.txt that will usually follow in a batch file.
Techniques I gathered from other answers:
Makes a 0 byte file a very clear, backward-compatible way:
type nul >EmptyFile.txt
idea via: anonymous, Danny Backett, possibly others, myself inspired by JdeBP's work
A 0 byte file another way, it's backward-compatible-looking:
REM. >EmptyFile.txt
idea via: Johannes
A 0 byte file 3rd way backward-compatible-looking, too:
echo. 2>EmptyFile.txt
idea via: TheSmurf
A 0 byte file the systematic way probably available since Windows 2000:
fsutil file createnew EmptyFile.txt 0
idea via: Emm
A 0 bytes file overwriting readonly files
ATTRIB -R filename.ext>NUL
(CD.>filename.ext)2>NUL
idea via: copyitright
A single newline (2 bytes: 0x0D 0x0A in hex notation, alternatively written as \r\n):
echo.>AlmostEmptyFile.txt
Note: no space between echo, . and >.
idea via: How can you echo a newline in batch files?
edit It seems that any invalid command redirected to a file would create an empty file. heh, a feature!
compatibility: uknown
TheInvisibleFeature <nul >EmptyFile.txt
A 0 bytes file: invalid command/ with a random name (compatibility: uknown):
%RANDOM%-%TIME:~6,5% <nul >EmptyFile.txt
via: great source for random by Hung Huynh
edit 2 Andriy M points out the probably most amusing/provoking way to achieve this via invalid command
A 0 bytes file: invalid command/ the funky way (compatibility: unknown)
*>EmptyFile.txt
idea via: Andriy M
A 0 bytes file 4th-coming way:
break > file.txt
idea via: foxidrive thanks to comment of Double Gras!
REM. > empty.file
If there's a possibility that the to be written file already exists and is read only, use the following code:
ATTRIB -R filename.ext
CD .>filename.ext
If no file exists, simply do:
CD .>filename.ext
(updated/changed code according to DodgyCodeException's comment)
To supress any errors that may arise:
ATTRIB -R filename.ext>NUL
(CD .>filename.ext)2>NUL
One more to add to the books - short and sweet to type.
break>file.txt
break>"file with spaces in name.txt"
fsutil file createnew file.cmd 0
You can use a TYPE command instead of COPY. Try this:
TYPE File1.txt>File2.txt
Where File1.txt is empty.
There are infinite approaches.
Commands that output nothing:
break
cls
color
goto
pushd
popd
prompt
title
Weird Commands:
CD.
REM.
#echo off
cmd /c
START >FILE
The outdated print command produces a blank file:
print /d:EMPTY_TEXT_FILE nul
You can also use SET to create a null byte file as follows
set x=x > EmptyFile.txt
Or if you don't want to create an extra variable reassign an existing variable like
set PROMPT=%PROMPT% > EmptyFile.txt
or like this:
set "PROMPT=%PROMPT%" > EmptyFile.txt
The easiest way is:
echo. > Filename.txt
IMPORTANT:
If you don't set the encoding, many softwares can break. git is a very popular example.
Set-Content "your_ignore_file.txt" .gitignore -Encoding utf8 this is case-sensitive and forces utf8 encoding!