I have +1TB worth of 1.5 GB files in a number of nested folders that I want to compress each one to a separate 7z archive, putting the resulting archives the in the same folders as the original files, all while these are removed to leave space (the drive is not big enough to store the archives and the originals at the same time).
I'm trying to use these settings
My intention was selecting them from a windows search result page and sending them to compress from there.
The WinRAR GUI has options for this, but 7-Zip doesn't seem to, but I don't want to compress to RAR.
How can I go about this? Manually compressing each file sounds like hell, and I have zero knowledge of BATCH scripting.
Last edit: Radorn Keldam 2018-06-27
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I guess that you need to put each individual file to a separate archive. True, unlike WinRAR, 7-Zip does not still have such a feature. If you are not familiar with Command Prompt, you can create a batch file: @echo off
cd /d upper_level_folder
for /r %%a in (*) do (
cd "%~pa"
7z a -mx9 -mmt4 -m0=lzma2:d128m -sdel "%~na.7z" "%~nxa"
)
Put the necessary path instead of upper_level_folder. If you put %1 here, you can drag the folder of interest onto the batch file. Refer to 7-Zip's help to tune its command-line switches.
I suggest you to test this script on a specially created folder first. Thus you can debug it without losing important data.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello, and thanks for replying.
I've tested the script, but I can't get it to work. I already put 7-Zip in the PATH and verified that I can run it from any directory, but the script still doesn't work. I get this:
The following usage of the path operator in batch-parameter
substitution is invalid: %~pa"
For valid formats type CALL /? or FOR /?
The syntax of the command is incorrect.
I'm on Windows 7 64bit if that makes any difference.
BTW, can you recommend me a good windows scripting manual?
Last edit: Radorn Keldam 2018-06-28
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes, I've made a mistake. The percent sign should be doubled: cd "%%~pa"
7z ... "%%~na.7z" "%%~nxa"
I believe you can start with Windows help (by typing help in the Command Prompt) and Microsoft Docs. Unfortunately, I don't know any modern books on this topic.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
OK it works now. Thanks again.
I've observed a potential problem with it, though.
Running it on my test folder, It seems it only deletes the original files when it finishes compressing the files in a given folder and jumps to the next, not after each individual file. Some of the folders are pretty big, and I'm pretty sure I'll run out of space with one of these.
Also, I've found a problem I didn't foresee before. There are some files that have the same name but different extension and they are being added to the same 7z archive, which is expected, but undesirable.
Could it be done so that it adds the original file extension as part of the filename before the .7z archive extension, and, also, only add and/or avoid certain extensions?
Sorry for giving you so much trouble and thank you for putting in the time so far.
If it's going to get too complex to add all that specificity I'll totally understand if you don't want to keep at it and I'll try to manage a way arround the problem.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
it only deletes the original files when it finishes compressing the files in a given folder
The -sdel switch forces 7-Zip to delete files after the current archive is created. The above script puts each file into a separate archive (as long as file names are different), doesn't it? Please, also check whether the file is in its archive's root folder (i.e. no path is stored). 7-Zip does not allow to exclude the path, so any mistake in setting current directory will backfire.
it adds the original file extension
Replace "%%~na.7z" with "%%~nxa.7z". Selective addition of file extension is more difficult, so please specify a clear algorithm to get a possible way to do it.
Sorry for giving you so much trouble
That's OK.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Well, some files are accompained by a .CHECK file of 0bytes that I add to keep track of those I have already dealt with. So, if there's, say, a "file2018-06-28.bin" that I have checked already, I will add a 0byte "file2018-06-28.CHECK" file beside it to "mark" it. I don't want these in the archives.
Not sure how I would specify an algorithm for that, though.
As for the deletion of files. It's working correctly now. I don't know what happened before.
Last edit: Radorn Keldam 2018-06-28
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Try replacing 7z ... with if /i %%~xa neq .check 7z .... This way, no files with .check extension (in either case) will be archived. There is also a possibility to check for zero size, but I don't think you need it right now.
Instead of creating a marker file, you may find it convenient to use the "Archive" file attribute. Windows sets it whenever a file is modified. Unfortunately, 7-Zip 18.05 cannot process files with only certain attributes, so you still need a script.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Well, I'm not doing anything to the files themselves to change their content, but more like revising them and I want a visual cue that I can easily notice when browsing through them (in details view). I also can't change the filenames.
There's no readily available method to manually change that file attribute, nor does it stand out visually when you browse, so .CHECK files it is for me.
I could even do something ridiculous like .check------------------------- which really stands out visually.
Anyway, thank you for all your help.
It's running along. and it will finish in about... 4 days xDD
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In case anyone finds it interesting, I did a small modification to Shell's script to make it not only skip my .CHECK files, but also skip .7z archives. This way I can interrupt the script and resume it later and it won't re-archive the archives that have already been made.
@echo off
cd /d .\files
for /r %%a in (*) do (
cd "%%~pa"
if /i %%~xa neq .7z if /i %%~xa neq .CHECK 7z a -mx9 -mmt4 -m0=lzma2:d128m -sdel "%%~nxa.7z" "%%~nxa"
)
pause
I also added a "PAUSE" command at the end so that the console window won't automatically close when the script finishes, and, instead, waits for me to close it. That way I know that everything went as expected and nothing else caused it to prematurely close.
Last edit: Radorn Keldam 2018-06-30
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Lets say I have a directory with 570 regular folders, within everyone of those folders there are 100++ Zipped archives.
I want a script that test the newest .zip inside every 570 folder automatically everyday and also logs the result in some way. But I am stuck.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In order to find the newest file, use dir /b /o-d *.zip in the for /f loop - its name will appear in the first line. If you log the result, you can add the check whether the log file exists in the loop, thus the files after the first one will be skipped.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Well, it is a bit off-topic here. I'll try to provide an example; however, I have not tested it thouroughly. Generally, it is better to write a script yourself - this helps you to correct it should anything fail. for /d %%a in (*) do (
del %%a\logfile.log
for /f %%b in ('dir /b /o-d %%a\*.zip') do (
pushd %%a
7z t -bse1 %%b >logfile.log
if errorlevel 1 echo Error with %%a\%%b
popd
)
)
👍
1
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have +1TB worth of 1.5 GB files in a number of nested folders that I want to compress each one to a separate 7z archive, putting the resulting archives the in the same folders as the original files, all while these are removed to leave space (the drive is not big enough to store the archives and the originals at the same time).![](https://s33.postimg.cc/94e8yoln3/7zip.png)
I'm trying to use these settings
My intention was selecting them from a windows search result page and sending them to compress from there.
The WinRAR GUI has options for this, but 7-Zip doesn't seem to, but I don't want to compress to RAR.
How can I go about this? Manually compressing each file sounds like hell, and I have zero knowledge of BATCH scripting.
Last edit: Radorn Keldam 2018-06-27
I guess that you need to put each individual file to a separate archive. True, unlike WinRAR, 7-Zip does not still have such a feature. If you are not familiar with Command Prompt, you can create a batch file:
@echo off
cd /d upper_level_folder
for /r %%a in (*) do (
cd "%~pa"
7z a -mx9 -mmt4 -m0=lzma2:d128m -sdel "%~na.7z" "%~nxa"
)
Put the necessary path instead of upper_level_folder. If you put
%1
here, you can drag the folder of interest onto the batch file. Refer to 7-Zip's help to tune its command-line switches.I suggest you to test this script on a specially created folder first. Thus you can debug it without losing important data.
Hello, and thanks for replying.
I've tested the script, but I can't get it to work. I already put 7-Zip in the PATH and verified that I can run it from any directory, but the script still doesn't work. I get this:
I'm on Windows 7 64bit if that makes any difference.
BTW, can you recommend me a good windows scripting manual?
Last edit: Radorn Keldam 2018-06-28
Yes, I've made a mistake. The percent sign should be doubled:
cd "%%~pa"
7z ... "%%~na.7z" "%%~nxa"
I believe you can start with Windows help (by typing
help
in the Command Prompt) and Microsoft Docs. Unfortunately, I don't know any modern books on this topic.OK it works now. Thanks again.
I've observed a potential problem with it, though.
Running it on my test folder, It seems it only deletes the original files when it finishes compressing the files in a given folder and jumps to the next, not after each individual file. Some of the folders are pretty big, and I'm pretty sure I'll run out of space with one of these.
Also, I've found a problem I didn't foresee before. There are some files that have the same name but different extension and they are being added to the same 7z archive, which is expected, but undesirable.
Could it be done so that it adds the original file extension as part of the filename before the .7z archive extension, and, also, only add and/or avoid certain extensions?
Sorry for giving you so much trouble and thank you for putting in the time so far.
If it's going to get too complex to add all that specificity I'll totally understand if you don't want to keep at it and I'll try to manage a way arround the problem.
The
-sdel
switch forces 7-Zip to delete files after the current archive is created. The above script puts each file into a separate archive (as long as file names are different), doesn't it? Please, also check whether the file is in its archive's root folder (i.e. no path is stored). 7-Zip does not allow to exclude the path, so any mistake in setting current directory will backfire.Replace
"%%~na.7z"
with"%%~nxa.7z"
. Selective addition of file extension is more difficult, so please specify a clear algorithm to get a possible way to do it.That's OK.
Well, some files are accompained by a .CHECK file of 0bytes that I add to keep track of those I have already dealt with. So, if there's, say, a "file2018-06-28.bin" that I have checked already, I will add a 0byte "file2018-06-28.CHECK" file beside it to "mark" it. I don't want these in the archives.
Not sure how I would specify an algorithm for that, though.
As for the deletion of files. It's working correctly now. I don't know what happened before.
Last edit: Radorn Keldam 2018-06-28
Try replacing
7z ...
withif /i %%~xa neq .check 7z ...
. This way, no files with .check extension (in either case) will be archived. There is also a possibility to check for zero size, but I don't think you need it right now.Instead of creating a marker file, you may find it convenient to use the "Archive" file attribute. Windows sets it whenever a file is modified. Unfortunately, 7-Zip 18.05 cannot process files with only certain attributes, so you still need a script.
Well, I'm not doing anything to the files themselves to change their content, but more like revising them and I want a visual cue that I can easily notice when browsing through them (in details view). I also can't change the filenames.
There's no readily available method to manually change that file attribute, nor does it stand out visually when you browse, so .CHECK files it is for me.
I could even do something ridiculous like .check------------------------- which really stands out visually.
Anyway, thank you for all your help.
It's running along. and it will finish in about... 4 days xDD
In case anyone finds it interesting, I did a small modification to Shell's script to make it not only skip my .CHECK files, but also skip .7z archives. This way I can interrupt the script and resume it later and it won't re-archive the archives that have already been made.
I also added a "PAUSE" command at the end so that the console window won't automatically close when the script finishes, and, instead, waits for me to close it. That way I know that everything went as expected and nothing else caused it to prematurely close.
Last edit: Radorn Keldam 2018-06-30
Hi guys, sorry for posting on an old discussion.
Lets say I have a directory with 570 regular folders, within everyone of those folders there are 100++ Zipped archives.
I want a script that test the newest .zip inside every 570 folder automatically everyday and also logs the result in some way. But I am stuck.
In order to find the newest file, use
dir /b /o-d *.zip
in thefor /f
loop - its name will appear in the first line. If you log the result, you can add the check whether the log file exists in the loop, thus the files after the first one will be skipped.You wouldnt have an example to look at?
Well, it is a bit off-topic here. I'll try to provide an example; however, I have not tested it thouroughly. Generally, it is better to write a script yourself - this helps you to correct it should anything fail.
for /d %%a in (*) do (
del %%a\logfile.log
for /f %%b in ('dir /b /o-d %%a\*.zip') do (
pushd %%a
7z t -bse1 %%b >logfile.log
if errorlevel 1 echo Error with %%a\%%b
popd
)
)