Create file command in batch files (*.bat) - windows

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 message? 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.

Related

What is the meaning of cd. > for creating empty file in windows CMD?

"What is the meaning of cd. > for creating empty file in windows CMD?"
In my view point CD means change directory but why i have to put .> for creating empty file in windows CMD?
C:\Users\smith\Desktop\web>cd. >index.html
An alternative way to create a zero (0) length file. This seems more explicit and probably more quickly understood.
COPY NUL nnn.txt
or
TYPE NUL >nnn2.txt
> redirects output from the preceding operation to a file handle. Unfortunately, there's no straighforward way to output an empty string with echo in cmd, so if you use:
echo. > newfile.txt
for example, you end up with newfile.txt containing a trailing new-line.
cd. "changes" the location to, well, the current location (so effectively does nothing) and then it outputs... well, nothing! The effect being that:
cd. > newfile.txt
results in an empty text file

Is there a command to create files using cmd on windows?

I learnt that I can use NUL> but it is working strangely. Whenever I use it, it reports "Access is denied" but the file is created.
You're getting access denied since you're trying to run the nul device (whether you redirect standard output or not is irrelevant):
c:\pax> nul
Access is denied.
c:\pax> nul >myfile.txt
Access is denied.
What you need to do to get an empty file, although there are other ways to do it, is send the output of the nul device to your file, with something like:
c:\pax> type nul >myfile.txt
c:\pax> dir myfile.txt
Volume in drive C has no label.
Volume Serial Number is DEAD-BEEF
Directory of c:\pax
21/06/2018 04:57 PM 0 myfile.txt
1 File(s) 0 bytes
0 Dir(s) 31,415,926,535,902,718,281,828,459 bytes free
Only that first line above is needed to create an empty file, the rest is just to show that it worked.
Just run:
echo.>your_file.txt
copy con abc.txt
and type file content. Use Ctrl+Z to finish
mkdir thisisafile
cd thisisafile
echo.>suchtextmuchwow.txt
echo . >thisisatext.txt
notepad anothertext.txt
copy nul "iambored.txt"
type NUL > 1.txt
Note : using the " notepad " command will give you an error saying " you don't have such a file, do you want to create it? " while the "echo.>" command will directly create.
Note : if you type "echo.>" it'll create a txt file with a single line break but no characters inside, but if you type "echo . >" it'll create a text file with a dot ( . )in it.
Note : If you use "copy nul" the problem with that command is that it will always display that the file was copied and in order to avoid that you can also try the following command: type NUL > 1.txt
you need to try this one it will create new file
echo.>filename
example
echo.>new.txt

win cmd - A string in a text file to be read while being run in a command

I remember doing this exercise back in college, but I can't recall exactly how o do this.
You save a string (could be your password, certain input shortcuts, etc) in a text file. Then, with your cmd prompt, you write your command, but instead of manually typing out the string (in this case, its a LONG string and randomized), you load it from the said text file automatically and when executing the command, it will read the content of the text file to automatically input for you.
First, let me say that this is NOT a good thing to do with a password.
Put the password into a file.
ECHO>magic.txt wh2084nJ)&322ng0adf
Get the password back from the file and use it.
SET /P "MYPASSWORD=" <magic.txt
ECHO %MYPASSWORD%
EDIT:
Create a .bat script called RUNMYPROGRAM.bat with the following contents.
#ECHO OFF
SET /P "USERNAME=" <username.txt
SET /P "PASSWORD=" <password.txt
login username %USERNAME% password %PASSWORD%

DOS batch : Different behaviour between command line and drag and drop

I'm trying to write the first argument of a command line in a file, but it works in command line and not with drag and drop.
The very simple batch file (test_echo.cmd) is as following:
#echo OFF
echo %1 > echo_arg_in_file.txt`
On the command line,
C:\rep>test_echo.cmd "C:\rep\fichier de test.txt"`
creates a file echo_arg_in_file.txt with "C:\rep\fichier de test.txt" written inside.
But with a drag and drop of the file "C:\rep\fichier de test.txt" on the batch file, nothing happens... (the test to delete > echo_arg_in_file.txt was done before and displays well "C:\rep\fichier de test.txt")
Any explanation?
I'm not sure about your precise environment, but if I have to bet, current active directory is the problem
Replace your test_echo.cmd with
#echo off
for %%a in (.) do echo %%~fa
pause
Then execute the file both by double clicking it and by drag/drop a file. In both cases you will see the current active directory for the started cmd process.
Why is this relevant? As you have not included a path in the original file redirect, this file will be created in the current active directory that, maybe, could not be what you expect.
You can find more information here
For a quick solution,
#echo OFF
> "%~dp0\echo_arg_in_file.txt" echo %1
that will create the file in the same folder that hold the batch file
What Windows' version. Vista can't drag and drop into a command prompt for security reasons. Restricted possibilities are on later versions (cause we all whinged).
Prior to Vista it was the same as typing the file name if dragged into the window.
If talking about a shortcut each file is one parameter (use shift command to handle this).

How to make a batch file using CMD?

Can someone please tell me how I can create a batch file (empty) using CMD? I tried below, didn't work.
echo C:\Users\Yohan\Desktop\test > test.bat
From your command prompt:
type NUL>test.bat
or
copy NUL test.bat
Here NUL does not refer to the ASCII NUL (character of ASCII integer value zero). It's a system reserved word for NUL device (imagine a fake file of zero length).
echo.>>mybatchfile.bat
Note that this will create a file containing a single newline.
If you want to Create the file in C:\Users\Yohan\Desktop\test
First use the CD command to change the current directory, then try. or else it will create in system32 (if you run the cmd from accessories)
Try the below :
> cd C:\Users\Yohan\Desktop\test then
> echo empty >a.bat
Thats it.
From your command prompt:
copy con test.bat
[ctrl]+z
The [ctrl]+z is the End of File marker, closing and saving the empty file.
Basically, you are just copying with the source being the CONsole and the destination being the .BAT file.
just to add another possibility to create an empty file (0 Byte):
break>mybatchfile.bat
The concept behind the creation of an empty file is simple: take nothing and put it in a file. A way to do that is using a command that produce no output and redirect its output to the file, so you just need to know which commands produce no output.
In the old MS-DOS command.com days, that command was rem:
rem Create an empty file > empty.txt
However, such functionality was removed in Windows cmd.exe. Other commands that show nothing and works in Windows are:
break > empty.txt
call > empty.txt
cd . > empty.txt
color > empty.txt
endlocal > empty.txt
setlocal > empty.txt
shift > empty.txt
title > empty.txt
verify off > empty.txt
The problem with these commands is that they do a certain task, so using they to create an empty file seems strange. For example, goto command also show nothing:
goto nextLine > empty.txt
:nextLine
Another example:
(if a == b echo Create an empty file) > empty.txt
Other command that show nothing is exit, so you may write a subroutine that create an empty file with the name given in its parameter this way:
:CreateEmptyFile
exit /B > %1
So, which command should we use? Well, in my particular case I prefer that the purpose of the command be perfectly clear, so I choose an auto-documented set command:
set dummyVar=Create an empty file > empty.txt

Resources