I'm trying to compile a very simple command in a .bat file.
whoami > "Desktop/laptop.txt"
However, when I run it, cmd.exe interprets it as:
whoami 1> "Desktop/laptop.txt"
This is invalid due to the number 1 added before the >. The command does not run.
I can't find a way to get this number 1 to go away.
To conclude what said in comments, the command line interpreter, interprets > (redirection character) and >> (append character) as 1> and 1>> respectively if they are alone, e.g. echo sth > filename or echo sth >> filename.
That is because the numeric handles are as follows:
0 redirects/appends STDIN to somewhere specified.
1 redirects/appends STDOUT to somewhere specified.
2 redirects/appends STDERR to somewhere specified
3-9 are undefined
So, when you type your command:
whoami > "Desktop/laptop.txt"
interpreter cannot understand it and automatically makes it 1> ... because it assumes you want to redirect STDOUT to Desktop/laptop.txt.
See some interesting links for further reading:
https://ss64.com/nt/syntax-redirection.html
https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490982(v=technet.10)
https://stackoverflow.com/a/4095133 and especially phase 2
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 am creating a batch script that creates and writes to a VBScript. I am writing to the file like this:
echo (code for vbscript) >> [name of vbscript].vbs
However, there is one part of the code that is troubling me:
echo If Len(m) > 1 then >> sys.vbs
Since the code itself has a > symbol in it, the batch file interprets it as me trying to write to the VBScript and cuts off the code, but that is not the case. I want the code to say If Len(m) > 1 Then but instead it just says If Len(m) then.
I already tried working around this by modifying the code to say:
echo if NOT Len(m) < 1 then >> sys.vbs
but that does not work either. If I do that, then it omits the whole line of code altogether.
I'm sorry if I didn't explain this problem well enough, it's hard to describe.
<, > and >> are redirection operators in batch. As #rojo already pointed out in the comments you need to escape them with a caret (^) if you want to echo them as literal characters in the output file:
echo If Len(m) ^> 1 Then >> sys.vbs
Using double quotes around the echoed string does not work in this case, because echo includes the double quotes in the output.
However, I agree with #Tomalak that you may want to reconsider your approach. Generating VBScript from a batch file on the fly is usually not a good/clean way of solving whatever problem you have on your hands.
Hello stackoverflow users!
I'm not really new to batch. I just never used pipes | in batch and even after I read reference on ss64.com I don't understand what's the pipe used for.
At first I thought it is OR operator or something (obviously I know now it's not).
I only know that it's located between two lines (commands) like &, but I still don't get what it does exactly, and how it is used practically in code.
Thanks for answering!
Pipe [|]: Redirect standard output of commandA to standard input of commandB
http://www.robvanderwoude.com/redirection.php
example :
echo KKZiomek | find "KKZ"
will redirect the echo KKZiomek in the input of the FIND and be used as second parameter of it.
Like well commented by #aschipfl the space is piped too.
so better use :
echo KKZiomek| find "KKZ"
The pipe is used to send the output of one command to the input of another command.
For example, del /p will ask for confirmation when you delete files. However, you can pipe echo y to it to send a y to the del command and del will act as if the user had pressed y.
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!