从当前文件夹的所有zip文件中提取所有特定文件,然后仅将文件zip后退到单独的文件夹批处理

时间:2019-11-12 19:18:43

标签: batch-file batch-rename

我尝试学习并进行批量处理,该处理将从目录中的所有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 >>>

  

我做出的输出与我的请求类似,但仍具有zipfile的日期和时间,并且文件名具有我期望的近似文件格式

有人可以帮忙吗? >我认为我有解决方案的版本 任何建议都值得欢迎

1 个答案:

答案 0 :(得分:0)

鉴于您的.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