Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append text with .bat

I want to create a log of every operation processed in a batch file and used the following but to no avail. How do I fix it (the file was not created)?

REM>> C:\"VTS\ADVANCED TOOLS\SYSTEM\LOG\Advanced tools %date%.log" 
like image 721
Mac Avatar asked Feb 28 '11 07:02

Mac


People also ask

How do I append to a batch file?

Content writing to files is also done with the help of the double redirection filter >>. This filter can be used to append any output to a file. Following is a simple example of how to create a file using the redirection command to append data to files.

How do I save a .TXT file as a .bat file?

Click File and then Save, and then navigate to where you want to save the file. For the file name, type test. bat and if your version of Windows has a Save as type option, choose All files, otherwise it saves as a text file. Once you have completed these steps, click the Save button and exit notepad.

How do you add text to a file in CMD?

Use the echo command, used with the append redirection operator, to add a single line of text to a file. This adds the message Remember to back up mail files by end of week. to the end of the file notes.


2 Answers

You need to use ECHO. Also, put the quotes around the entire file path if it contains spaces.

One other note, use > to overwrite a file if it exists or create if it does not exist. Use >> to append to an existing file or create if it does not exist.

Overwrite the file with a blank line:

ECHO.>"C:\My folder\Myfile.log" 

Append a blank line to a file:

ECHO.>>"C:\My folder\Myfile.log" 

Append text to a file:

ECHO Some text>>"C:\My folder\Myfile.log" 

Append a variable to a file:

ECHO %MY_VARIABLE%>>"C:\My folder\Myfile.log" 
like image 120
aphoria Avatar answered Oct 11 '22 22:10

aphoria


I am not proficient at batch scripting but I can tell you that REM stands for Remark. The append won't occur as it is essentially commented out.

http://technet.microsoft.com/en-us/library/bb490986.aspx

Also, the append operator redirects the output of a command to a file. In the snippet you posted it is not clear what output should be redirected.

like image 45
Eric Avatar answered Oct 11 '22 22:10

Eric