Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing Batch file results to a text file

I've created a simple batch file to reorganize a set of files/folders. It's working as it should, but I need to print the results to a log file. I need to output the results of each action (creating a directory, moving a file, rename/deleting a file). When I use command >> results.txt all I can get out of it is "1 file(s) moved." a ton of times. Here's the code:

FOR %%a IN (C:\scans\*.txt) DO CALL :read %%~na
TREE /f >> tree.txt
@GOTO :EOF

:read
@set FILE=%1
FOR /f "tokens=1,2" %%b IN (%FILE%.txt) DO CALL :makeDir %%b %%c
@GOTO :EOF

:makeDir
@set ACCOUNT=%1
@set CHECK=%2
mkdir %ACCOUNT%
CALL :move
@GOTO :EOF

:move
MOVE %FILE%.gif %ACCOUNT%
REN %ACCOUNT%\%FILE%.gif %CHECK%.gif
DEL %FILE%.txt
@GOTO :EOF

How can I print to the log file (results.txt) whenever an action is performed?

EDIT: new code w/ echoes:

@echo off
FOR %%a IN (C:\scans\*.txt) DO CALL :read %%~na
TREE /f >> tree.txt
@GOTO :EOF

:read
@set FILE=%1
FOR /f "tokens=1,2" %%b IN (%FILE%.txt) DO CALL :makeDir %%b %%c
@GOTO :EOF

:makeDir
@set ACCOUNT=%1
@set CHECK=%2
mkdir %ACCOUNT%
@echo Made directory for %ACCOUNT% >> results.txt
CALL :move
@GOTO :EOF

:move
MOVE %FILE%.gif %ACCOUNT%
@echo %FILE% moved to %ACCOUNT%
REN %ACCOUNT%\%FILE%.gif %CHECK%.gif
@echo %ACCOUNT%\%FILE% renamed %CHECK% >> results.txt
DEL %FILE%.txt
@echo %FILE% deleted. >> results.txt
@GOTO :EOF
like image 248
Chris V. Avatar asked Oct 13 '11 16:10

Chris V.


People also ask

How do I save a batch file as a text?

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 I redirect output to a file in Windows?

To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.


3 Answers

You can add this piece of code to the top of your batch file:

@Echo off
SET LOGFILE=MyLogFile.log
call :Logit >> %LOGFILE% 
exit /b 0

:Logit
:: The rest of your code
:: ....

It basically redirects the output of the :Logit method to the LOGFILE. The exit command is to ensure the batch exits after executing :Logit.

like image 99
Radix Avatar answered Oct 10 '22 03:10

Radix


There's nothing wrong with your redirection of standard out to a file. Move and mkdir commands do not output anything. If you really need to have a log trail of those commands, then you'll need to explicitly echo to standard out indicating what you just executed.

The batch file, example:

@ECHO OFF
cd bob
ECHO I just did this: cd bob

Run from command line:

myfile.bat >> out.txt

or

myfile.bat > out.txt
like image 34
Stealth Rabbi Avatar answered Oct 10 '22 02:10

Stealth Rabbi


For Print Result to text file

we can follow

echo "test data" > test.txt

This will create test.txt file and written "test data"

If you want to append then

echo "test data" >> test.txt
like image 37
Nikunj K. Avatar answered Oct 10 '22 03:10

Nikunj K.