0

I want to find all the *.pdf files(including the pdf with the same name)for all 3 of my partitions C,D and F and put them into a .rar or .zip. When I started I wanted to just copy the pdf into a file but I realised it wasn't enough for what I wanted.

Can anyone give me any pointers? I wrote this:

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\
5
  • 1
    What have you researched or tried yourself? Are these PDF files in a specific directory on each drive? Or are they throughout the drive?
    – CharlieRB
    Apr 2, 2013 at 18:28
  • Does it have to be done via the command line or a batch file? Do you really need to copy/move all the PDFs to a single dir (per drive)? Are you looking to create separate archives for each drive or one single massive archive?
    – Karan
    Apr 2, 2013 at 18:52
  • To charlierb the code i have written its my own they work pretty ok but they have a problem with pdf with the same name in which case i want to put them as well.i have tried to find how i can rar them but till now no luck i guess it will be better to start over again from zero.as for the pdfs they are all around my pc and i want them all i have 3 drives C, D and F
    – Psema
    Apr 2, 2013 at 19:01
  • to karan i prefer to do it via batch file and i look to make a huge archive with all of the pdfs. as for the seperate copy i dont know if its really needed i am rather new to the idea of cmd and make batch files(this is my first one to be more exact)
    – Psema
    Apr 2, 2013 at 19:03
  • If any of the answers have resolved your question, you could indicate you have accepted that answer by clicking the checkmark on the left side of the answer (when clicked, the checkmark will change from white to green). If some answers are "almost" working for you, you could leave a comment asking for more help. Apr 12, 2013 at 23:23

2 Answers 2

0

Download and install the archiving and compression utility 7-zip.

Then run something like this for every source directory:

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

7z.exe is the commandline version of 7-zip. Run 7z.exe -? to get to know all options and switches.

1
  • Thank you mate i ll try it.i ahve instelled it already and i am reading how to run it in cmd :)
    – Psema
    Apr 2, 2013 at 21:05
0

Here is a batch script to do what you need.

You didn't mention what you want to happen in case of duplicate path\filename.pdf (from different drives).

So, what I did was to add a unique number to the end of the filename if a duplicate is found.

As an example of this, if you have these files:

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

and your target path is:

"D:\Test"

then the script will copy:

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

Initially, the script will only display the files to be copied. Once you are confident the correct files will be copied to the correct path, make this change to the script...

Find this line (near the top):

set zzlive=0

and change it to:

set zzlive=1

Also, you will find some lines like:

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

Add as many of these "Folders" as you need... just number them sequentially.


When the script runs, these lines will "expand" to:

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

Finally, look for the line that looks like:

rem Create .zip

Below that line, add your command to create your ".zip" file however you want to do it, depending on where you want to put the ".zip" file and other ".zip" file options you require. If you need help with this, just let me know some details and I will help you.


If you need more information, or if you need the script to work differently, just let me know.

Here is the script:


@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\

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .