压缩目标:
文件夹: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

用7z命令压缩多个文件夹和文件相关推荐

  1. linux压缩解压缩文件夹或文件命令详解

    一.压缩当前目录下的文件夹或者文件app到app.tar tar -cvf app.tar app 二.解压缩当前目录下的压缩包app.tar到app tar -xvf app.tar 三.不解压的情 ...

  2. linux-----压缩解压缩文件夹、文件命令详解

    linux tar压缩解压缩文件夹.文件命令详解 常用命令: 1.压缩当前目录下文件夹/文件test到test.tar.gz: tar -zcvf test.tar.gz test 2.解压缩当前目录 ...

  3. centos彻底删除文件夹、文件命令

    centos彻底删除文件夹.文件命令(centos 新建.删除.移动.复制等命令: 1.新建文件夹 mkdir 文件名 新建一个名为test的文件夹在home下 view source1 mkdir ...

  4. 命令python所在的驱动器和文件夹_Python文件夹与文件的操作-阿里云开发者社区...

    最近在写的程序频繁地与文件操作打交道,这块比较弱,还好在百度上找到一篇不错的文章,这是原文传送门,我对原文稍做了些改动. 有关文件夹与文件的查找,删除等功能 在os模块中实现.使用时需先导入这个模块, ...

  5. Linux删除文件夹和文件的命令

    Linux 删除文件夹和文件的命令: -r 就是向下级文件夹递归,全部删除.-f 就是强制删除.删除文件夹:例如:删除/etc/nginx/log目录以及其下所有文件.文件夹rm -rf /etc/n ...

  6. File对文件夹、文件进行压缩

    对文件夹或者文件进行压缩工具类 import javax.servlet.http.HttpServletResponse; import java.io.BufferedInputStream; i ...

  7. 文件压缩(文件夹中有文件夹)

    有个朋友在我之前写的一个压缩文件里问:我这边是一个文件夹下面还有多个文件夹,像这样的怎么处理? 对于这样的问题我想到的是递归方式,为一级文件夹下面的每个文件进行判断是文件夹还是文件! 下面是详细的代码 ...

  8. 命令行删除文件夹及文件夹下文件

    如果文件夹下存储的文件很多(几个G或者几个T的数据),右键删除速度很慢,所以需要用到命令行删除,速度相对快很多.我整理了两个系统的删除方法,以此记录方便备查. Win10 cmd命令行删除文件夹及文件 ...

  9. dos命令删除文件夹和文件的方法

    在dos命令删除文件夹或文件,先要确定文件夹或文件是否有特殊的属性,比如"系统"."只读"."隐藏",如果有,去掉这些属性,命令如下 文件夹 ...

最新文章

  1. 大数据是一座孤单的小岛
  2. 全链路压测自动化实践
  3. Storm具体的解释(二)、成为第一Storm申请书
  4. 记录webpack使用问题,使用报错“UnhandledPromiseRejectionWarning,file-loader图片过大,无法加载图片,打包html文件报错TypeError
  5. docker -v 覆盖了容器中的文件_10分钟让你理解 docker 容器中的 uid 和 gid
  6. redhat mysql tar_mysql5.7版本tar包手动安装---redhat7.0
  7. python爬取网页上的特定链接_自学python爬虫二:如何正常操作urllib2通过指定的URL抓取网页内容...
  8. 二叉搜索树的删除操作可以交换吗_JavaScript数据结构 — 二叉搜索树(BST)ES6实现...
  9. java runnable 异常_Java实现多线程异常捕获Runnable的案例
  10. 移动端canvas_web前端开发分享-css,js移动篇
  11. linux闹钟命令,Linux基础命令一
  12. Cisco2811路由器的首次接触
  13. 使用ipad通过usb线连接电脑
  14. 路漫漫其修远兮,吾将上下而求索
  15. item_review - 获得淘宝商品评论电商API
  16. photoshop抠图场景二
  17. 大数据热词科普(三)
  18. andriod获取带字母的iccid
  19. 到西藏工作,出差,旅行,出游,体验,好奇一切的一切,大全科!!绝对是经典“教科书”类,上面见不到的!全面了解,西藏的生活水平,和注意事项,是去西藏,拉萨前的必读性文章。
  20. php获取无限极团队人数,PHP实现无限极菜单

热门文章

  1. Linux /中创建目录文件,复制,移动,该改名
  2. 航顺主流替代型HK32F103系列
  3. 【T2噬菌体】ASP.NET AJAX客户端编程之旅(一)——Hello!ASP.NET AJAX
  4. 如何使用cmd命令行进入自己想要进入的目录?
  5. 图像语义分割方法研究进展
  6. java基础之final关键字,形式参数和返回值问题
  7. 「计算机科学速成课」笔记
  8. C语言2048源代码
  9. ping程序的C语言源代码
  10. java强转规则_java类型转换及其规则介绍