如何使用可变路径名对文件名进行硬编码?

时间:2016-10-06 05:37:23

标签: batch-file

首先,我想道歉,因为我的编程知识很少而且我的手指交叉,我不是在浪费你的时间。

我的问题是我有一系列文件夹,包含图像文件及其所有辅助文件。图像文件及其所有辅助文件具有相同的名称。在一个文件夹中可以有许多图像文件(及其相应的辅助文件)。

我需要做的是将图像文件,它的辅助文件,免责声明文件和自述文件一起压缩。这是它变得棘手的地方。整个批次只有一个免责声明文件,因此它位于根目录中。给我悲伤的部分是自述。每个文件夹只有一个自述文件,它总是被命名为“readme.txt”。每个图像没有单独的readme.txt - 每个图像文件夹只有一个。但是,一个readme.txt需要在zip文件中输入其文件夹中的所有图像。

我递归遍历每个文件夹的代码获取每个图像,它的辅助文件,免责声明,并将它们全部拉到一起。我似乎无法做到的是在每个文件夹中找到自述文件并将其拉入其中。

我认为问题是我无法弄清楚如何将路径名称变为变量并以硬编码readme.txt结束。

编辑:目录结构如下:

  • 文件夹A
    • disclaimer.txt
    • 文件夹B
      • 文件夹C
        • a.tfw
        • a.tif
        • a.aux.xml
        • A.XML
        • b.tfw
        • b.tif
        • b.aux.xml
        • B.XML
        • c.tfw
        • c.tif
        • c.aux.xml
        • c.xml
        • README.TXT
        • 文件夹D
          • d.tfw
          • d.tif
          • d.aux.xml
          • d.xml
          • e.tfw
          • e.tif
          • e.aux.xml
          • e.xml
          • f.tfw
          • f.tif
          • f.aux.xml
          • f.xml
          • README.TXT

这是我的代码:

@echo off

for /r %%F in (*.tfw) DO "C:\Program Files\7-Zip\7Z.exe" a "%%F.zip" "%%~fF" "%%~dpnF.tif"  "%%~dpnF.xml" "%%~dpnF.tif.xml" "%%~dpnF.aux.xml" "readme.txt" "C:\temp\disclaimer.txt"

for /r %%a in (*) do (
    for %%f in ("%%~na") do (
        ren %%~a %%~nf%%~xa
    )
)

XCOPY *.zip d:\new_aerials /s

1 个答案:

答案 0 :(得分:0)

我建议将此注释批处理代码用于此任务:

@echo off
setlocal EnableDelayedExpansion

rem Search for file disclaimer.txt and exit the batch processing with
rem an error message if this file cannot be found in directory tree
rem of the current directory.

for /R %%I in (disclaimer.txt*) do (
    set "DisclaimerFile=%%~fI"
    goto FindReadme
)
set "FileName=disclaimer.txt"
goto ErrorMessage

rem Search for first file readme.txt and exit the batch processing
rem with an error message if not a least 1 readme file can be found
rem in directory tree of the current directory.

:FindReadme
for /R %%I in (readme.txt*) do (
    set "ReadmeFile=%%~fI"
    goto CompressFiles
)
set "FileName=readme.txt"

:ErrorMessage
echo Could not find the file "%FileName%" in directory
echo.
echo    %CD%
echo.
echo or any subdirectory of this directory.
echo.
endlocal
pause
goto :EOF

rem Compress into a ZIP file each *.tfw file found in any directory of
rem the directory tree of current directory with all files with same
rem name in same directory as the *.tfw file with the disclaimer.txt
rem found before and the readme file in same directory as the *.tfw
rem file or last found readme.txt. The ZIP file is created in same
rem directory as the *.tfw file with same name as the *.tfw file.

rem Last copy all *.zip files with duplicating the directory tree
rem to the directory D:\new_aerials

:CompressFiles
for /R %%I in (*.tfw) do (
    if exist "%%~dpIreadme.txt" set "ReadmeFile=%%~dpIreadme.txt"
    "%ProgramFiles%\7-Zip\7z.exe" a -tzip -x^^!*.zip -mx=9 -mtc=off -y "%%~dpnI.zip" "%%~dpnI.*" "!ReadmeFile!" "%DisclaimerFile%" >nul
)
%SystemRoot%\System32\xcopy.exe *.zip D:\new_aerials\ /Q /R /S /Y >nul
endlocal

有关使用过的7-Zip交换机的详细信息,请查看7-Zip帮助文件7zip.chm。需要使用!在此处转义感叹号^^,因为在运行压缩时会启用延迟展开。

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?
  • xcopy /?

另请参阅Microsoft文章Using command redirection operators,以便重定向7z.exexcopy.exe输出的消息,以便 STDOUT 处理设备 NUL 隐藏那些输出消息。

相关问题