Skip to content

Instantly share code, notes, and snippets.

@FarisHijazi
Last active May 6, 2024 14:28
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FarisHijazi/8750e7978def3996f793d85272b63b1f to your computer and use it in GitHub Desktop.
Save FarisHijazi/8750e7978def3996f793d85272b63b1f to your computer and use it in GitHub Desktop.
7zip: Unzip and delete context menu. Registry entry and .bat file for windows context menu "unzip and delete" command. instead of needing to unzip, then delete an archive manually
@echo off
set output_dir=%~n1
IF EXIST "%output_dir%\" (
echo "%output_dir%\" already exists, gonna increment the name
set "n=0"
:loop
set /a n+=1
set "output_dir=%output_dir%_%n%"
if exist "%output_dir%" echo "%output_dir%" already exists & goto :loop
)
"C:\Program Files\7-Zip\7z.exe" x %1 -o"%output_dir%\"
IF %ERRORLEVEL% EQU 0 IF EXIST "%output_dir%\" (
echo "%output_dir%\" was created
del %1
) else (
Echo An error was found & pause
)
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\Unzip and delete]
"icon"="C:\\Program Files\\7-Zip\\7zG.exe"
[HKEY_CLASSES_ROOT\*\shell\Unzip and delete\command]
@="\"<PATH TO>\\unzip_and_delete.bat\" \"%1\""
@Robert-K
Copy link

You probably want to replace that 'e' on line 13 with an 'x'.
Extraction with 'e' dumps all files into the output folder root.
'x' preserves directory structure (the default when using the 7-Zip context menu).

@FarisHijazi
Copy link
Author

@Robert-K done, thanks for letting me know

@Robert-K
Copy link

Thanks for sharing! Great script, exactly what i was looking for :)

@areen-c
Copy link

areen-c commented Jul 11, 2022

Unfortunately, this script won't work with protected archive

@boktai1000
Copy link

boktai1000 commented Apr 16, 2024

I've tried adding this but unfortunately it never works for me. For my example, I have the script located at D:\Scripts\unzip_and_delete.bat

image

My script looks as follows:

image


Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\Unzip and delete]
"icon"="C:\\Program Files\\7-Zip\\7zG.exe"
[HKEY_CLASSES_ROOT\*\shell\Unzip and delete\command]
@="\"D:\Scripts\\unzip_and_delete.bat\" \"%1\""

And my registry values always look like this:

image

image

The error I get is as follows:

image

[Window Title]
D:\STAGING\TEST\New Text Document.zip

[Content]
This file does not have an app associated with it for performing this action. Please install an app or, if one is already installed, create an association in the Default Apps Settings page.

[OK]

UPDATE: Right after making this post, I was able to figure it out! I guess I just had to complain about it, but I hope my comment will help someone else in the future. What tipped me off is the grey "S" that was appearing in Notepad++ syntax highlighting, and I was missing another backslash. In my example / setup, this is the correct registry - please use as a reference or adjust as you need. Enjoy and thanks, my apologies for the post and update but I hope this bit of troubleshooting helps someone else in the future!

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\Unzip and delete]
"icon"="C:\\Program Files\\7-Zip\\7zG.exe"
[HKEY_CLASSES_ROOT\*\shell\Unzip and delete\command]
@="\"D:\\Scripts\\unzip_and_delete.bat\" \"%1\""

@boktai1000
Copy link

I noticed an unfortunate side effect that I'm not entirely sure if others have encountered, but if you attempt to unzip and delete a file that has an & in the name you'll wind up with a truncated directory nameand Windows will be unable to delete the directory.

Fortunately, you can use 7-Zip itself as a File Manager and it seems to have the ability to allow you to rename the folder, and then you can delete it or use it appropriately.

I'm sure there's probably a way to add some additional logic to prevent or fix this from happening with escaping characters, but hopefully someone smarter than myself will be able to identify what is required to do this.

Just a heads up here and to anyone else that may see this - thank you! At the moment I need to be careful when batch unzip/deleting large amounts of files and identifying if they have a & before processing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment