I wanted to know if possible how to create a command line to do the following - if a directory exist ZIP only the contents into a ZIP file. If a directory has sub-directories ZIP only the contents into another ZIP file.


C:\Directory\sample.txt ZIP only sample.txt
C:\Directory\Directory1\sample1.txt ZIP only sample1.txt
C:\Directory\Directory1\Directory2\sample2.txt ZIP only sample2.txt

So it would have created 3 zip files in C:\Directory and sub-directories.

I will not know the name of the sub-directories so can I also assign some sort of variable that says if there are directories or sub-directories in C:\Directory then start above ZIP(s)?

Thank you,


  • I can create a bat file to create the zips if I know the name of the directories or sub-directories but don't know where to begin if I DO NOT know the name of the directories or sub-directories. I will like it to scan and if it finds directories create a zip file with only the contents. Help?? – PaulJavier Jul 23 '13 at 19:34
  • The short answer is YES it is possible, you will just have to program the .cmd/.bat file to do this yourself. – mdpc Jul 23 '13 at 19:51
  • Do you have an example to program the cmd/bat file to do this? – PaulJavier Jul 23 '13 at 19:56
  • I recommend trying out "for" command yourself rather than having someone write the entire batch file for you...We can help if you are stuck at some points...First, can you loop over all the files in a directory and its subdirs? – tumchaaditya Oct 31 '13 at 23:33

Put this in batch file:

REM Usage: ZipFilesRecursively.bat "C:\My Files"
for /R "%~f1" %%F in (*) do 7z a "%%~dpFfiles.zip" "%%F"

It compresses all and only all files to files.zip archive created in C:\My Files directory and all its subdirectories.

  • 1
    Good job. You may want to mention, though, that it requires you to have a copy of 7zip, and have the 7z.exe in your path. Also, you can easily just drag and drop a folder on the batch file, rather than ever having to go to the command line. – trlkly Apr 16 '14 at 8:29

If you don't know the list of directories, you can loop through them with the FOR command.

In a Windows command prompt, type FOR /? to get info on its syntax and use.

What I would probably do is use that to copy all files you care about into a new subdirectory and zip that. But using the help file, you should be able to figure out what you need to do.

  • I need the files to stay in their directories as well as the ZIP files. – PaulJavier Jul 23 '13 at 20:09
  • @PaulJavier You should be able to do it that way too. It just might require some shell script cleverness, depending on what you're using for your zip program. Just experiment! The FOR command will get you where you need to go, though. – Ben Richards Jul 23 '13 at 20:11
  • The knowledge I have is what I found on the net when researching bat file creation for zip files. Which has worked great because I set the paths. My issue is too many directories and sub-directories are being created (2,505). I rather have it scan for directories then Zip its contents. I will be using 7zip. I will check out the FOR/? command, never heard of it. Thanks. – PaulJavier Jul 23 '13 at 20:20

This may help you out:

Dir /A:D /B - displays all the sub-directories in a directory

Dir /A:-D /B - displays only the files contained inside a directory

The result of Dir /A:D /B can be used to check if a directory contains sub-directory or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.