检查批处理文件中的相对路径树

Checking Relative Path Trees in Batch Files

提问人:Cole Wintringham 提问时间:8/5/2023 最后编辑:Cole Wintringham 更新时间:8/5/2023 访问量:37

问:

我正在开发辐射 4 武器模组,为了提高效率,我正在尝试将我的 .tga 纹理自动压缩为 .dds 文件。我已经完成了大部分内容,但我想通过将 .tga 文件的相对路径与根路径进行比较来自动分配相对路径。就目前而言,我为接收器自动化了该过程,并且效果很好。但是,我完成它的方式需要太多的 IF 语句。我希望能够将任何给定文件的相对目录路径作为变量分配给 .bat 的目录,以便复制源文件夹结构。

我目前的.bat如下

@echo off

:Check
:: I CHECK IF THE PATHS ARE CORRECT
:: AND PREVIEW THE COMMAND TO COMPRESS 

:: CHECK FOR DIFFUSE MAPS TO BE ENCODED AS BC3 .DDS FILES
FOR /r %%i IN (*_d.tga) DO (
    setlocal EnableDelayedExpansion
    :: I GET THE FULL PATH OF THE .TGA FILE
    set "thisFilePath=%%~dpnxi"
    :: I LOCATE THE COMPRESSION PROGRAM 
    set "compressonatorCLIPath=C:\Compressonator_4.4.19\bin\CLI\compressonatorcli.exe"
    :: I SPECIFY THE LOCATION OF MY WORKING .DDS FILES
    set "outputTexturePath= E:\SteamLibrary\steamapps\common\Fallout 4\Working\FROST_AR15_Canada\HiPoly\GameFileSystem\Textures\Cerebral\Diemaco_AR15\"
    :: I CAPTURE THE PARENT FOLDER OF THE .TGA FILE
    for %%p in ("%%~dpi\.") do set "foundFileSubDir=%%~nxp\"
    :: HERE IS WHERE I AM STUCK
    :: THE IF COMMANDS ARE TEMPORARY WORKAROUNDS
    :: FOR EXAMPLE ANY FILE IN THIS DIRECTORY'S "\Receivers\C7_Lower\" SUBDIR NEEDS TO BE SENT TO THE OUTPUT DIR'S  
    if "!foundFileSubDir!"=="C7_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else if "!foundFileSubDir!"=="C8_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else (set "thisFileSubDir=!foundFileSubDir!")
    :: I STRIP THE .TGA EXTENSION
    set "thisFileName=%%~ni"
    :: I NOW COMPILE EVERYTHING FROM ABOVE TO GET
    set "fullOutputPath=!outputTexturePath!!thisFileSubDir!!thisFileName!.dds"
    @echo !compressonatorCLIPath! "!thisFilePath!" "!fullOutputPath!" -fd BC3  -Quality 1.0000  -NumThreads 6 
    endlocal
)
:: CHECK FOR NORMAL AND SPEC MAPS TO BE ENCODED AS BC5 .DDS FILES
FOR /r %%i IN (*_s.tga *_n.tga *_ndx.tga) DO (
    ::SAME LOGIC AS THE DIFFUSE MAPS
    setlocal EnableDelayedExpansion
    set "thisFilePath=%%~dpnxi"1
    set "compressonatorCLIPath=C:\Compressonator_4.4.19\bin\CLI\compressonatorcli.exe"
    set "outputTexturePath="E:\SteamLibrary\steamapps\common\Fallout 4\Working\FROST_AR15_Canada\HiPoly\GameFileSystem\Textures\Cerebral\Diemaco_AR15\"
    for %%p in ("%%~dpi\.") do set "foundFileSubDir=%%~nxp\"
    if "!foundFileSubDir!"=="C7_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else if "!foundFileSubDir!"=="C8_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else (set "thisFileSubDir=!foundFileSubDir!")
    set "thisFileName=%%~ni"
    set "fullOutputPath=!outputTexturePath!!thisFileSubDir!!thisFileName!.dds"
    @echo !compressonatorCLIPath! "!thisFilePath!" "!fullOutputPath!" -fd BC5  -Quality 1.0000  -NumThreads 6 
    endlocal
)
choice /M "Are paths correct?"
:: IF THE COMPRESSION COMMANDS HAVE THE RIGHT DESTINATIONS I PROCEED
if %errorlevel%==1 goto Continue
:: IF THEY DO NOT I SKIP THE COMPRESSION PROCESS TO AVOID WRITING TEXTURES TO THE WRONG DESTINATIONS
if %errorlevel%==2 goto Abort

:Continue
:: SAME PROCESS AS :Check BUT THE COMPRESSION COMMAND IS NOW RUN INSTEAD OF ECHOED
FOR /r %%i IN (*_d.tga) DO (
    setlocal EnableDelayedExpansion
    set "thisFilePath=%%~dpnxi"
    set "compressonatorCLIPath=C:\Compressonator_4.4.19\bin\CLI\compressonatorcli.exe"
    set "outputTexturePath=E:\SteamLibrary\steamapps\common\Fallout 4\Working\FROST_AR15_Canada\HiPoly\GameFileSystem\Textures\Cerebral\Diemaco_AR15\"
    for %%p in ("%%~dpi\.") do set "foundFileSubDir=%%~nxp\"
    if "!foundFileSubDir!"=="C7_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else if "!foundFileSubDir!"=="C8_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else (set "thisFileSubDir=!foundFileSubDir!")
    set "thisFileName=%%~ni"
    set "fullOutputPath="!outputTexturePath!!thisFileSubDir!!thisFileName!".dds"
    !compressonatorCLIPath! "!thisFilePath!" !fullOutputPath! -fd BC3  -Quality 1.0000  -NumThreads 6 
    endlocal
)
:: PROGRESS NOTIFICATION, WANT A PROGRESS BAR LATER
@echo BC3 files compressed!
FOR /r %%i IN (*_s.tga *_n.tga *_ndx.tga) DO (
    setlocal EnableDelayedExpansion
    set "thisFilePath=%%~dpnxi"
    set "compressonatorCLIPath=C:\Compressonator_4.4.19\bin\CLI\compressonatorcli.exe"
    set "outputTexturePath=E:\SteamLibrary\steamapps\common\Fallout 4\Working\FROST_AR15_Canada\HiPoly\GameFileSystem\Textures\Cerebral\Diemaco_AR15\"
    for %%p in ("%%~dpi\.") do set "foundFileSubDir=%%~nxp\"
    if "!foundFileSubDir!"=="C7_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else if "!foundFileSubDir!"=="C8_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else (set "thisFileSubDir=!foundFileSubDir!")
    set "thisFileName=%%~ni"
    set "fullOutputPath="!outputTexturePath!!thisFileSubDir!!thisFileName!".dds"
    !compressonatorCLIPath! "!thisFilePath!" !fullOutputPath! -fd BC5  -Quality 1.0000  -NumThreads 6 
    endlocal
)
:: PROGRESS NOTIFICATION, WANT A PROGRESS BAR LATER
@echo BC5 files Compressed!
:: ALL.TGA FILES HAVE NOW BEEN PROPERLY ENCODED 
goto :end

:Abort
:: NO COMPRESSION, PROCESS ABORTED
goto :end

:end
::END OF FILE

或使用 REM

@echo off

:Check
REM I CHECK IF THE PATHS ARE CORRECT
REM AND PREVIEW THE COMMAND TO COMPRESS 

REM CHECK FOR DIFFUSE MAPS TO BE ENCODED AS BC3 .DDS FILES
FOR /r %%i IN (*_d.tga) DO (
    setlocal EnableDelayedExpansion
    REM I GET THE FULL PATH OF THE .TGA FILE
    set "thisFilePath=%%~dpnxi"
    REM I LOCATE THE COMPRESSION PROGRAM 
    set "compressonatorCLIPath=C:\Compressonator_4.4.19\bin\CLI\compressonatorcli.exe"
    REM I SPECIFY THE LOCATION OF MY WORKING .DDS FILES
    set "outputTexturePath= E:\SteamLibrary\steamapps\common\Fallout 4\Working\FROST_AR15_Canada\HiPoly\GameFileSystem\Textures\Cerebral\Diemaco_AR15\"
    REM I CAPTURE THE PARENT FOLDER OF THE .TGA FILE
    for %%p in ("%%~dpi\.") do set "foundFileSubDir=%%~nxp\"
    REM HERE IS WHERE I AM STUCK
    REM THE IF COMMANDS ARE TEMPORARY WORKAROUNDS
    REM FOR EXAMPLE ANY FILE IN THIS DIRECTORY'S "\Receivers\C7_Lower\" SUBDIR NEEDS TO BE SENT TO THE OUTPUT DIR'S  
    if "!foundFileSubDir!"=="C7_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else if "!foundFileSubDir!"=="C8_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else (set "thisFileSubDir=!foundFileSubDir!")
    REM I STRIP THE .TGA EXTENSION
    set "thisFileName=%%~ni"
    REM I NOW COMPILE EVERYTHING FROM ABOVE TO GET
    set "fullOutputPath=!outputTexturePath!!thisFileSubDir!!thisFileName!.dds"
    @echo !compressonatorCLIPath! "!thisFilePath!" "!fullOutputPath!" -fd BC3  -Quality 1.0000  -NumThreads 6 
    endlocal
)
REM CHECK FOR NORMAL AND SPEC MAPS TO BE ENCODED AS BC5 .DDS FILES
FOR /r %%i IN (*_s.tga *_n.tga *_ndx.tga) DO (
    REMSAME LOGIC AS THE DIFFUSE MAPS
    setlocal EnableDelayedExpansion
    set "thisFilePath=%%~dpnxi"1
    set "compressonatorCLIPath=C:\Compressonator_4.4.19\bin\CLI\compressonatorcli.exe"
    set "outputTexturePath="E:\SteamLibrary\steamapps\common\Fallout 4\Working\FROST_AR15_Canada\HiPoly\GameFileSystem\Textures\Cerebral\Diemaco_AR15\"
    for %%p in ("%%~dpi\.") do set "foundFileSubDir=%%~nxp\"
    if "!foundFileSubDir!"=="C7_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else if "!foundFileSubDir!"=="C8_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else (set "thisFileSubDir=!foundFileSubDir!")
    set "thisFileName=%%~ni"
    set "fullOutputPath=!outputTexturePath!!thisFileSubDir!!thisFileName!.dds"
    @echo !compressonatorCLIPath! "!thisFilePath!" "!fullOutputPath!" -fd BC5  -Quality 1.0000  -NumThreads 6 
    endlocal
)
choice /M "Are paths correct?"
REM IF THE COMPRESSION COMMANDS HAVE THE RIGHT DESTINATIONS I PROCEED
if %errorlevel%==1 goto Continue
REM IF THEY DO NOT I SKIP THE COMPRESSION PROCESS TO AVOID WRITING TEXTURES TO THE WRONG DESTINATIONS
if %errorlevel%==2 goto Abort

:Continue
REM SAME PROCESS AS :Check BUT THE COMPRESSION COMMAND IS NOW RUN INSTEAD OF ECHOED
FOR /r %%i IN (*_d.tga) DO (
    setlocal EnableDelayedExpansion
    set "thisFilePath=%%~dpnxi"
    set "compressonatorCLIPath=C:\Compressonator_4.4.19\bin\CLI\compressonatorcli.exe"
    set "outputTexturePath=E:\SteamLibrary\steamapps\common\Fallout 4\Working\FROST_AR15_Canada\HiPoly\GameFileSystem\Textures\Cerebral\Diemaco_AR15\"
    for %%p in ("%%~dpi\.") do set "foundFileSubDir=%%~nxp\"
    if "!foundFileSubDir!"=="C7_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else if "!foundFileSubDir!"=="C8_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else (set "thisFileSubDir=!foundFileSubDir!")
    set "thisFileName=%%~ni"
    set "fullOutputPath="!outputTexturePath!!thisFileSubDir!!thisFileName!".dds"
    !compressonatorCLIPath! "!thisFilePath!" !fullOutputPath! -fd BC3  -Quality 1.0000  -NumThreads 6 
    endlocal
)
REM PROGRESS NOTIFICATION, WANT A PROGRESS BAR LATER
@echo BC3 files compressed!
FOR /r %%i IN (*_s.tga *_n.tga *_ndx.tga) DO (
    setlocal EnableDelayedExpansion
    set "thisFilePath=%%~dpnxi"
    set "compressonatorCLIPath=C:\Compressonator_4.4.19\bin\CLI\compressonatorcli.exe"
    set "outputTexturePath=E:\SteamLibrary\steamapps\common\Fallout 4\Working\FROST_AR15_Canada\HiPoly\GameFileSystem\Textures\Cerebral\Diemaco_AR15\"
    for %%p in ("%%~dpi\.") do set "foundFileSubDir=%%~nxp\"
    if "!foundFileSubDir!"=="C7_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else if "!foundFileSubDir!"=="C8_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else (set "thisFileSubDir=!foundFileSubDir!")
    set "thisFileName=%%~ni"
    set "fullOutputPath="!outputTexturePath!!thisFileSubDir!!thisFileName!".dds"
    !compressonatorCLIPath! "!thisFilePath!" !fullOutputPath! -fd BC5  -Quality 1.0000  -NumThreads 6 
    endlocal
)
REM PROGRESS NOTIFICATION, WANT A PROGRESS BAR LATER
@echo BC5 files Compressed!
REM ALL.TGA FILES HAVE NOW BEEN PROPERLY ENCODED 
goto :end

:Abort
REM NO COMPRESSION, PROCESS ABORTED
goto :end

:end
REM END OF FILE

我遇到的问题是if语句:

if "!foundFileSubDir!"=="C7_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else if "!foundFileSubDir!"=="C8_Lower\" (set "thisFileSubDir=Receivers\!foundFileSubDir!") else (set "thisFileSubDir=!foundFileSubDir!")

虽然它有效,但它需要我为每个不是 .bat 父文件夹的直接子文件夹添加额外的 if 语句。 目前的结构是这样的:

Diemaco_AR15
├───Barrels
├───Handguards
├───Muzzles
├───Receivers
│   │   Diemaco_Receiver_01_BufferTube_d.tga
│   │   Diemaco_Receiver_01_BufferTube_n.tga
│   │   Diemaco_Receiver_01_BufferTube_ndx.tga
│   │   Diemaco_Receiver_01_BufferTube_s.tga
│   │   Diemaco_Receiver_01_CarryHandle_d.tga
│   │   Diemaco_Receiver_01_CarryHandle_n.tga
│   │   Diemaco_Receiver_01_CarryHandle_ndx.tga
│   │   Diemaco_Receiver_01_CarryHandle_s.tga
│   │   Diemaco_Receiver_01_Upper_Bits_d.tga
│   │   Diemaco_Receiver_01_Upper_Bits_n.tga
│   │   Diemaco_Receiver_01_Upper_Bits_ndx.tga
│   │   Diemaco_Receiver_01_Upper_Bits_s.tga
│   │   Diemaco_Receiver_01_Upper_d.tga
│   │   Diemaco_Receiver_01_Upper_n.tga
│   │   Diemaco_Receiver_01_Upper_ndx.tga
│   │   Diemaco_Receiver_01_Upper_s.tga
│   │   Diemaco_Receiver_02_BufferTube_d.tga
│   │   Diemaco_Receiver_02_BufferTube_n.tga
│   │   Diemaco_Receiver_02_BufferTube_ndx.tga
│   │   Diemaco_Receiver_02_BufferTube_s.tga
│   │   Diemaco_Receiver_02_CarryHandle_d.tga
│   │   Diemaco_Receiver_02_CarryHandle_n.tga
│   │   Diemaco_Receiver_02_CarryHandle_ndx.tga
│   │   Diemaco_Receiver_02_CarryHandle_s.tga
│   │   Diemaco_Receiver_02_Upper_Bits_d.tga
│   │   Diemaco_Receiver_02_Upper_Bits_n.tga
│   │   Diemaco_Receiver_02_Upper_Bits_ndx.tga
│   │   Diemaco_Receiver_02_Upper_Bits_s.tga
│   │   Diemaco_Receiver_02_Upper_d.tga
│   │   Diemaco_Receiver_02_Upper_n.tga
│   │   Diemaco_Receiver_02_Upper_ndx.tga
│   │   Diemaco_Receiver_02_Upper_s.tga
│   │   Diemaco_Receiver_03_Rail_d.tga
│   │   Diemaco_Receiver_03_Rail_n.tga
│   │   Diemaco_Receiver_03_Rail_ndx.tga
│   │   Diemaco_Receiver_03_Rail_s.tga
│   │   Diemaco_Receiver_04_BufferTube_d.tga
│   │   Diemaco_Receiver_04_BufferTube_n.tga
│   │   Diemaco_Receiver_04_BufferTube_ndx.tga
│   │   Diemaco_Receiver_04_BufferTube_s.tga
│   │   Diemaco_Receiver_04_Rail_d.tga
│   │   Diemaco_Receiver_04_Rail_n.tga
│   │   Diemaco_Receiver_04_Rail_ndx.tga
│   │   Diemaco_Receiver_04_Rail_s.tga
│   │   Diemaco_Receiver_04_Upper_Bits_d.tga
│   │   Diemaco_Receiver_04_Upper_Bits_n.tga
│   │   Diemaco_Receiver_04_Upper_Bits_ndx.tga
│   │   Diemaco_Receiver_04_Upper_Bits_s.tga
│   │   Diemaco_Receiver_04_Upper_d.tga
│   │   Diemaco_Receiver_04_Upper_n.tga
│   │   Diemaco_Receiver_04_Upper_ndx.tga
│   │   Diemaco_Receiver_04_Upper_s.tga
│   │   Diemaco_Receiver_05_BufferTube_d.tga
│   │   Diemaco_Receiver_05_BufferTube_n.tga
│   │   Diemaco_Receiver_05_BufferTube_ndx.tga
│   │   Diemaco_Receiver_05_BufferTube_s.tga
│   │   Diemaco_Receiver_05_MonoRail_Carbine_d.tga
│   │   Diemaco_Receiver_05_MonoRail_Carbine_n.tga
│   │   Diemaco_Receiver_05_MonoRail_Carbine_ndx.tga
│   │   Diemaco_Receiver_05_MonoRail_Carbine_s.tga
│   │   Diemaco_Receiver_05_MonoRail_Rifle_d.tga
│   │   Diemaco_Receiver_05_MonoRail_Rifle_n.tga
│   │   Diemaco_Receiver_05_MonoRail_Rifle_ndx.tga
│   │   Diemaco_Receiver_05_MonoRail_Rifle_s.tga
│   │   Diemaco_Receiver_05_Upper_Bits_d.tga
│   │   Diemaco_Receiver_05_Upper_Bits_n.tga
│   │   Diemaco_Receiver_05_Upper_Bits_ndx.tga
│   │   Diemaco_Receiver_05_Upper_Bits_s.tga
│   │   Diemaco_Receiver_05_Upper_d.tga
│   │   Diemaco_Receiver_05_Upper_n.tga
│   │   Diemaco_Receiver_05_Upper_ndx.tga
│   │   Diemaco_Receiver_05_Upper_s.tga
│   │
│   ├───C7_Lower
│   │       Diemaco_Receiver_01_Lower_d.tga
│   │       Diemaco_Receiver_01_Lower_n.tga
│   │       Diemaco_Receiver_01_Lower_ndx.tga
│   │       Diemaco_Receiver_01_Lower_s.tga
│   │       Diemaco_Receiver_02_Lower_d.tga
│   │       Diemaco_Receiver_02_Lower_n.tga
│   │       Diemaco_Receiver_02_Lower_ndx.tga
│   │       Diemaco_Receiver_02_Lower_s.tga
│   │       Diemaco_Receiver_03_Lower_d.tga
│   │       Diemaco_Receiver_03_Lower_n.tga
│   │       Diemaco_Receiver_03_Lower_ndx.tga
│   │       Diemaco_Receiver_03_Lower_s.tga
│   │       Diemaco_Receiver_04_Lower_d.tga
│   │       Diemaco_Receiver_04_Lower_n.tga
│   │       Diemaco_Receiver_04_Lower_ndx.tga
│   │       Diemaco_Receiver_04_Lower_s.tga
│   │       Diemaco_Receiver_05_Lower_d.tga
│   │       Diemaco_Receiver_05_Lower_n.tga
│   │       Diemaco_Receiver_05_Lower_ndx.tga
│   │       Diemaco_Receiver_05_Lower_s.tga
│   │
│   └───C8_Lower
│           Diemaco_Receiver_01_Lower_d.tga
│           Diemaco_Receiver_01_Lower_n.tga
│           Diemaco_Receiver_01_Lower_ndx.tga
│           Diemaco_Receiver_01_Lower_s.tga
│           Diemaco_Receiver_02_Lower_d.tga
│           Diemaco_Receiver_02_Lower_n.tga
│           Diemaco_Receiver_02_Lower_ndx.tga
│           Diemaco_Receiver_02_Lower_s.tga
│           Diemaco_Receiver_03_Lower_d.tga
│           Diemaco_Receiver_03_Lower_n.tga
│           Diemaco_Receiver_03_Lower_ndx.tga
│           Diemaco_Receiver_03_Lower_s.tga
│           Diemaco_Receiver_04_Lower_d.tga
│           Diemaco_Receiver_04_Lower_n.tga
│           Diemaco_Receiver_04_Lower_ndx.tga
│           Diemaco_Receiver_04_Lower_s.tga
│           Diemaco_Receiver_05_Lower_d.tga
│           Diemaco_Receiver_05_Lower_n.tga
│           Diemaco_Receiver_05_Lower_ndx.tga
│           Diemaco_Receiver_05_Lower_s.tga
│
├───Sights

因为我不想检查targa本身是否在里面,而是检查是否在.如果不是,我需要附加到变量中。IE 让变量更像 .Diemaco_AR15\Receivers\C8_Lower\Diemaco_Receiver_01_Lower_d.tgaDiemaco_AR15\Receivers\C8_Lower\C8_Lower\Diemaco_AR15\Recievers\foundFileSubDir!foundFileMainDir!\!foundFileSubDir!\

有什么办法可以做到这一点吗?

for-loop 变量 批处理文件 自动化

评论

2赞 Stephan 8/5/2023
::是(无效)标签。我知道,它经常被用作注释,它在代码块之外工作(如“忽略这一行”),但它往往会在代码块中做非常意想不到的事情。请改用命令(如“remark”中所示)。REM

答: 暂无答案