I have a huge folder that I would like to transform into several small ones of 20 files each and compressed. I would like to do this automatically with a batch file and 7zip.
To achieve this I thought about 2 steps :
1. Create a sub-folder for each 20 files ( source ) :
@echo off
set /a counter=1
set /a filesperfolder=20
cd "C:\Users\Desktop\dir\"
:loopstart
set dirname=dir_%counter%
md %dirname%
echo %dirname%
dir /b | findstr /v /i "dir_*"> %temp%\temp.txt && for /l %%l in (1,1,%filesperfolder%) do @for /f "tokens=1,2* delims=:" %%a in ('findstr /n /r "^" %temp%\temp.txt ^| findstr /r "^%%l:"') do @move %%b %dirname%\%%b >nul
set /a counter=%counter%+1
for /f "tokens=*" %%a in ('type %temp%\temp.txt ^| find /c /v ""') do set _filesmoved=%%a
del %temp%\temp.txt
IF %_filesmoved% LSS 20 goto done
goto loopstart
:done
cls
echo All files were moved!!
pause
exit
Unfortunately this does not work : The syntax of the command is incorrect.
. I've tried debugging the script by removing the @echo off
and it tells me that the dir /b | findstr /v /i "dir_*"> %temp%\\temp.txt && for /l %%l in (1,1,%filesperfolder%) do @for /f "tokens=1,2* delims=:" %%a in ('findstr /n /r "^" %temp%\\temp.txt ^| findstr /r "^%%l:"') do @move %%b %dirname%\\%%b >nul
dir /b | findstr /v /i "dir_*"> %temp%\\temp.txt && for /l %%l in (1,1,%filesperfolder%) do @for /f "tokens=1,2* delims=:" %%a in ('findstr /n /r "^" %temp%\\temp.txt ^| findstr /r "^%%l:"') do @move %%b %dirname%\\%%b >nul
part is not working ( same error ).This part is quite fuzzy for me and a little help would be welcome.I specify that the file temp.txt contains my complete list of files, without any separator (one file per line).
--- UPDATE: filenames must not contain spaces ---
2. Compress all these subfolders one by one with ( source ) :
for /D %d in (*.*) do 7z a -tzip "%d.zip" ".\\%d\\*"
Do you have an idea for (I summarize) : create, for a large number of files, subfolders of 20 files in order to compress each subfolder one by one.
If you have any idea on how to compress each 20 files directly (without going through the creation of subfolders) I also agree!
Thank you in advance for your help!
Resolved!
I found my mistake. In reality my files included spaces that the script could not fit.
So, to answer my basic question which was: in a big folder filled with thousands of files, how to compress each 20 files in subfolders? it is necessary (to my knowledge) to:
And it's done!
Do not hesitate to suggest a solution if you know of a "faster" one.
No need to create folders. Just iterate over the files and add every single file to it's designated zip file (instead of copying every 20 files into a folder and zip that folder) Use two counters to keep track of (source)files and zip files. One counter for the files (use the Modulo-operator to check for each 20) and one for the zip filenames. I started the zip filenames with 10001
instead of 1
and took the last four chars of it to get sortable file names ( 0001.zip
etc).
@echo off
setlocal enabledelayedexpansion
set filesperfolder=20
cd /d "%cd%"
set "zipDest=C:\temp"
set filecounter=0
set foldercounter=10001
for %%a in (*) do (
set /a filecounter+=1
set /a check=filecounter %% filesperfolder
if !check! == 0 set /a foldercounter+=1
ECHO 7z a -tzip "%zipDest%\!foldercounter:~-4!.zip" "%%a"
)
Obviously adapt the cd /d
line (source folder), destination folder (Attention: don't use the source folder, or your zip files might be zipped again into other zip files), and maybe the zip-filename (like "%zipDest%\\MyZips-!foldercounter:~-4!.zip"
)
NOTE: I disarmed the 7zip command with ECHO
for testing and troubleshooting. Remove the ECHO
when the output satisfies you.
To delete each file after successfully zipping them , change the zip line to:
7z a -tzip "%zipDest%\!foldercounter:~-4!.zip" "%%a" && del "%%a"
The quotes should take care of any spaces.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.