Create file command in batch files (*.bat)
I want to create a file named "new text document.txt" in the folder %tv%
using a batch file ( *.bat
). This is my batch file:
set tv=D:\prog\arpack96\ARPACK\SRC
cd "%tv%"
@CON >> "new text document.txt"
set tv=
Although I can really create the file in %tv%
, but when I run the above batch file, I will get an error message saying
' ' is not recognized as an internal or external command, operable program or batch file.
Is there anyway to get rid of this error mess开发者_开发问答age? Or I am creating the file using the wrong command?
In order to get a truly empty file without having to provide user interaction, you can use the set /p
command with a little trickery:
set tv=c:\documents and settings\administrator
cd "%tv%"
<nul >"new text document.txt" (set /p tv=)
The set /p
asks the user for input into the tv
variable after outputting the prompt after the =
character.
Since the prompt is empty, no prompt is shown. Since you're reading from nul, it doesn't wait for user interaction. And you can store the empty string straight into tv
thus removing the need to unset it.
Actually, after some more thought, there's an easier way. I've used that set /p
trick in the past since it's the only way I know of echoing text without a newline being added (great for progress bars in the console). But if all you want is an empty file, you can probably get away with:
copy /y nul "new text document.txt"
The copy
simply copies the contents of the nul
device (effectively an empty file) to your new file name. The /y
is to ensure it's overwritten without bothering the user.
type nul > file.txt
file.txt should be created empty
or paxdiablo's answer
Create an empty file from a *.bat file - this worked for me.
echo off > test.txt
con is the windows specialized file name and it should not be used.
copy con >> filename.txt
shall ask you to enter the text and you can save the text by typing Ctrl Z.
精彩评论