如何处理2个FOR循环之后批处理? [英] How to process 2 FOR loops after each other in batch?

查看:219
本文介绍了如何处理2个FOR循环之后批处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是两个 FOR 循环分开工作,但不想一个接一个地工作。



第一个循环创建XML文件,只有当创建完成后,第二个循环才会启动并计算创建的XML文件的大小并将其写入.txt文件









$ 'dir / b / s C:\ Users \NekhayenkoO\test\'do(
echo Verarbeite %%〜na
jhove -m PDF-hul -h xml -o C: \ Users \NekhayenkoO\outputxml\ %%〜na.xml %% a


为/ f %%我在('dir / b / s C:\\ (
echo %%〜ni %%〜zi>> C:\ Users \NekhayenkoO\outputxml\size.txt


暂停


解决方案

这个问题在知道<$ c $时很容易回答所以我在万维网上搜索 jhove ,很快找到了主页 JHOVE | JSTOR / Harvard对象验证环境,也可以从SourceForge项目下载 jhove-1_11.zip JHOVE



所有这一切都是由我来完成的,找出 jhove 是一个Java应用程序,在Linux上运行,也可能在Mac上使用shell脚本 jhove ,在Windows上执行批处理文件 jhove.bat PATH ,用于匹配文件名为 jhove。* 的文件,文件扩展名列于环境变量 PATHEXT ,因为 jhove.bat 指定了没有文件扩展名,但是,从批处理文件中执行批处理文件,而不使用c ommand CALL 会导致当前批处理文件的脚本执行在另一个执行的批处理文件中继续,而不会返回到当前的批处理文件。



出于这个原因,Windows命令解释器运行到 C:\ Users \NekhayenkoO\test jhove.bat

通过使用两个简单的批处理文件,例如 C:\ Temp



Test1.bat:

<$ p

$ cd $ D $%$ d $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b echo%〜n0:离开%〜f0

Test2.bat:

  @echo%〜n0:参数是:%* 
@echo%〜n0:离开%〜f0 $从命令提示符窗口> C:\ Temp \ Test 1内运行时b $ b

.bat 输出结果是:


Test2:参数是:Test1.bat

Test2:离开C:\Temp\Test2.bat




Test1.bat 的处理在 Test2.bat 没有回到 Test1.bat



现在 Test1.bat do 之后插入命令 CALL ,$ c>被修改为

Test1.bat:

  @echo off 
cd / D%〜dp0
为%%我在(* .bat)中调用Test2.bat%% I
echo离开%〜f0

在命令提示符窗口中运行 Test1.bat 的输出现在是:


Test2:参数是:Test1.bat

Test2:离开C:\Temp\Test2.bat

Test2:参数是:Test2.bat

Test2:离开C:\Temp\Test2.bat

Test1:离开C:\Temp\Test1.bat


批处理文件 Test1.bat 调用现在批处理文件 Test2.bat FOR 循环确实在两个批处理文件的目录中找到的所有* .bat文件上执行。



因此解决方案正在使用 Squashman 建议的命令 CALL


$ ('dir / b / s%USERPROFILE%)

$
$ set $ $







$ b $ \ test \2 ^> nul')do(
echo Verarbeite %%〜na
call jhove.bat -m PDF-hul -h xml -o%USERPROFILE%\outputxml \ %%〜na.xml%% a


for / f %% i('dir / b / s%USERPROFILE%\outputxml\
echo %%〜ni %%〜zi>>%USERPROFILE%\outputxml\size.txt


pause
endlocal

对环境变量 USERPR的引用使用OFILE 来代替 C:\ Users \NekhayenkoO



全部文件名用双引号括起来,以防目录中的任何文件包含空格字符或任何其他需要用双引号引起来的特殊字符。



和last <添加了code> 2> nul ,通过命令 DIR 重定向错误消息输出以处理 STDERR NUL 来压制它。重定向操作符> 必须在这里用 ^ 转义,以执行命令 DIR ,而不是在解析命令 FOR 时放置重定向操作符。



为了解所使用的命令及其工作方式,请打开命令提示符窗口,在那里执行下列命令,并仔细阅读为每个命令显示的所有帮助页面。


  • call /?

  • cd /?
  • code> dir /?
  • echo /?



请阅读微软文章使用命令重定向操作符


My problem is that two FOR loops are working separately, but don't want to work one after another.

The goal is:

The first loop creates XML files and only when the creation has already been done the second loop starts and counts the size of created XML files and writes it into .txt file.

@echo off
Setlocal EnableDelayedExpansion

for /f %%a in ('dir /b /s C:\Users\NekhayenkoO\test\') do (
    echo Verarbeite %%~na
    jhove -m PDF-hul -h xml -o C:\Users\NekhayenkoO\outputxml\%%~na.xml %%a
)

for /f %%i in ('dir /b /s C:\Users\NekhayenkoO\outputxml\') do (
    echo %%~ni %%~zi >> C:\Users\NekhayenkoO\outputxml\size.txt
)

pause

解决方案

This question can be answered easily when knowing what jhove is.

So I searched in world wide web for jhove, found very quickly the homepage JHOVE | JSTOR/Harvard Object Validation Environment and downloaded also jhove-1_11.zip from SourceForge project page of JHOVE.

All this was done by me to find out that jhove is a Java application which is executed on Linux and perhaps also on Mac using the shell script jhove and on Windows the batch file jhove.bat for making it easier to use by users.

So Windows command interpreter searches in current directory and next in all directories specified in environment variable PATH for a file matching the file name pattern jhove.* having a file extension listed in environment variable PATHEXT because jhove.bat is specified without file extension and without path in the batch file.

But the execution of a batch file from within a batch file without usage of command CALL results in script execution of current batch file being continued in the other executed batch file without ever returning back to the current batch file.

For that reason Windows command interpreter runs into jhove.bat on first file found in directory C:\Users\NekhayenkoO\test and never comes back.

This behavior can be easily watched by using two simple batch files stored for example in C:\Temp.

Test1.bat:

@echo off
cd /D "%~dp0"
for %%I in (*.bat) do Test2.bat "%%I"
echo %~n0: Leaving %~f0

Test2.bat:

@echo %~n0: Arguments are: %*
@echo %~n0: Leaving %~f0

On running from within a command prompt window C:\Temp\Test1.bat the output is:

Test2: Arguments are: "Test1.bat"
Test2: Leaving C:\Temp\Test2.bat

The processing of Test1.bat was continued on Test2.bat without coming back to Test1.bat.

Now Test1.bat is modified to by inserting command CALL after do.

Test1.bat:

@echo off
cd /D "%~dp0"
for %%I in (*.bat) do call Test2.bat "%%I"
echo Leaving %~f0

The output on running Test1.bat from within command prompt window is now:

Test2: Arguments are: "Test1.bat"
Test2: Leaving C:\Temp\Test2.bat
Test2: Arguments are: "Test2.bat"
Test2: Leaving C:\Temp\Test2.bat
Test1: Leaving C:\Temp\Test1.bat

Batch file Test1.bat calls now batch file Test2.bat and therefore the FOR loop is really executed on all *.bat files found in directory of the two batch files.

Therefore the solution is using command CALL as suggested already by Squashman:

@echo off
setlocal EnableDelayedExpansion

for /f %%a in ('dir /b /s "%USERPROFILE%\test\" 2^>nul') do (
    echo Verarbeite %%~na
    call jhove.bat -m PDF-hul -h xml -o "%USERPROFILE%\outputxml\%%~na.xml" "%%a"
)

for /f %%i in ('dir /b /s "%USERPROFILE%\outputxml\" 2^>nul') do (
    echo %%~ni %%~zi>>"%USERPROFILE%\outputxml\size.txt"
)

pause
endlocal

A reference to environment variable USERPROFILE is used instead of C:\Users\NekhayenkoO.

All file names are enclosed in double quotes in case of any file found in the directory contains a space character or any other special character which requires enclosing in double quotes.

And last 2>nul is added which redirects the error message output to handle STDERR by command DIR on not finding any file to device NUL to suppress it. The redirection operator > must be escaped here with ^ to be interpreted on execution of command DIR and not as wrong placed redirection operator on parsing already the command FOR.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • cd /?
  • dir /?
  • echo /?
  • for /?

And read also the Microsoft article Using command redirection operators.

这篇关于如何处理2个FOR循环之后批处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆