Batch file to ZIP only files in directory or sub directory
8,247
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.
Related videos on Youtube
Author by
PaulJavier
Updated on September 18, 2022Comments
-
PaulJavier over 1 year
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.
Example:
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,
Paul
-
PaulJavier almost 11 yearsI 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??
-
tumchaaditya over 10 yearsI 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?
-
-
user3359503 almost 11 years@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. -
PaulJavier almost 11 yearsThe 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.
-
trlkly about 10 yearsGood 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.