How to make a batch file using CMD? - windows

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

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

Windows command line using directory and EXE file parameters

I have been trying to run an application I built and output it to a file. However, I am running into problems with the command line arguments required to do this.
This is an example of my problem using ipconfig.
The following command works:
ipconfig > output.txt
Whereas this will create the file, but not populate it with the ipconfig output:
start /D "C:\>WINDOWS\system32" ipconfig.exe > output.txt
I think it is the use of start that is causing this issue, but I'm not sure.
SOLUTION
This is the code which managed to solve the problem for me:
char path[500]; // Create character array
strcpy (path, "cd "); // Copy 'cd' into the array
strcat (path, toolLocation); // Copy the path of the tool into the array
strcat (path, " & ip.exe > output.txt"); // Append on the name of the exe and output to a file
system (path); // Run the built array
I am creating a character array and then appending to it. The vital bit here was the & being used in the system call. This is working as an and and first cd'ing to the directory before executing the .exe file.
In your command, the > is redirecting the output of start rather than the output of ipconfig. That explains why you are seeing nothing – start is simply not outputting anything.
Based on the comments to the question, you can achieve your goals with ShellExecute like this:
ShellExecute(
0,
"open",
"cmd.exe",
"/C ipconfig > output.txt",
NULL,
SW_HIDE
);
Rather than using start I think you might want to use cd to change the directory.
Try this batch file:
cd "C:\Program Files\Tools\2012"
ip.exe >output.txt
Or for use without a batch and just command line:
"C:\Program Files\Tools\2012" ip.exe >output.txt"
Although system32 is in PATH so I'm not sure why you are accessing the ipconfig exe by it's full path, but this should work.
The error is this:
start /D "C:\>WINDOWS\system32" ipconfig.exe > output.txt
should be
start /D "C:\WINDOWS\system32" ipconfig.exe > output.txt
without > in the path. Although C:\> is shown at the prompt with cmd.exe it is not part of the path name and > is actually invalid for the purpose, to my knowledge.
Additionally I would strongly suggest you use:
start /D "%SystemRoot%\system32" ipconfig.exe > output.txt
Furthermore because start creates a new console (and new stderr and stdout) you are catching the output of start not of ipconfig. So you may wanna use:
pushd "%SystemRoot%\system32" & ipconfig.exe > output.txt & popd
but that will attempt to write output.txt into %SystemRoot%\system32 and will fail on most systems unless you are admin. So give an absolute path or simply leave out the crud:
ipconfig.exe > output.txt
ipconfig.exe is always in the default system PATH variable, so it will work unless the admin has "fixed" the system in which case you can still do:
%SystemRoot%\system32\ipconfig.exe > output.txt

How can I write a null ASCII character (nul) to a file with a Windows batch script?

I'm attempting to write an ASCII null character (nul) to a file from a Windows batch script without success. I initially tried using echo like this:
echo <Alt+2+5+6>
which seems like it should work (typing <Alt+2+5+6> in the command window does write a null character - or ^# as it appears), but echo then outputs:
More?
and hangs until I press <Return>. As an alternative I tried using:
copy con tmp.txt >nul
<Alt+2+5+6><Ctrl+Z>
which does exactly what I need, but only if I type it manually in the command window. If I run it from a batch file it hangs until I press <Ctrl+Z> but even then the output file is created but remains empty.
I really want the batch file to stand alone without requiring (for example) a separate file containing a null character which can be copied when needed.
Okay, this was tricky, and the solution is ugly, but it works.
You can use the batch file itself as the file containing the null character to be copied.
This batch file, called null.bat:
findstr /v /r \n null.bat >> myfile.txt
[NULL]
(where the last line contains only the null character) appends the null character to myfile.txt.
findstr /v /r shows all lines that aren't matched by the regex, i.e. only the last one, because there's no newline character.
I have tried several other things, but this was the only one I could find that didn't strip the null character.
Note: findstr was first shipped with Windows 2000 so may not be available on prior versions of Windows
I don't like the solution that cannot be easy copy/paste-d to text files.So few more ways:
1) mshta (can be directly used in batch file or from command line):
mshta vbscript:execute("CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write( Chr(00) ):Close")>testfile
2) certutil (requires a temp file)
echo 00>null.hex
certutil -decodehex null.hex null.bin
3) makecab just check the subroutine here - http://ss64.com/nt/syntax-genchr.html
Here's an article which describes how to write arbitrary bytes with a batch file (search for h2b.com). This effectively solves the problem of writing any non-printable data using a batch script (including null).
An alternative to the accepted answer which doesn't involve having to put null characters into the batch file is as follows:
#echo off
set NULL_FILE=null.txt
set DEBUG_COMMANDS=write-null.dbg
echo e 100 >%DEBUG_COMMANDS%
echo 0 >>%DEBUG_COMMANDS%
echo n %NULL_FILE% >>%DEBUG_COMMANDS%
echo rbx >>%DEBUG_COMMANDS%
echo 0 >>%DEBUG_COMMANDS%
echo rcx >>%DEBUG_COMMANDS%
echo 1 >>%DEBUG_COMMANDS%
echo w >>%DEBUG_COMMANDS%
echo q >>%DEBUG_COMMANDS%
debug < %DEBUG_COMMANDS% >nul
del %DEBUG_COMMANDS%
This is obviously more verbose and also has the downside that it won't work on Win64 machines (due to debug no longer being available in those environments).
create null file of size <size>:
fsutil file createnew <file> <size>

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

Resources