简体   繁体   中英

Batch file to Extract a specific file from Zip folder and rename to include Zip name

I have Approx 300 zip files that will all have a file called SP_OUT.db contained withing one of the folders in the zip. These folders themselves may or may not be be zipped. I want to unzipped the file SP_OUT.db and place in a new folder. However as all the files are the same name i want to rename them to include the name of the ZIP in which it came from.

For example

A11_21156_AHDW1_1.zip extract SP_OUT.db and rename it to A11_21156_AHDW1_1SP_OUT.db A06_21047_APERCLASH1_1.zip extract SP_OUT.db and rename it to A06_21047_APERCLASH1_1.db

Any help with this would be great.

Thanks

What OS are you using? It looks like there should be a way to do this if you don't mind installing the program 7zip. You could then incorporate a 7zip command like the one below to only extract the SP_OUT.db files.

7z e archive.zip -oc:\soft SP_OUT.db -r

extracts all SP_OUT.db files from archive archive.zip to c:\\soft folder

May need to try using the x argument instead of e

Source: http://sevenzip.sourceforge.jp/chm/cmdline/commands/extract.htm

.

EDIT: Worked out a script to extract each file. However I havent worked out yet how to add the original zip name to the start of the extracted SP_OUT.db filename.

I've marked the place in the script below where you woudl need to do this, and added 'REN' (Rename) after it to change the name of the extracted file.

@ECHO off
TITLE All your SP_OUT.db are belong to us
SETLOCAL ENABLEDELAYEDEXPANSION

REM Set your working directories below.
set targetFile=SP_OUT.db
set sourceDir=%CD%\source
set outputDir=%CD%\output
set 7ziplocation=C:\Progra~1\7zip\7z.exe

:start
FOR /f "delims=" %%a IN ('dir/s/b/a-d "%sourcedir%\*.zip"') DO (
 %7ziplocation% e %%a -o%outputDir% SP_OUT.db -r
 CALL :process2 %%a
  )
GOTO :eof

:process2
SET "fdir=%1"
REM ********* %fdir% will be the full path including filename of original zip file.
REM ********* Work out how to get just zip file name and put save to variable here.
REN %outputDir%\SP_OUT.db %put_the_above_zip_name_here%_SP_OUT.db
GOTO :eof

just read the man page:

7-Zip [64] 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18

Usage: 7z  [...]  [...]
       []


  a: Add files to archive
  b: Benchmark
  d: Delete files from archive
  e: Extract files from archive (without using directory names)
  l: List contents of archive
  t: Test integrity of archive
  u: Update files to archive
  x: eXtract files with full paths

  -ai[r[-|0]]{@listfile|!wildcard}: Include archives
  -ax[r[-|0]]{@listfile|!wildcard}: eXclude archives
  -bd: Disable percentage indicator
  -i[r[-|0]]{@listfile|!wildcard}: Include filenames
  -m{Parameters}: set compression Method
  -o{Directory}: set Output directory
  -p{Password}: set Password
  -r[-|0]: Recurse subdirectories
  -scs{UTF-8 | WIN | DOS}: set charset for list files
  -sfx[{name}]: Create SFX archive
  -si[{name}]: read data from stdin
  -slt: show technical information for l (List) command
  -so: write data to stdout
  -ssc[-]: set sensitive case mode
  -ssw: compress shared files
  -t{Type}: Set type of archive
  -u[-][p#][q#][r#][x#][y#][z#][!newArchiveName]: Update options
  -v{Size}[b|k|m|g]: Create volumes
  -w[{path}]: assign Work directory. Empty path means a temporary directory
  -x[r[-|0]]]{@listfile|!wildcard}: eXclude filenames
  -y: assume Yes on all queries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM