First of all, I have to say My english is NOT good ! So Viewer Discretion Is Adviced
More details about the title:
Usually, there are two ways to compress files into an archive. One way is Select all the files, the other is select the directory of those files.
Problem is When we decompressing an archive, we do not know whether there are files in the archive nor Just a standalone directory. So the most quickly and safely way is "Extract to Folder".
And now, there is another problem came out. Which is "the needless directory" I called in the topic title. Because if there is a standalone directory in the archive, now we have created another Folder outside the standalone folder.
But my Script can handle all those Problems! It will figure out the structure of the Folder which we decompressed and if there is a "needless directory", it would remove the directory.
Here are the major functions:
- Press custom hotkey to decompress the Selected archive. use 7-Zip's GUI module: 7zG.exe
- If there are the same name files or directories in the current directory, Will Rename before Moving.
- Figure out the structure of the Folder we decompressed and if there is a "needless directory", remove it. If there is Only one file in the archive, decompress to the current directory.
- Play a prompt sound when it done.
Screenshot of the decompressing progress:
Note:First install 7-Zip, then specify a hotkey to call this Script.If no sound play at the end, means you should Specify another one instead.
Usage Method:Select the archives you want to decompress, then press the hotkey Specified by you to call this Script.Download Script:http://ttzm33.googlepages.com/SmartDecompressedArchives.ahksource code of the Script:Code:
/* Author: ttzm menk33@163.com 2009-1-25
Note:
First install 7-Zip, then specify a hotkey to call this Script.
If no sound play at the end, means you should Specify another one instead.
Functions:
1. Press custom hotkey to decompress the Selected archive. use 7-Zip's GUI module: 7zG.exe
2. If there are the same name files or directories in the current directory, Will Rename before Moving.
3. Figure out the structure of the Folder we decompressed and if there is a "needless directory", remove it. If there is Only one file in the archive, decompress to the current directory.
4. Play a prompt sound when it done.
Usage Method:
Select the archives you want to decompress, then press the hotkey Specified by you to call this Script.
*/
ClipSaved := ClipboardAll
Clipboard =
Send ^c ; Get archive's paths
ClipWait 3
If ErrorLevel
{
MsgBox Copy archive's paths to Clipboard exceeded 3 seconds and Failed ! Press OK will exit Script.
ExitApp
}
SelectFiles := Clipboard
Clipboard := ClipSaved
ClipSaved =
Loop Parse, SelectFiles, `n, `r ; retrieve archive's paths one by one
{
SplitPath A_LoopField, , ArchiveDIR, , ArchiveName ; Split the Archive's path into its directory and the filename with no path.
DecompressDIR = %ArchiveDIR%\%ArchiveName%
IfExist %DecompressDIR%
{
Msgbox Folder "%ArchiveName%" already Exist before Decompression! So give up this Decompression.
Continue
}
RunWait C:\Program Files\7-Zip\7zG.exe x "%A_LoopField%" -o"%DecompressDIR%" ; Command line Syntax of 7-Zip's GUI Module For decompression.
; Only Find Files in the decompressed directory
Loop %DecompressDIR%\* ; If there is no Files found, the loop will be jumped over !
{
FileNUM = %A_Index%
If FileNUM = 1 ; Find one file, keep its path, directory, and name, then continue.
{
DecompressedFileName = %A_LoopFileName%
Continue
}
Else Goto TheEnd ; Find two files.
}
; Only Find Folders in the decompressed directory
Loop %DecompressDIR%\*, 2 ; If no directory, the loop will be jump over !
{
FolderNUM = %A_Index%
If FolderNUM = 1 ; Find one directory, keep its path, name, and directory, then continue.
{
DecompressedFolderName = %A_LoopFileName%
Continue
}
Else Goto TheEnd ; Find two directories.
}
; Now, we finished the loop Searching of files and directories. Continue compareing the following 4 situations and do the right things !
If FolderNUM = 1
{
If FileNUM = 1
Goto TheEnd ; there are one directory and one file.
Else ; Only one directory, start to move it.
{
If DecompressedFolderName = %ArchiveName%
{
IfExist %ArchiveDIR%\%ArchiveName%(TEMP_Use) ;%ArchiveName%(TEMP_Use) will using below
{
MsgBox Existing "%ArchiveDIR%\%ArchiveName%(TEMP_Use)" cause script didn't remove useless DIR!
Continue
}
FileMoveDir %DecompressDIR%\%DecompressedFolderName%, %ArchiveDIR%\%ArchiveName%(TEMP_Use), R ;Rename
FileRemoveDir %DecompressDIR%
FileMoveDir %ArchiveDIR%\%ArchiveName%(TEMP_Use), %ArchiveDIR%\%ArchiveName%, R
}
Else
{
IfExist %ArchiveDIR%\%DecompressedFolderName%
{
Msgbox Before Moving out, Folder with the same name is already Exist ! So first append "Rename" before moving.
FileMoveDir %DecompressDIR%\%DecompressedFolderName%, %ArchiveDIR%\%DecompressedFolderName%(Rename), R
}
Else FileMoveDir %DecompressDIR%\%DecompressedFolderName%, %ArchiveDIR%\%DecompressedFolderName%, R
FileRemoveDir %DecompressDIR%
}
Goto TheEnd
}
}
Else ; Didn't find directory
{
If FileNUM = 1 ; Only one file, Move it out.
{
IfExist %ArchiveDIR%\%DecompressedFileName%
{
Msgbox Before Moving out, File with the same name is already Exist ! So first append "Rename" before moving.
SplitPath DecompressedFileName, , , EXT, NameNoEXT
FileMove %DecompressDIR%\%DecompressedFileName%, %ArchiveDIR%\%NameNoEXT%(Rename).%EXT%
}
Else FileMove %DecompressDIR%\%DecompressedFileName%, %ArchiveDIR%
FileRemoveDir %DecompressDIR%
Goto TheEnd
}
Else
{
MsgBox This is an empty Archive.
Continue
}
}
TheEnd:
SoundPlay C:\Program Files\Messenger\type.wav, 1 ; Here you can specify the prompt sound.
FileNUM = ; Empty this variable and the below variable, so they can reuse in the next loop's 4 situations.
FolderNUM =
}
ExitApp
Summary of the source code:1. Copy the path of Selected archives, then restore the Clipboard.
2. Useing a string parsing loop to get those paths one by one. This is the outermost loop.
3. Calling the GUI module of 7-Zip to decompress.
4. Calling a File-loop to Only get Files in the decompressed Folder.
5. Calling a File-loop to Only get Directories in the decompressed Folder.
6. Integrating the two Loop's results above, then compare to the following 4 situations and do the right things:
a. One file and one directory.
b. Only one directory.
c. Only one file.
d. Empty archive.
7. Play the prompt sound, then empty the used variables and go back to treat the next archive.