1

I have part of this script that I use to individually encrypt/zip files recursively in a folder structure.

It works, except it seems one particular folder it will go back and start zipping the already zipped files. I can't quite figure out what could be causing it.

Filenames in the folder are all consistent too (they're images). Like "2017-08-16 18.16.51.jpg". Once each file is zipped it names the file file1.7z file2.7z file3.7z until it goes through all the files in all the subfolders.

Here is the code:

SETLOCAL DISABLEDELAYEDEXPANSION
...
...
<MORE SCRIPT HERE>
...
...

set count=0
for /r "%sourcedir%" %%a in (*) do (
CLS
set /A count+=1
SETLOCAL EnableDelayedExpansion
set fnz=file!count!.7z

ECHO ***
ECHO *** ENCRYPTING "%%a" as "!fnz!"
ECHO ***
ECHO *** FILE NUMBER !count! OUT OF !files! 
ECHO ***
ECHO %%a !fnz! >> "!batchdir!7ZIP_FILEREF_!folderzip!.txt"

!batchdir!7z.exe a -mx9 -sdel -p"!password!" -mhe=on "%%~dpa!fnz!" "%%a" >nul

ENDLOCAL
)

...
...
<MORE SCRIPT HERE>
...
...
ENDLOCAL

Variables batchdir and folderzip are set earlier on in the script.

Here is some example output from the log when it flips from the last file, to start zipping already zipped file file100.7z. File/folder on left is the file being zipped. The file file###.7z on right is the zipped file name:

E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\2017-08-13 13.24.18.jpg file961.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\2017-08-13 13.25.02.mp4 file962.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\2017-08-14 18.03.02.mp4 file963.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\2017-08-15 12.33.20.jpg file964.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\2017-08-16 18.16.51.jpg file965.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\2017-08-16 18.16.53.jpg file966.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\2017-08-16 18.18.26.jpg file967.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\2017-08-16 18.18.27.jpg file968.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\2017-08-16 18.18.29.jpg file969.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\2017-08-17 13.10.56.jpg file970.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\2017-08-18 06.59.19.jpg file971.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\BMPhoneWallpaper.png file972.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\file100.7z file973.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\file101.7z file974.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\file102.7z file975.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\file103.7z file976.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\file104.7z file977.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\file105.7z file978.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\file106.7z file979.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\file107.7z file980.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\file108.7z file981.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\file109.7z file982.7z 
E:\Temp\MDISC\Personal Test C17\Pictures\Camera 2017\file110.7z file983.7z 

Like I said, so far it's only this ONE subfolder Camera 2017. I have other folders like Camera 2014 Camera 2015 Camera 2016 etc like that that zip no problem at all.

2
  • Try restricting your search to only images. May 21 '20 at 9:02
  • But I need it to encrypt EVERY file. In EVERY folder. Not just images. I have more than just images. I have other folders with documents and every other kind of file. That's just the strange thing is that it chokes on this particular folder. It's almost like it looks at file100.7z as a file that existed before in that folder, when in fact, 7z generated that file during the for loop.
    – HTWingNut
    May 21 '20 at 15:26
2

The for /r loop pharses the files in alphabetical order and it continues pharsing while it is runnig and since you are creating files

Areon.jpg  ->file22.7z

Banana.jpg ->file23.7z

Canon.jpg  ->file24.7z

Dallas.jpg ->file25.7z

Elephant   ->file26.7z

file01.7z  ->file27.7z

When it reaches "f" it starts including the files that you created with 7zip in the list.

I guess if the source destination files would have the same you wouldn't have that problem:

Areon.jpg  ->Areaon.7z
Banana.jpg ->Banana.7z
Canon.jpg  ->Canon.7z

Anyway for /r is not the best option if you are working on operations that create files.

Better use a For /f in combination with a dir command:

for /f "Delims=" %%a in ('dir /a-d /b /s /a "%sourcedir%\*"') do (

insted of:

for /r "%sourcedir%" %%a in (*) do (
5
  • Thank you! I will give it a shot. But why does it only do it sometimes? I just tried with several different data sets and sometimes it goes through without a problem and other times it does not. It seems it may be a limit on number of files in a folder. Maybe it keeps so many in memory and after that it has to re-reference the list? I dunno. Thanks, I'll give your suggestion a try.
    – HTWingNut
    May 22 '20 at 0:12
  • 1
    If the files created are set before the letter for /r is currently pharsing then there is no problem. For example for /r is pharsing d... and you create files that start with a, b, c then there is no problem because for /r already went through this letters but if you create files that are still going to be pharsed by for /r like e,f,g,h...then there might be a problem. For /f takes 1 shot on the result of the dir command and doesn't repharse anything while it is running it just goes througt the files that the dir command gave him. May 22 '20 at 0:19
  • Thank you! It seems to work like a charm! I had to remove the /a switch for it to work properly. Thanks again. You saved me losing more hair! :)
    – HTWingNut
    May 22 '20 at 2:05
  • 2
    No problemo. The /a switch also includes hidden and system files but if you don't wnat them you can remove it. May 22 '20 at 4:39
  • Yeah, regular attributes are fine. Hidden, read-only, or system files aren't necessary for what's needed to be encrypted. Thanks so much.
    – HTWingNut
    May 22 '20 at 5:20

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.