如何将某种类型的所有文件复制到压缩文件中

如何将某种类型的所有文件复制到压缩文件中

我想找到我所有 3 个分区 C、D 和 F 的所有 *.pdf 文件(包括同名的 pdf),并将它们放入 .rar 或 .zip 中。开始时,我只想将 pdf 复制到一个文件中,但我意识到这还不足以满足我的要求。

有人能给我指点一下吗?我写了这个:

mkdir d:\test
mkdir f:\test1
chcp 65001
XCOPY C:\*.pdf D:test /S /H
XCOPY F:\*.pdf D:test /S /H    
XCOPY D:\*.pdf F:test1 /S /H
copy f:\test1 d:\test\kra\

答案1

下载并安装归档和压缩实用程序7-zip

然后对每个源目录运行类似的命令:

"C:\Program Files\7-Zip\7z.exe" a pdfArchive.7z -r d:\test\*.pdf

7z.exe是 7-zip 的命令行版本。运行7z.exe -?以了解所有选项和开关。

答案2

这是一个可以执行您需要的操作的批处理脚本。

您没有提到如果出现重复的 path\filename.pdf(来自不同的驱动器),您希望发生什么。

因此,如果发现重复,我所做的就是在文件名末尾添加一个唯一的数字。

举例来说,如果您有以下文件:

C:\Dir\A.pdf
C:\Dir\B.pdf
C:\Dir\B-1.pdf
C:\Dir\B-2.pdf
C:\Dir\C.pdf
C:\Dir\C-1.pdf
F:\Dir\A.pdf
F:\Dir\B.pdf
F:\Dir\C.pdf

你的目标路径是:

"D:\Test"

然后脚本将复制:

C:\Dir\A.pdf -> D:\Test\Dir\A.pdf
C:\Dir\B.pdf -> D:\Test\Dir\B.pdf
C:\Dir\B-1.pdf -> D:\Test\Dir\B-1.pdf
C:\Dir\B-2.pdf -> D:\Test\Dir\B-2.pdf
C:\Dir\C.pdf -> D:\Test\Dir\C.pdf
C:\Dir\C-1.pdf -> D:\Test\Dir\C-1.pdf
F:\Dir\A.pdf -> D:\Test\Dir\A-1.pdf
F:\Dir\B.pdf -> D:\Test\Dir\B-3.pdf
F:\Dir\C.pdf -> D:\Test\Dir\C-2.pdf

最初,脚本将仅显示要复制的文件。一旦您确信正确的文件将被复制到正确的路径,请对脚本进行以下更改...

找到此行(靠近顶部):

set zzlive=0

并将其更改为:

set zzlive=1

此外,你还会发现类似这样的几行:

set zztarget1="C:\*%zzftype%"
set zztarget2="F:\*%zzftype%"
set zztarget3="D:\*%zzftype%"

根据需要添加任意数量的“文件夹”...只需按顺序编号即可。


当脚本运行时,这些行将“扩展”为:

set zztarget1="C:\*.pdf"
set zztarget2="F:\*.pdf"
set zztarget3="D:\*.pdf"

最后,查找如下行:

rem Create .zip

在该行下面,添加命令以创建“.zip”文件,具体操作取决于您要将“.zip”文件放在哪里以及您需要的其他“.zip”文件选项。如果您需要帮助,只需告诉我一些详细信息,我就会帮助您。


如果您需要更多信息,或者需要脚本以不同的方式工作,请告诉我。

脚本如下:


@echo off


rem set "live" (or "test"). 0=test, 1=live
set zzlive=0


rem    set "Test" folder
set "zzhold=D:\test"


rem    set File type to process
set "zzftype=.pdf"


rem set targets
set zztarget1="C:\*%zzftype%"
set zztarget2="F:\*%zzftype%"
set zztarget3="G:\*%zzftype%"
rem set more targets, up to 10 ...


rem    set "Subst" drive. Use *ANY* unused drive letter
set "zzsubst=V:"

rem **********************************
rem probably do not need to edit lines below here
rem just find "rem Create .zip" line and add your zip command there
rem **********************************

rem    insure test folder exists
md "%zzhold%">nul 2>&1


rem    make Substituted drive so XCOPY won't complain about cyclical copy
if exist "%zzsubst%\*.*" goto :start
subst %zzsubst% "%zzhold%"
if exist "%zzsubst%\*.*" goto :start
echo Error "Subst-ing" drive %zzsubst%
goto :EOF





:start
rem:start

set zztarget=%zztarget1%
for %%e in (%zztarget%) do if "%%~e."=="." goto :done
call :work

set zztarget=%zztarget2%
for %%e in (%zztarget%) do if "%%~e."=="." goto :done
call :work

set zztarget=%zztarget3%
for %%e in (%zztarget%) do if "%%~e."=="." goto :done
call :work

set zztarget=%zztarget4%
for %%e in (%zztarget%) do if "%%~e."=="." goto :done
call :work

set zztarget=%zztarget5%
for %%e in (%zztarget%) do if "%%~e."=="." goto :done
call :work

set zztarget=%zztarget6%
for %%e in (%zztarget%) do if "%%~e."=="." goto :done
call :work

set zztarget=%zztarget7%
for %%e in (%zztarget%) do if "%%~e."=="." goto :done
call :work

set zztarget=%zztarget8%
for %%e in (%zztarget%) do if "%%~e."=="." goto :done
call :work

set zztarget=%zztarget9%
for %%e in (%zztarget%) do if "%%~e."=="." goto :done
call :work

set zztarget=%zztarget10%
for %%e in (%zztarget%) do if "%%~e."=="." goto :done
call :work


goto :done




:work
rem:work

@echo.
if %zzlive% EQU 0 echo Test mode - no files will be copied.
echo.|set /P "zzdummy=Gathering filenames, Please wait... "
set zzstart=0


rem get list of files to copy and pass each filename one to :work1

for /f "usebackq delims=>" %%f in (`xcopy /s /f /l /h %zztarget% "%zzsubst%\"`) do call :work1 "%%~f"
@echo.

goto :EOF





:done
rem:done

rem Create .zip
rem put your desired command to create your ".zip" file here.



rem go clear environment vars and exit
goto :cleanup





:work1
rem:work1

if not %zzstart% EQU 0 goto :work1b

@echo Complete - processing files.
@echo.
set zzstart=1
goto :work1b





:work1b
rem:work1b

set "zz1t=%~1"
set "zz1fullfn=%zz1t:~0,-2%"
set zz1t=%zz1fullfn:~-4%
rem @echo [%zz1fullfn%]  [%zz1t%]



rem skip lines not containing a valid .pdf filename
if "%zz1t%."=="." goto :EOF
if /I not "%zz1t%"==".pdf" goto :EOF



rem don't process files in "Test" folder
for /f "usebackq delims=" %%g in (`echo set "zz1t=%%zz1fullfn:%zzhold%\=%%"`) do %%~g
if "%zz1fullfn%."=="%zz1t%." goto :work2

rem We are trying to process a file in the "Test" folder... skip it.
rem     @echo.
rem     echo Skipping file: [%zz1fullfn%]

goto :EOF





:work2
rem:work2

rem echo Processing file: [%zz1fullfn%]
for %%g in ("%zz1fullfn%") do call :work3 "%%~dg" "%%~pg" "%%~ng" "%%~xg"
goto :EOF





:work3
rem:work3

set "zz1drive=%~1"
set "zz2path=%~2"
set "zz3file=%~3"
set "zz4ext=%~4"


set "zzsource=%zz1fullfn%"
set "zzdestpath=%zzhold%%zz2path%"
set "zzdestfullfn=%zzdestpath%%zz3file%%zz4ext%"
set "zzxcopydest=%zzdestpath%"

if exist "%zzdestfullfn%" goto :work3fixf

goto :work3copy





:work3copy
rem:work3copy

if %zzlive% EQU 0 goto :work3show

xcopy /h /i /f "%zzsource%" "%zzdestpath%"
@echo.
goto :EOF





:work3show
rem:work3show

xcopy /h /i /f /l "%zzsource%" "%zzdestpath%"|find /i "%zzsource%"
@echo.
goto :EOF





:work3fixf
rem:work3fixf

set zzindex=0





:work3loop
rem:work3loop

set /A zzindex=zzindex+1
set "zzdestfullfn=%zzdestpath%%zz3file%-%zzindex%%zz4ext%"
if not exist "%zzdestfullfn%" goto :work3rencopy

rem if you want to set a "Maximum" for number of dups to check, test "%zzindex%" here

goto :work3loop





:work3rencopy
rem:work3rencopy

if %zzlive% EQU 0 goto :work3renshow

md "%zzdestpath%">nul 2>&1
copy /y nul "%zzdestfullfn%">nul 2>&1
xcopy /h /i /f /y "%zzsource%" "%zzdestfullfn%"
@echo.

goto :EOF





:work3renshow
rem:work3renshow

echo xcopy /h /i /f /y %zzsource% -^> %zzdestfullfn% (ren)
@echo.

goto :EOF





:cleanup
rem:cleanup

subst %zzsubst% /d>nul 2>&1

set "zz1drive="
set "zz1fullfn="
set "zz1t="
set "zz2path="
set "zz3file="
set "zz4ext="
set "zzdestfullfn="
set "zzdestpath="
set "zzftype="
set "zzhold="
set "zzindex="
set "zzlive="
set "zzsource="
set "zzstart="
set "zzsubst="
set "zztarget="
set "zzxcopydest="

goto :EOF





rem     mkdir d:\test
rem     mkdir f:\test1
rem     chcp 65001
rem     XCOPY C:*.pdf D:test /S /H
rem     XCOPY F:*.pdf D:test /S /H
rem     XCOPY D:*.pdf F:test1 /S /H
rem     copy f:\test1 d:\test\kra\

相关内容