19

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
2
  • You try turning ECHO on? Oct 13, 2011 at 16:51
  • it's actually on, isnt it? it should echo the move and ren command invokations.
    – PA.
    Oct 13, 2011 at 16:55

6 Answers 6

21

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.

1
  • When I try this, the script never gets past the exit /b 0 line Apr 4, 2023 at 3:14
16

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
6
  • 1
    Ok so I just tried adding lines line this: echo >> results.txt %FILE%.gif moved to %ACCOUNT% Doing them like that made the batch file create a directory named for every .gif and .txt in the folder... :/
    – Chris V.
    Oct 13, 2011 at 17:19
  • You have to turn ECHO OFF, and then echo your comment text, not the actual command. See my update above. Oct 13, 2011 at 17:34
  • I'm not sure I follow. I tried that and instead of appending the echo to the results.txt file, it created a directory called "Directory". (The text I attempted to echo was "Directory created.")
    – Chris V.
    Oct 13, 2011 at 17:47
  • I don't know what "appending the echo to the results.txt file" means. Why not add to your post with the new version of your bat file with the ECHOs Oct 13, 2011 at 19:04
  • 1
    Every time a move, rename, delete, or a mkdir occurs, I need to append that action to the results.txt file. As I was doing mkdir, for example....ECHO Directory %ACCOUNT% created. >> results.txt That was leading to the batch file making a directory for every single text file I was iterating over. You also said 'run [the redirect/output to file] from command line', but what I'm trying to do is output to the file each time within the batch file it performs that action. So I should only be running the single batch file once and everything happens in the file.
    – Chris V.
    Oct 13, 2011 at 20:50
6

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
1
  • 2
    Thanks for explaining the difference between > and >> Feb 16, 2017 at 4:57
3

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

3

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".

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.