Using a batch file I wanted to:
1) open notepad
2) write something in notepad and save it
is it possible to do so. How can you do that?
Using a batch file I wanted to:
1) open notepad
2) write something in notepad and save it
is it possible to do so. How can you do that?
In the batch file use following to start Windows Notepad with a text file of your choice on which the user can enter text which is processed further by the batch file once the user exited Windows Notepad and saved the entered text to the file.
@echo off
rem Create a text file with 0 bytes.
type NUL >"%TEMP%\UserText.txt"
rem Start Windows Notepad with that empty text file and halt
rem execution of batch file until user finished typing the
rem text and exiting Notepad with saving the text file.
%SystemRoot%\notepad.exe "%TEMP%\UserText.txt"
rem Delete the text file if its file size is still 0.
for %%I in ("%TEMP%\UserText.txt") do if %%~zI == 0 del "%TEMP%\UserText.txt" & goto :EOF
rem Do something with the text file like printing the text.
type "%TEMP%\UserText.txt"
rem Finally delete the text file no longer needed.
del "%TEMP%\UserText.txt"
pause
But if the batch file should create itself a text file, there is no need for using Windows Notepad at all as demonstrated by this code:
@echo off
(
echo This is a demo on how text can be written into a text file.
echo/
echo The command ECHO is used to output text to console which is redirected
echo with redirection operator ^> into a file which is created always new
echo with overwriting the text file if already existing by chance.
echo/
echo See the Microsoft article "Using command redirection operators" with URL
echo https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490982(v=technet.10^)
echo for details.
) >"%TEMP%\UserText.txt"
rem Do something with the text file like printing the text.
type "%TEMP%\UserText.txt"
rem Finally delete the text file no longer needed.
del "%TEMP%\UserText.txt"
pause
Note: Some characters must be escaped with caret character ^
to be interpreted as literal characters on processing the ECHO command lines by Windows command processor. The redirection operators <>|&
must be escaped with ^
and also )
if the command line is inside a command block starting with (
and ending with matching )
which is the closing round bracket not escaped with ^
and not written inside a double quoted argument string.
The percent sign %
must be escaped with one more percent sign in a batch file to be interpreted as literal character and not as begin of a batch file argument reference, as begin of a loop variable reference or as begin/end of an environment variable reference. And an exclamation mark !
must be escaped with two carets, i.e. with ^^
if delayed environment variable expansion is enabled which is not the case by default.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
del /?
echo /?
for /?
goto /?
if /?
pause /?
rem /?
set /?
type /?
See also How does the Windows Command Interpreter (CMD.EXE) parse scripts?