Script/Button to clean subdirectories in a compressed file (.cbz)

The objective of this script is to create .cbz files without subdirectories because some utilities have problems with them mainly when there very long and generate errors or bad results.

Steps:
1- Start with a compressed file (.cbz, but we can also begin with .zip, .cbr, .rar)
2- Uncompressed it
3- Clean flatten structure directory
4- Delete original file
5- Generate a new compressed file with same name as the deleted one but with .zip (default)
6- Rename the .zip to .cbz to get the same filename.ext as we start

For the flatten operation I am using suction.exe which I found easy.
I also tried with DirFlatten.exe but I haven't been able to make it run. I also comment the operation in order someone can correct or see what am I doing wrong.

@nofilenamequoting
@sync:
// @Output: Step 2 
copy EXTRACT=sub HERE UNATTENDED=yes
//
// @Output: Step 3
@set extract_dir={sourcepath}{file|noext}
c:\USBTools\Utilities\suction.exe "{$extract_dir}"
// c:\USBTools\Utilities\DirFlatten.exe "{$extract_dir}"
//
// @Output: Step 4
Delete FILE="{file$}" NORECYCLE QUIET 
//
// @Output: Step  5
Copy ARCHIVE=single HERE FILE="{$extract_dir}" 
//
// @Output: Step 6
rename PATTERN "{file|ext=zip}" TO "{file|ext=cbz}"

Everything works fine except Step 6. I use same command as
How change File Extension (from .zip to .cbz) in a script with "rename PATTERN":
but the problem persist.
Hope someone can help to discover the problem or improve this little utilitie.

Improvements:
I have found when Main directory, filename, subdirectory and compressed names are VERY LONG suction.exe doesnt process these files. I don't know exactly the numbers but I have a problem with a file with these figures:
In my case, the path for the file I am having troubles is 258 char long in total

  • Main directory : 9 chars
  • Filename : 105 chars
  • Subdirectory: 98 chars
  • Image : 41 chars
  • TOTAL: 253 + \\\

Any suggestion to automate a reduction on the subdirectory or workaround?
I could rename directly to Sub1 (the name is not relevant because the info is on the Filename).

Can't really test right now, but the problem might come from the fact you delete the original file on step 4, hence loosing selection. Then when you reach step 6, {file} is empty since there are no more selected file.
You can try to store the value of the {file} before deleting it (@set as you did in the other post) and use that variable name in the final step.

EDIT : it seems you already have it in {extract_dir}
EDIT2 : As for suction, I don't know what it does, but if the goal in the end is to reduce folder depth, it's probably doable with Opus. If it has to handle "complicated" things (regroup in top folder files scattered in multiple sub-dirs), it will probably require scripting (JS or VB) and a bit of logic to decide what to do on duplicate names if it has to be automagic without user intervention ...

This flattens a folder:

Rename FROM=* PATTERN=* TO=$.\* RECURSE WHENEXISTS=rename

Append this to the Copy ARCHIVE command to skip the folders:

FILTERDEF type match files
2 Likes

Thanks @PassThePeas . I dind't knew I loose a variable if the file associated is deleted.
Then I modified the script using loaded variables instead of {file} option.

@nofilenamequoting
@sync:
@set file_init={file}
@set file_cbz={file|ext=cbz}
@set file_zip={file|ext=zip}
@output: {$file_init}
@output: {$file_cbz}
@output: {$file_zip}
// @Output: Step 2 
copy EXTRACT=sub HERE UNATTENDED=yes
//
// @Output: Step 3
@set extract_dir={sourcepath}{file|noext}
c:\USBTools\Utilities\suction.exe "{$extract_dir}"
// c:\USBTools\Utilities\DirFlatten.exe "{$extract_dir}"
//
// @Output: Step 4
Delete FILE="{file$}" NORECYCLE QUIET 
//
// @Output: Step  5
Copy ARCHIVE=single HERE FILE="{$extract_dir}" 
@output: {$file}
@output: {$file_cbz}
@output: {$file_zip}
//
// @Output: Step 6
rename PATTERN "{$file_zip}" TO "{$file_cbz}"

When I run it, I have this log file

test - Copy.cbz
test - Copy.cbz
test - Copy.zip
copy EXTRACT=sub HERE UNATTENDED=yes
c:\USBTools\Utilities\suction.exe "E:\COMICS\TEST\test - Copy"
Delete FILE="test - Copy.cbz" NORECYCLE QUIET
Copy ARCHIVE=single HERE FILE="E:\COMICS\TEST\test - Copy"

test - Copy.cbz
test - Copy.zip
Rename PATTERN "test - Copy.zip" TO "test - Copy.cbz"

Efectively, the output {file} doesn't show anything after deleting the file.
But the command rename seams to be correct (as before) but doesn' t rename anything.
crazy :scream:

You should use code blocks rather than quotes to post code, otherwise the forum will interpret backslashes and other characters in unwanted ways.

Formatting tips specific to the Opus forum has more detail, and is also linked above the post editor whenever you're writing a post or reply.

Sorry. I correct it ..inmediatelly

Don't worry, you don't.

Try for yourself:

@set myVar={file}
/home\dopusrt.exe /argsmsgbox {$myVar} 
Delete FILE={file}
/home\dopusrt.exe /argsmsgbox {$myVar} 

Thanks @lxp for clarification. As said I couldn't test, and since the variable is not a user set one but the {file} variable which is supposed to represent the selected file, I thought it could be re-evaluated at that moment.

Just tested this case and it actually works :

@output "--"
@output {file}
DELETE {file}
@output {file}

{file} remains.
All in all, that does not explain why the last step of the original script is not working ...

1 Like

With everybody help, I have been able to get something working:

@nofilenamequoting
@sync:
@set file_init={file}
@set file_cbz={file|ext=cbz}
@set file_zip={file|ext=zip}
// @Output: Step 2 
copy EXTRACT=sub HERE UNATTENDED=yes
//
// @Output: Step 3
@set extract_dir={sourcepath}{file|noext}
c:\USBTools\Utilities\suction.exe "{$extract_dir}"
// c:\USBTools\Utilities\DirFlatten.exe "{$extract_dir}"
//
// @Output: Step 4
Delete FILE="{file$}" NORECYCLE QUIET 
//
// @Output: Step  5
C:\USBTools\Utilities\7z.exe a -tzip "{sourcepath}\{$file_cbz}" "{$extract_dir}\*.*"
//
// @Output: Step 6
Delete "{$extract_dir}

Using 7z.exe and as the majority of files I have to process has ONLY one subdirectory and no risk then to overlap if in uncompress everything without tree hierarchy.
Then for my basic needs I can reduce my script to two 7z commands

@nofilenamequoting
@sync:
@runmode hide:
@set file_init={file}
@set file_cbz={file|ext=cbz}
@set file_zip={file|ext=zip}
@set extract_dir={sourcepath}{file|noext}
// extract flat
C:\USBTools\Utilities\7z.exe e -o"{$extract_dir}" "{sourcepath}\{file}"
Delete "{sourcepath}\{file}"
// compress only files directly to .cbz file
C:\USBTools\Utilities\7z.exe a -tzip "{sourcepath}\{$file_cbz}" "{$extract_dir}\*.*"
Delete "{$extract_dir}"

With this I avoid the rename problem and the path length problem.
Anyway, I would like to understand why the rename PATTERN command doesn't work in the previous script (where I don't use 7z.exe to avoid the problem)

I just tested the script from your second post, just commenting the call to suction which I don't have, and it worked for me ... :interrobang:

@nofilenamequoting
@sync:
@set file_init={file}
@set file_cbz={file|ext=cbz}
@set file_zip={file|ext=zip}
@output: {$file_init}
@output: {$file_cbz}
@output: {$file_zip}
// @Output: Step 2 
copy EXTRACT=sub HERE UNATTENDED=yes
//
// @Output: Step 3
@set extract_dir={sourcepath}{file|noext}
// c:\USBTools\Utilities\suction.exe "{$extract_dir}"
// c:\USBTools\Utilities\DirFlatten.exe "{$extract_dir}"
//
// @Output: Step 4
Delete FILE="{file$}" NORECYCLE QUIET 
//
// @Output: Step  5
Copy ARCHIVE=single HERE FILE="{$extract_dir}" 
@output: {$file}
@output: {$file_cbz}
@output: {$file_zip}
//
// @Output: Step 6
rename PATTERN "{$file_zip}" TO "{$file_cbz}"

Very strange. I have copied the same script (commented suction)
and it is till not working. Here screen capture where you have the log and the directory where the .zip file is not converted to .cbz


very strange for me. As I explain before if I put the rename command in a single button it works. Then the disfunction is related with something happening before.
I thought i could be the file deletion. but adapting the variables to avoid it, doesn't solve the problem.
But now @PassThePeas say he run it without problem :alien:

I solved my need with another tool. But after all the time I have spend on this bloody command I would like to understand something.

Maybe it's related to the new archive being selected or not.
I tried to add Select *.zip DESELECT just before calling the step rename : it's not working anymore.

I tried to see if I could find any related setting that could be different between our two configurations, but the ones I found had no effect on the original script (without the DESELECT). I tried :
File Operations / Copy / Select automatically new files copied/moved
File Operations / Options / Deselect files used in a function
(sorry if it's not the exact name of the options, I'm trying to guess the english setting from my french one :slight_smile: ).

Anybody would have an idea of a setting that would cause the zip archive not to be selected ?

Correct. I add select *.zip just before the "rename" and it works.
Partly understood. :wink:

You can use Rename FROM *.zip ... to make it act on all .zip files, without having to select them (which may not always be reliable if the file you're selecting was only just created; the lister might not have seen the new file yet). Most commands let you specify a path or wildcard to work on instead of the selection.

New problem to improve the scritps.
I see it works fine if I run only with one file.
I tried to select more than one file, then it doesn't work ok.
It run each command on all selected entry before beginning the next command/line.
Is there an option to make the scritp execute completly on the first file selected before running the script with the second file and so on?

See @perfile:

https://docs.dopus.com/doku.php?id=reference:command_reference:command_modifier_reference

I have included @nodeselect and @sync but this till doesn't do what it should.

@nofilenamequoting
@nodeselect
@runmode hide
//
@set file_init={file$}
@output: {$file_init}
@set file_cbz={file$|ext=cbz}
@set file_zip={file$|ext=zip}
@set extract_dir={sourcepath}{allfile$|noext}
@output: {$extract_dir}
//
@sync: C:\USBTools\Utilities\7z.exe e -o"{$extract_dir}" "{sourcepath}\{file$}" -aou
Delete "{sourcepath}\{file}"
//@confirm:
@sync: C:\USBTools\Utilities\7z.exe a -tzip "{sourcepath}\{$file_zip}" "{$extract_dir}\*.*"
Delete "{$extract_dir}"

I said @perfile, not either of those. :slight_smile:

Sorry I loose this line and just add the link to the manual.
Now I understand. I will test @perfile. Thanks.