从当前文件夹的所有 zip 文件中提取所有特定文件,并将仅文件压缩回单独的文件夹批处理

Extract all specific file from all zip files from current folder and zip back only file into separate folder batch

我已经尝试学习并制作批处理,它将从目录中的所有 zip 文件中提取一个特定文件,并按照以下格式为它们命名:filename_%myvariable%.xml

但我正在努力为提取的文件指定正确的名称。这是我现在的代码,我已经让它工作了 [下面的更新代码]

cls
REM script was made in order to extract file.xml from archive file and to make it as new zip file containing only file.xml
REM script also makes file.xml renamed by archive folder name in front of the file.xml
REM *******
REM new version will not compress files separate it will make one archive with files in different names
REM version 1.1

@echo off

SETLOCAL ENABLEEXTENSIONS
setlocal enabledelayedexpansion

Rem setting current folder and srcipt name in variable
SET me=%~n0
SET parrent=%~dp0

SET

rem loop to do every zip file in current folder
for /R "%parrent%" %%I in (*.zip) do (

REM Unzip FILE.XML into each separate folder
    REM 7za x "%%I" -o"%%~dpI%%~nI" "file.xml" -r -y -p2468

REM Extract file.XML into current folder
    7za e "%%I" -o"%%~dpI" "file.xml" -r -y -p2468

REM Rename extracted file.XML file into zipfilename_file.xml
    REM rename %parrent%%%~nI\subfolder\file.xml %%~nI%_file.xml

REM Rename file.XML in current folder into zipfilename_file.xml
    rename ej.xml %%~nI%_file.xml

REM Zip folder with renamed file.XML to the Desktop\temp folder of the current userprofile%\Desktop\temp\ without password
    REM 7za a -tzip "%userprofile%\Desktop\temp\%%~nI.zip" "%%~dpI%%~nI\subfolder\*.xml" -r -y

REM Zip folder with renamed file.XML to the Desktop\temp folder of the current userprofile%\Desktop\temp\ WITH password
    REM 7za a -tzip "%userprofile%\Desktop\temp\%%~nI.zip" "*.xml" -r -y -p2468

rem Copy *_file.XML files to the %userprofile%\Desktop\temp\ for export
    md "%userprofile%\Desktop\temp"
    copy /y *.xml "%userprofile%\Desktop\temp\"

REM Delete all folders that are zipped to save space and free memory
    REM rd /s /q "%%~dpI%%~nI"

REM Delete all *file.XML files that are copied to save space and free memory
    del /s /q *.xml
    )

REMpause
EXIT

zip文件结构如下:

Name_ArchiveYYYYMMDDHHMMSS.zip
|-subfolder
    |-file.xml
|-otherfolder1
|-otherfolder2
etc...

作为最终结果,我希望使用 file.xml 以下列格式输出:Name_YYYYMMDD_file.xml >>>

i have made output to be similar to my request still having date and time of the zipfile and having filename in approximate file format i was expecting

有人可以帮忙吗? > 我想我有自己的解决方案 欢迎提出任何建议

鉴于您的 .zip 文件命名格式 Name_ArchiveYYYYMMDDHHMMSS.zip 和预期的输出文件格式 Name_YYYYMMDD_file.xml,我建议:

@Echo Off
SetLocal DisableDelayedExpansion
Rem Loop to do every zip file in current folder
For /R "%~dp0" %%I In ("*_*.zip") Do (
    Rem Unzip file.xml into current folder
    7za e "%%I" -o"%%~dpI" "file.xml" -r -y -p2468
    Rem Set variable for string expansion and substitution, [%%I format Name_ArchiveYYYYMMDDHHMMSS.zip]
    For /F "Tokens=1*Delims=_" %%# In ("%%~nI") Do (
        Set "DateString=%%$"
        Rem Enable delayed expansion
        SetLocal EnableDelayedExpansion
        Rem Rename file.xml in current folder to Name_YYYYMMDD_file.xml
        Ren "file.xml" "%%#_!DateString:~-14,8!_file.xml"

        Rem Any other commands using !DateString! here

        Rem End delayed expansion
        EndLocal

        Rem Any other commands using %%#, but not using DateString here

    )

    Rem Any other commands using %%I, but not using %%# or DateString here

)
Pause
EndLocal
Exit /B