用7z命令压缩多个文件夹和文件

压缩目标:
文件夹:Folder1
文件:Folder2\Folder2_File2.txt
文件夹:Folder3

参考之前的文章:
《用7z命令压缩文件夹》
https://blog.csdn.net/cpdoor2163_com/article/details/109580529

修改出新的压缩命令,CompressCommand2.bat,的内容:
:: For compress, set source folder, target folder and target file name
set sourceFolderPath1=D:\cpWorkZone\Practice\CompressCommand\SourceFolder
set targetFolderPath1=D:\cpWorkZone\Practice\CompressCommand\BackupFolder
set targetFileName1=SourceFolder

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set currentTimeString1=%date:~0,4%-%date:~5,2%-%date:~8,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%
set targetFilePath1=%targetFolderPath1%\%targetFileName1%_%currentTimeString1%.7z
:: echo %targetFilePath1%

7z.exe.lnk a -t7z %targetFilePath1% %sourceFolderPath1%\Folder1 %sourceFolderPath1%\Folder2\Folder2_File2.txt %sourceFolderPath1%\Folder3

pause

压缩完成,生成SourceFolder_2020-11-10_11_15_47.7z。
解压出来,发现压缩的文件夹和文件都“顶”到当前根目录上了:
D:\cpWorkZone\Practice\CompressCommand\BackupFolder\SourceFolder_2020-11-10_11_15_47>tree /F
卷 D_HDD_931 的文件夹 PATH 列表
卷序列号为 5264-1BBA
D:.
│  Folder2_File2.txt

├─Folder1
│      Folder1_File1.txt

└─Folder3
        Folder3_File1.txt
        Folder3_File2.txt
        Folder3_File3.txt

虽然也能用,但是缺少了源目录的目录结构,当然确实可以使用“tree /F > FolderTree.txt”把目录结构保存到FolderTree.txt文本文件里保存起来,不过,操作有点繁琐。

用7z命令压缩多个文件夹和文件,并保留完整源目录路径到压缩包的方法:
1.
添加SourceFileList.txt,这里记录了需要压缩的源目录文件夹和文件的目录路径,内容:
D:\cpWorkZone\Practice\CompressCommand\SourceFolder\Folder1
D:\cpWorkZone\Practice\CompressCommand\SourceFolder\Folder2\Folder2_File2.txt
D:\cpWorkZone\Practice\CompressCommand\SourceFolder\Folder3

2.
使用7z的“-spf”开关,和“@listfile”开关,创建CompressCommand5.bat,的内容:
:: For compress, set source file list, target folder and target file name
set sourceFileList=D:\cpWorkZone\Practice\CompressCommand\CompressCommandFolder\SourceFileList.txt
set targetFolderPath=D:\cpWorkZone\Practice\CompressCommand\BackupFolder
set targetFileName=TargetFile

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set currentDateYear=%date:~0,4%
set currentDateMonth=%date:~5,2%
set currentDateDay=%date:~8,2%

set timeTemp=%time: =0%
set currentTimeHour=%timeTemp:~0,2%
set currentTimeMinute=%time:~3,2%
set currentTimeSecond=%time:~6,2%

set currentTimeString=%currentDateYear%-%currentDateMonth%-%currentDateDay%_%currentTimeHour%_%currentTimeMinute%_%currentTimeSecond%
:: echo %currentTimeString%

set targetFilePath=%targetFolderPath%\%targetFileName%_%currentTimeString%.7z
:: echo %targetFilePath%

7z.exe.lnk a -t7z %targetFilePath% -spf @%sourceFileList%

pause

3.
运行压缩命令,创建出来压缩包SourceFolder_2020-11-10_11_44_55.7z,解压可以看到:
D:\cpWorkZone\Practice\CompressCommand\BackupFolder\SourceFolder_2020-11-10_11_44_55>tree /F
卷 D_HDD_931 的文件夹 PATH 列表
卷序列号为 5264-1BBA
D:.
└─D_
    └─cpWorkZone
        └─Practice
            └─CompressCommand
                └─SourceFolder
                    ├─Folder1
                    │      Folder1_File1.txt
                    │
                    ├─Folder2
                    │      Folder2_File2.txt
                    │
                    └─Folder3
                            Folder3_File1.txt
                            Folder3_File2.txt
                            Folder3_File3.txt

这个压缩包里保留了源目录的完整路径。

参考:
a.
《Command Line Syntax》的List file
https://sevenzip.osdn.jp/chm/cmdline/syntax.htm

b.
7z命令帮助:
7-Zip 18.06 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-12-30

Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...]

<Commands>
  a : Add files to archive
  b : Benchmark
  d : Delete files from archive
  e : Extract files from archive (without using directory names)
  h : Calculate hash values for files
  i : Show information about supported formats
  l : List contents of archive
  rn : Rename files in archive
  t : Test integrity of archive
  u : Update files to archive
  x : eXtract files with full paths

<Switches>
  -- : Stop switches parsing
  @listfile : set path to listfile that contains file names
  -ai[r[-|0]]{@listfile|!wildcard} : Include archives
  -ax[r[-|0]]{@listfile|!wildcard} : eXclude archives
  -ao{a|s|t|u} : set Overwrite mode
  -an : disable archive_name field
  -bb[0-3] : set output log level
  -bd : disable progress indicator
  -bs{o|e|p}{0|1|2} : set output stream for output/error/progress line
  -bt : show execution time statistics
  -i[r[-|0]]{@listfile|!wildcard} : Include filenames
  -m{Parameters} : set compression Method
    -mmt[N] : set number of CPU threads
    -mx[N] : set compression level: -mx1 (fastest) ... -mx9 (ultra)
  -o{Directory} : set Output directory
  -p{Password} : set Password
  -r[-|0] : Recurse subdirectories
  -sa{a|e|s} : set Archive name mode
  -scc{UTF-8|WIN|DOS} : set charset for for console input/output
  -scs{UTF-8|UTF-16LE|UTF-16BE|WIN|DOS|{id}} : set charset for list files
  -scrc[CRC32|CRC64|SHA1|SHA256|*] : set hash function for x, e, h commands
  -sdel : delete files after compression
  -seml[.] : send archive by email
  -sfx[{name}] : Create SFX archive
  -si[{name}] : read data from stdin
  -slp : set Large Pages mode
  -slt : show technical information for l (List) command
  -snh : store hard links as links
  -snl : store symbolic links as links
  -sni : store NT security information
  -sns[-] : store NTFS alternate streams
  -so : write data to stdout
  -spd : disable wildcard matching for file names
  -spe : eliminate duplication of root folder for extract command
  -spf : use fully qualified file paths
  -ssc[-] : set sensitive case mode
  -sse : stop archive creating, if it can't open some input file
  -ssw : compress shared files
  -stl : set archive timestamp from the most recently modified file
  -stm{HexMask} : set CPU thread affinity mask (hexadecimal number)
  -stx{Type} : exclude archive type
  -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

c.
《Need leading zero for batch script using %time% variable》
https://serverfault.com/questions/147515/need-leading-zero-for-batch-script-using-time-variable

d.
《String processing in windows batch files: How to pad value with leading zeros?》
https://stackoverflow.com/questions/13398545/string-processing-in-windows-batch-files-how-to-pad-value-with-leading-zeros

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值