在子目录中批处理 运行.exe

Batch Running .exe in subdirectories

所以我有一个 exe (xmllint.exe) 可以漂亮地打印特定子目录中的一些 XML 文件。 xmllint.exe 在我的主目录中,也就是我的脚本所在的位置。这是我目前所拥有的

setlocal enabledelayedexpansion

for /D /r %%d in (./*targetfolder) do (
    pushd %%d
    for %%x in (*.xml) do (
        ::Get the filename, without the .old-extension
        set "filename=%%~nx"
        set "extension=.xml"

        ren %%x %%~x.old

        ::Concatenate filename and extension
        set "finalname=!filename!!extension!"
        xmllint.exe %%x >> !finalname!

        del %%~x.old
    )
    popd
)

当然它不会运行子目录中的xmllint.exe,因为它找不到。

您正在查找的是 %~dp0 参见 call /? of for /?

但是我不明白,如果你在 xmlint 之前重命名文件,你将如何在 xmlint 中使用 %%x

@echo off
setlocal enabledelayedexpansion

rem :: set the target folder first with:
set "targetfolder=c:\path\of\target"

for /D /r %%a in (%targetfolder%\*.xml) do (

    rem :: make a copy
    copy "%%~a" "%%~dpna.old"

    "%~dp0\xmllint.exe" "%%~dpna.old">> "%%~a"

    del "%%~dpna.old"
)

编辑,更改 FOR /R 现在应该可以工作了。

@echo off
rem :: set the target folder first with:
set "targetfolder=c:\path\of\target"
for /R %targetfolder% %%a in (*.xml) do (
    rem :: make a copy
    copy "%%~a" "%%~dpna.old"
    "%~dp0xmllint.exe" "%%~dpna.old">> "%%~a"
    del "%%~dpna.old"
)

重要提示:您必须更改行:

"%~dp0xmllint.exe" "%%~dpna.old">> "%%~a"

"%~dp0xmllint.exe" "%%~dpna.old"> "%%~a"

通过只保留一个 > 它将重新创建文件而不是附加到文件。