将批处理文件结果打印到文本文件

发布于 2024-12-09 10:50:47 字数 1188 浏览 0 评论 0原文

我创建了一个简单的批处理文件来重新组织一组文件/文件夹。它正常工作,但我需要将结果打印到日志文件中。我需要输出每个操作的结果(创建目录、移动文件、重命名/删除文件)。当我使用命令>>时results.txt 我能从中得到的只是“1 个文件已移动”。很多次。代码如下:

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

每当执行操作时,如何打印到日志文件(results.txt)?

编辑:带有回显的新代码:

@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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心如狂蝶 2024-12-16 10:50:47

您可以将这段代码添加到批处理文件的顶部:

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

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

它基本上将 :Logit 方法的输出重定向到 LOGFILEexit命令用于确保批处理在执行:Logit后退出。

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.

池予 2024-12-16 10:50:47

将标准输出重定向到文件没有任何问题。 Move 和 mkdir 命令不输出任何内容。如果您确实需要这些命令的日志跟踪,那么您需要显式回显到标准输出,指示您刚刚执行的内容。

批处理文件,例如:

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

从命令行运行:

myfile.bat >> out.txt

myfile.bat > out.txt

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
南汐寒笙箫 2024-12-16 10:50:47

对于将结果打印到文本文件,

我们可以遵循

echo "test data" > test.txt

这将创建 test.txt 文件并写入“测试数据”

如果您想附加然后

echo "test data" >> test.txt

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
萤火眠眠 2024-12-16 10:50:47

要在文本文件中显示批处理文件的结果,可以使用

此命令

chdir > test.txt

。此命令会将结果重定向到 test.txt。

当您打开 test.txt 时,您将在 test.txt 中找到目录的当前路径

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

伴我心暖 2024-12-16 10:50:47

第1步:只需将所有必需的代码放入“MAIN.BAT”文件中。

步骤 2:创建另一个 bat 文件,例如 MainCaller.bat,然后复制/粘贴以下 3 行代码:

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

步骤 3:只需双击“MainCaller.bat”。

所有输出都将记录到名为“log”的文本文件中。

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

逆流 2024-12-16 10:50:47

您是否尝试过将 DEL %FILE%.txt% 移至 @echo %FILE% 删除之后。 >>> results.txt 看起来像这样?

@echo %FILE% deleted. >> results.txt
DEL %FILE%.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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文