Scott Mitchell
Scott Mitchell

Reputation: 8759

ZIP each file in a directory that doesn't already have a ZIPped version in another folder

I am using 7-zip and Windows 2008 Server and need to ZIP each file in folder C:\Original that doesn't already have a corresponding ZIP file in C:\Archived. I would prefer doing this with a .bat file.

The C:\Original folder contains a series of .PDF files that are never changes, like so:

I want to run a batch file that will create a ZIP file for each PDF in the C:\Archive folder, but only if the file doesn't already exist. For instance, if the C:\Archive folder already has two files in it:

Running the batch file would ZIP just the two unzipped files, HouseLayout.pdf and SurveyResults.pdf.

I have a batch file that uses the FOR command that will ZIP all of the files in C:\Original, but I only want to ZIP those that don't already exist in C:\Archive.

Thanks

Upvotes: 1

Views: 851

Answers (2)

MC ND
MC ND

Reputation: 70933

for %%f in ("c:\original\*.pdf") do if not exist "c:\archive\%%~nf.zip" (
    7za a "c:\archive\%%~nf.zip" "%%~ff"
)

For each file in the indicated folder/set if not exist a file with the same name but with zip extension, create the zip file with the original file inside it.

Upvotes: 1

Adil Hindistan
Adil Hindistan

Reputation: 6605

I think you need something like this:

for /F %%I in ('dir /b c:\original\*.pdf') do set filename=%%I
set archivedFile=%filename:~,-4%

if not exist C:\archive\%archivedFile%.zip (
  REM zip it up
)

Upvotes: 0

Related Questions