Search code examples
batch-filebatch-rename

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


i have tried to learn and make batch that will extract one specific file from all zip files from directory and give them name in following format: filename_%myvariable%.xml

but i am struggling with giving the proper name to extracted file. here is my code i have for now and i have made it working [updated code below]

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 file structure is like this:

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

as final result i want to have output with file.xml in following format: 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

can someone help? > i think i have my version of solution any suggestions are more than welcome


Solution

  • Given your .zip file naming format, Name_ArchiveYYYYMMDDHHMMSS.zip and expected output file format, Name_YYYYMMDD_file.xml, I'd suggest:

    @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