Question

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:

  • MyResume.pdf
  • HouseLayout.pdf
  • SurveyResults.pdf
  • BlahBlahBlah.pdf

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:

  • MyResume.zip
  • BlahBlahBlah.zip

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

Was it helpful?

Solution

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.

OTHER TIPS

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
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top