将批处理文件结果打印到文本文件
我创建了一个简单的批处理文件来重新组织一组文件/文件夹。它正常工作,但我需要将结果打印到日志文件中。我需要输出每个操作的结果(创建目录、移动文件、重命名/删除文件)。当我使用命令>>时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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以将这段代码添加到批处理文件的顶部:
它基本上将
:Logit
方法的输出重定向到LOGFILE
。exit
命令用于确保批处理在执行:Logit
后退出。You can add this piece of code to the top of your batch file:
It basically redirects the output of the
:Logit
method to theLOGFILE
. Theexit
command is to ensure the batch exits after executing:Logit
.将标准输出重定向到文件没有任何问题。 Move 和 mkdir 命令不输出任何内容。如果您确实需要这些命令的日志跟踪,那么您需要显式回显到标准输出,指示您刚刚执行的内容。
批处理文件,例如:
从命令行运行:
或
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:
Run from command line:
or
对于将结果打印到文本文件,
我们可以遵循
这将创建 test.txt 文件并写入“测试数据”
如果您想附加然后
For Print Result to text file
we can follow
This will create test.txt file and written "test data"
If you want to append then
要在文本文件中显示批处理文件的结果,可以使用
此命令
。此命令会将结果重定向到 test.txt。
当您打开 test.txt 时,您将在 test.txt 中找到目录的当前路径
For showing result of batch file in text file, you can use
this command
This command will redirect result to test.txt.
When you open test.txt you will found current path of directory in test.txt
第1步:只需将所有必需的代码放入“MAIN.BAT”文件中。
步骤 2:创建另一个 bat 文件,例如 MainCaller.bat,然后复制/粘贴以下 3 行代码:
步骤 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:
Step 3: Just double click "MainCaller.bat".
All the output will be logged into the text file named "log".
您是否尝试过将 DEL %FILE%.txt% 移至 @echo %FILE% 删除之后。 >>> results.txt 看起来像这样?
Have you tried moving DEL %FILE%.txt% to after @echo %FILE% deleted. >> results.txt so that it looks like this?