WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Decompile CHM using 7-Zip

 Keywords: Decompile Help file CHM  7-Zip 

;==========================================================================================================================================
;
; Extract all elements from a CHM file into a folder tree (a. k. a. CHM decompiling).
;
; This script requires the installed 7-Zip File Archiver. Big "Thank You" to Igor Pavlov and the 7-Zip folk!
;
;------------------------------------------------------------------------------------------------------------------------------------------
; Version 1.02: 20100130:
;               Changed: WinBatch AskFileName dialog replaced by MSComDlg.CommonDialog ...
;               ... to enable selection of long filenames up to maximal buffer size of 32767 byte.
;               Added: Box to display status message.
;               Added: Multi file support.
;               Added: Auto rename of folders up to number 19 when duplicates arise.
;               Added: File date time of the original file appended to the respective document folder
;               Added: Alphabetical index folder inserted between root folder and document folder.
; Version 1.01: 20100129:
;               Added: Support for target folder structure.
; Version 1.00: 20100129:
;               Initial published.
; (c)Detlev Dalitz.20100129.
;==========================================================================================================================================

GoSub Define_Functions

; Define the location of the 7-Zip archiver exe file here.
strZipExe = ShortCutDir (38) : "\7-Zip\7z.exe" ; e. g. "C:\Program Files\7-Zip\7z.exe".


; Define and run AskFileName dialog.
strAFN_Title = "Select a CHM file for decompiling"     ; Title of the file name select box.
strAFN_Folder = ""                                     ; Initial drive and directory.
strAFN_Filetypes = "CHM files|*.chm|All files|*.*"     ; The "|" separates the descriptive phrase from the mask.
strAFN_Defaultmask = "*.chm"                           ; Default filename or mask.
intAFN_Flag = 512 | 524288 | 4096 | 2097152 | 2048 | 4 ; Open style multiple filenames (MSComDlg.CommonDialog.1)

strFileChmList = udfAskFileName (strAFN_Title, strAFN_Folder, strAFN_Filetypes, strAFN_DefaultMask, intAFN_Flag)
If strFileChmList == "" Then Goto CANCEL

BoxOpen ("CHM Decompiling", "")
WinPlace (650, 100, 1000, 450, "")

intItems = ItemCount (strFileChmList, @TAB)
intItem = 0

While strFileChmList != ""
   strFileChm = ItemExtract (1, strFileChmList, @TAB)
   strFileChmList = ItemRemove (1, strFileChmList, @TAB)

   ; Get file date time.
   strDT = "." : StrInsert (StrReplace (FileYmdHms (strFileChm), ":", ""), ".", "", 9, 1)

   ; Create our folder structure.
   ;strFolderChmRoot = ShortCutDir ("Local Settings") : "\CHM_DECO\"
   strFolderChmRoot = "P:\CHM_DECO\"

   blnResult = DirMake (strFolderChmRoot)
   Terminate (!blnResult, "Terminated.", "Cannot create CHM root folder." : @LF : strFolderChmRoot)

   strFolderChmIndex = strFolderChmRoot : StrUpper (StrSub (FileRoot (strFileChm), 1, 1)) : "\"
   blnResult = DirMake (strFolderChmIndex)
   Terminate (!blnResult, "Terminated.", "Cannot create CHM Index folder." : @LF : strFolderChmIndex)

   intAutoRenameMax = 19
   intAutoRename = 0
   strAutoRename = ""
   strFolderChmDocs = strFolderChmIndex : FileRoot (strFileChm) : strDT : "\"
   While DirExist (strFolderChmDocs)
      intAutoRename = intAutoRename + 1
      If intAutoRename > intAutoRenameMax Then Break
      strAutoRename = "_" : intAutoRename
      strFolderChmDocs = strFolderChmIndex : FileRoot (strFileChm) : strDT : strAutoRename : "\"
   EndWhile
   blnResult = @FALSE
   If intAutoRename <= intAutoRenameMax Then blnResult = DirMake (strFolderChmDocs)
   Terminate (!blnResult, "Terminated.", "Cannot create CHM document folder." : @LF : strFolderChmDocs)

   strFolderChmOriginal = strFolderChmDocs : "Original\"
   blnResult = DirMake (strFolderChmOriginal)
   Terminate (!blnResult, "Terminated.", "Cannot create CHM original folder." : @LF : strFolderChmOriginal)

   strFolderChmDecompiled = strFolderChmDocs : "Decompiled"
   blnResult = DirMake (strFolderChmDecompiled)
   Terminate (!blnResult, "Terminated.", "Cannot create CHM decompiled folder." : @LF : strFolderChmDecompiled)

   ; Copy selected CHM file to our original folder. Overwrites existing file without permission.
   blnResult = FileCopy (strFileChm, strFolderChmOriginal, @FALSE)


   ; Display status message.
   intItem = intItem + 1
   strMsgText = "Items selected: " : intItems : @LF
   strMsgText = strMsgText : "Item : " : intItem : @LF
   strMsgText = strMsgText : "Folder: " : @LF : StrReplace (strFolderChmDocs, "\", "\" : @LF)
   BoxText (strMsgtext)


   ; Define 7-Zip parameter string.
   strP1 = 'x'                         ; x = Extract with full paths command.
   strP2 = '"' : strFileChm : '"'      ;
   strP3 = '-aoa'                      ; -aoa = Overwrite all existing files without prompt switch.
   strP4 = '-o'                        ; -o = Set output directory switch.
   strP5 = '"' : strFolderChmDecompiled : '"' ;
   strP6 = '*'                         ; To process all files, you must use a * wildcard.
   strP7 = '-r'                        ; -r = Enable recurse subdirectories switch.
   strZipParams = strP1 : " " : strP2 : " " : strP3 : " " : strP4 : strP5 : " " : strP6 : " " : strP7

   ; Run 7-Zip extraction.
   intResult = RunShell (strZipExe, strZipParams, "", @ZOOMED, @GETEXITCODE)
   If intResult != 0 Then Pause ("Error Message from 7-Zip", "Exit Code = " : intResult)

EndWhile

; Visit last item's target folder.
RunZoom ("explorer.exe", "/e,/select," : strFolderChmDecompiled)

:CANCEL
BoxShut ()
Exit
;==========================================================================================================================================
:Define_Functions
;----------------------------------------------------------------------------------------------------------------------
#DefineFunction udfAskFileName (strTitle, strFolder, strFilter, strFilename, intFlags)
objDialog = ObjectCreate ("MSComDlg.CommonDialog.1")
objDialog.DialogTitle = strTitle
objDialog.InitDir = strFolder
objDialog.Filter = strFilter
objDialog.FilterIndex = 1
objDialog.FileName = strFilename
objDialog.Flags = intFlags
objDialog.MaxFileSize = 32767
objDialog.CancelError = @TRUE
intLastErrorMode = ErrorMode (@OFF)
LastError ()
objDialog.ShowOpen
ErrorMode (intLastErrorMode)
If LastError () > 0 Then Return ""
strSep = @TAB
objRegEx = ObjectCreate ("VBScript.RegExp")
objRegEx.Global = @TRUE
objRegEx.Pattern = "\0"
strFileNames = objRegEx.Replace(objDialog.FileName, strSep)
Drop (objRegEx, objDialog)
intItems = ItemCount (strFileNames, strSep)
If intItems == 1 Then Return strFileNames
strFolder = FilePath (ItemExtract (1, strFileNames, strSep) : "\")
strOut = ""
For intItem = 2 To intItems
   strOut = ItemInsert (strFolder : ItemExtract (intItem, strFileNames, strSep), -1, strOut, strSep)
Next
Return strOut
;..........................................................................................................................................
; This udf is a replacement for the standard WinBatch AskFileName dialog function
; to enable selection of long filenames up to maximal buffer size of 32767 byte.
;
; 20100130.Added backslash to let Filepath work correctly: "strFolder = FilePath (ItemExtract (1, strFileNames, strSep) : "\")"
; 20081027.Detlev Dalitz with crucial support from tonyd.
;..........................................................................................................................................
; Flags Property (Open, Save As Dialogs)
;
; cdlOFNAllowMultiselect      = 512      ; &H200     ; Specifies that the File Name list box allows multiple selections. The user can select more than one file at run time by pressing the SHIFT key and using the UP ARROW and DOWN ARROW keys to select the desired files. When this is done, the FileName property returns a string containing the names of all selected files. The names in the string are delimited by spaces.
; cdlOFNCreatePrompt          = 8192     ; &H2000    ; Specifies that the dialog box prompts the user to create a file that doesn't currently exist. This flag automatically sets the cdlOFNPathMustExist and cdlOFNFileMustExist flags.
; cdlOFNExplorer              = 524288   ; &H80000   ; Use the Explorer-like Open A File dialog box template. Works with Windows 95, Windows NT 4.0, or later versions.CdlOFNNoDereferenceLinks  &H100000  Do not dereference shell links (also known as shortcuts). By default, choosing a shell link causes it to be dereferenced by the shell.
; CdlOFNExtensionDifferent    = 1024     ; &H400     ; Indicates that the extension of the returned filename is different from the extension specified by the DefaultExt property. This flag isn't set if the DefaultExt property is Null, if the extensions match, or if the file has no extension. This flag value can be checked upon closing the dialog box.
; cdlOFNFileMustExist         = 4096     ; &H1000    ; Specifies that the user can enter only names of existing files in the File Name text box. If this flag is set and the user enters an invalid filename, a warning is displayed. This flag automatically sets the cdlOFNPathMustExist flag.
; cdlOFNHelpButton            = 16       ; &H10      ; Causes the dialog box to display the Help button.
; cdlOFNHideReadOnly          = 4        ; &H4       ; Hides the Read Only check box.
; cdlOFNLongNames             = 2097152  ; &H200000  ; Use long filenames.
; cdlOFNNoChangeDir           = 8        ; &H8       ; Forces the dialog box to set the current directory to what it was when the dialog box was opened.
; cdlOFNNoLongNames           = 262144   ; &H40000   ; No long file names.
; CdlOFNNoReadOnlyReturn      = 32768    ; &H8000    ; Specifies that the returned file won't have the Read Only attribute set and won't be in a write-protected directory.
; cdlOFNNoValidate            = 256      ; &H100     ; Specifies that the common dialog box allows invalid characters in the returned filename.
; cdlOFNOverwritePrompt       = 2        ; &H2       ; Causes the Save As dialog box to generate a message box if the selected file already exists. The user must confirm whether to overwrite the file.
; cdlOFNPathMustExist         = 2048     ; &H800     ; Specifies that the user can enter only valid paths. If this flag is set and the user enters an invalid path, a warning message is displayed.
; cdlOFNReadOnly              = 1        ; &H1       ; Causes the Read Only check box to be initially checked when the dialog box is created. This flag also indicates the state of the Read Only check box when the dialog box is closed.
; cdlOFNShareAware            = 16384    ; &H4000    ; Specifies that sharing violation errors will be ignored.
;    OFN_ENABLEHOOK           = 32       ; &H20      ;
;    OFN_ENABLETEMPLATE       = 64       ; &H40      ;
;    OFN_ENABLETEMPLATEHANDLE = 128      ; &H80      ;
;    OFN_NODEREFERENCELINKS   = 1048576  ; &H100000  ;
;    OFN_NONETWORKBUTTON      = 131072   ; &H20000   ;
;    OFN_NOTESTFILECREATE     = 65536    ; &H10000   ;
;..........................................................................................................................................
#EndFunction
;----------------------------------------------------------------------------------------------------------------------
Return ; from GoSub Define_Functions
;==========================================================================================================================================
; Exit Codes from 7-Zip
; Code Meaning
; 0 No error
; 1 Warning (Non fatal error(s)). For example, one or more files were locked by some other application, so they were not compressed.
; 2 Fatal error
; 7 Command line error
; 8 Not enough memory for operation
; 255 User stopped the process
;==========================================================================================================================================


Article ID:   W18243
Filename:   Decompile CHM using 7-Zip.txt
File Created: 2010:02:01:10:04:06
Last Updated: 2010:02:01:10:04:06