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

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

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

Oneliner to get / read the serial buffer and return the value to the standard output

I'm pretty sure this question should have been asked before, but I can't find the answer. I want to read the content of the serial buffer and echo it to the console / terminal / standard output.
Requirements:
CMD / batch commands, no powershell
it must be a oneliner
preferably using copy, type, more and other simple commands like these.
Attempts:
A. First assuming I have set the parameters:
mode COM7:9600,N,8,1,P
Then from here I have tried
copy COM7 CON
and
type COM7
but it just doesn't return anything.
B. Also answers on this page are for copying the content of the serial buffer to a file, but I need to print it to the terminal or at least store it in a variable.
C. From here I also tried:
set /P "var=" < COM7 & echo %var%
but it just returns %var% obviously not reading the buffer. Adding the cmd /V:ON /C at the beginning of the above command also did not help.
D. from this page
copy COM7 > CON
and
copy COM7 2>&1
and
copy COM7 | more
all led to the error:
Access is denied.
0 file(s) copied.
I would appreciate if you could help me find the right command. Thanks for your help in advance.

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

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

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