[command-line] 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

This question is related to command-line batch-file redirect

The answer is


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

Have you tried moving DEL %FILE%.txt% to after @echo %FILE% deleted. >> results.txt so that it looks like this?

@echo %FILE% deleted. >> results.txt
DEL %FILE%.txt

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.


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

Step 1: Simply put all the required code in a "MAIN.BAT" file.

Step 2: Create another bat file, say MainCaller.bat, and just copy/paste these 3 lines of code:

REM THE MAIN FILE WILL BE CALLED FROM HERE..........
CD "File_Path_Where_Main.bat_is_located"
MAIN.BAT > log.txt

Step 3: Just double click "MainCaller.bat".

All the output will be logged into the text file named "log".


For showing result of batch file in text file, you can use

this command

chdir > test.txt

This command will redirect result to test.txt.

When you open test.txt you will found current path of directory in test.txt


Examples related to command-line

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Flutter command not found Angular - ng: command not found how to run python files in windows command prompt? How to run .NET Core console app from the command line Copy Paste in Bash on Ubuntu on Windows How to find which version of TensorFlow is installed in my system? How to install JQ on Mac by command-line? Python not working in the command line of git bash Run function in script from command line (Node JS)

Examples related to batch-file

'ls' is not recognized as an internal or external command, operable program or batch file '' is not recognized as an internal or external command, operable program or batch file XCOPY: Overwrite all without prompt in BATCH CanĀ“t run .bat file under windows 10 Execute a batch file on a remote PC using a batch file on local PC Windows batch - concatenate multiple text files into one How do I create a shortcut via command-line in Windows? Getting Error:JRE_HOME variable is not defined correctly when trying to run startup.bat of Apache-Tomcat Curl not recognized as an internal or external command, operable program or batch file Best way to script remote SSH commands in Batch (Windows)

Examples related to redirect

React-Router External link Laravel 5.4 redirection to custom url after login How to redirect to another page in node.js How to redirect to an external URL in Angular2? How to redirect to a route in laravel 5 by using href tag if I'm not using blade or any template? Use .htaccess to redirect HTTP to HTTPs How to redirect back to form with input - Laravel 5 Using $window or $location to Redirect in AngularJS yii2 redirect in controller action does not work? Python Requests library redirect new url