批处理文件脚本到zip文件中的文件夹

时间:2016-02-03 19:30:57

标签: batch-file

我在这个模式中有一个文件夹结构。我刚刚显示了两个子目录和每个文件中的2个文件,但通常我在单个级别上有n个子目录,并且在它们下面有单个级别的文件(但可以是n个文件或文件夹)。

Directory master
folder 1:
  file1
  file2
folder 2:
  file 3
  file 4
  folder x

我需要创建一个Windows脚本,一个从主目录运行的批处理文件,并给我两个包含各自文件的zip文件x.zip和y.zip。

1.zip (only contains file1 and file2 -without folder 1 )
2.zip (only contains file3 and file4 and folder x -without folder 2- )

zip文件不包含文件夹1或文件夹2

此代码执行此操作,但压缩到其子文件夹为  1.zip(            文件夹1                文件1                文件2        )  2.zip(        文件夹2(                 文件3                 文件4                 文件夹x              )        )

for /d %%a in (*) do (ECHO zip -r -p "%%~na.zip" ".\%%a\*")

1 个答案:

答案 0 :(得分:-1)

我相信你可以通过下面的代码使用7-zip到zip文件来实现你想要的东西:

@echo off
setlocal enabledelayedexpansion
set YOUR_DIR=%1
set OWNING_DIR=%~dp0
set zipext=.zip

for /f "tokens=*" %%L in ('dir /b "%YOUR_DIR%"' ) do (
   set FOLDER_NAME=%%L
     set zipname=%OWNING_DIR%\!FOLDER_NAME!%zipext%
     set ERRORLEVEL=0
     type NUL
     set DIR_TO_CHECK=%YOUR_DIR%\!FOLDER_NAME!
     IF EXIST "!DIR_TO_CHECK!"\* (
       pushd %YOUR_DIR%\!FOLDER_NAME! >NUL 2>&1
       if %ERRORLEVEL% equ 0 (
         for /f "tokens=*" %%a in ( 'dir /b') do (
            "C:\Program Files\7-Zip\7z.exe" a  "!zipname!" "%%a"             
          )
       )
       popd
      ) 
   )

将其复制到名为zip_files.bat的批处理文件中,并使用

进行调用
zip_files "Directory_master"
相关问题