1

Introduction: I have 15 Nirsoft.com free utilities that I downloaded into a Windows folder ("Source Folder"), along with a PDF copy of the Nirsoft webpage from which I downloaded the utility. My python script below is designed to make a subdirectory in the Source Folder for each Nirsoft utility in the Source Folder. The script is then supposed to move into each new subdirectory, the zipped utility that belongs in that subdirectory, along with the PDF copy of the Nirsoft webpage from which I downloaded the utility.

My script copies the zipped utilities just fine. But the PDF copy of the Nirsoft webpage from which I downloaded the utility does not get copied correctly. The PDF copies in all of the subdirectories are corrupt and mysteriously each PDF copy has the same number of bytes as the zipped utility in that subdirectory.

Detailed Explanation: For each utility, I downloaded into the Source Folder: 1. The Nirsoft utility program, and 2. A PDF copy of the Nirsoft page about the utility.

In my script, "utils_all_ls" is a list of lists. Each list ("util_ls") in "utils_all_ls" is a list containing:

  1. The name of a subdirectory to be created;
  2. A description of the utility (that I'll use later when I create a button menu);
  3. A PDF copy of the Nirsoft page about the utility;
  4. The zipped Nirsoft utility downloaded from that page.

My python script below does create the subdirectories named in each util_ls[0]. My script does succeed in copying into each subdirectory a good copy of util_ls[4], which is the zipped Nirsoft utility. But the PDF copy of the Nirsoft page about that utility is always corrupt, and the PDF file always has the same number of bytes as the zipped utility in that subdirectory.

I can't figure out why the PDF files (1) always have the same number of bytes as the zipped utility, and (2) are corrupt.

Here's my script:

import os
import sys
import re
import shutil
import zipfile

utils_all_ls = [["Process Viewer", "Process Viewer for Windows", "CurrProcess_ Freeware Process Viewer for Windows.pdf", "cprocess.zip"], ["DiskCountersView", "Hard disk info, including health", "DiskCountersView.pdf", "diskcountersview.zip"], ["DiskSmartView", "Retrieves disk health info, etc.", "DiskSmartView.pdf", "disksmartview.zip"], ["Windows Drivers Loaded", "Windows Drivers Loaded info", "DriverView_ Loaded Windows Drivers List.pdf", "driverview-x64.zip"], ["FolderChangesView", "Folder & File Changes Viewer", "FolderChangesView - Monitor folder_drive_files changes on Windows.pdf", "folderchangesview.zip"], ["JumpListsView", "Jump lists info viewer", "JumpListsView - View jump lists information stored by Windows 7.pdf", "jumplistsview.zip"], ["LastActivityView", "User actions and events Log", "LastActivityView - View the latest computer activity in Windows operating system.pdf", "lastactivityview.zip"], ["MyUninstaller", "Uninstaller - Nirsoft's", "MyUninstaller_ Alternative uninstaller to the standard Windows Add _ Remove module.pdf", "MyUninstaller___myuninst.zip"], ["ServiWin", "Windows Services & Drivers", "ServiWin_ Windows Services_Drivers Tool (start_stop_restart service).pdf", "serviwin_setup.zip"], ["ShellExView", "Explorer Context-menu fixer", "ShellExView - Shell Extension Manager For Windows.pdf", "shexview-x64.zip"], ["ShellMenuView", "Explorer Context-menu en/disable", "ShellMenuView - Disable_enable context menu items of Explorer.pdf", "shmnview-x64.zip"], ["SpecialFoldersView", "List of all special folders hyperlinked", "SpecialFoldersView - Special Folders Viewer.pdf", "specialfoldersview-x64.zip"], ["USBDeview", "USB devices installed or connected", "USBDeview  View any installed_connected USB device on your system.pdf", "usbdeview-x64.zip"], ["WhatInStartup", "Startup - MsConfig alternative", "WhatInStartup - Disable_delete programs at Windows startup.pdf", "whatinstartup-x64.zip"], ["whatishang", "Hangup or program freeze info", "whatishang-x64  Get information about Windows software that stopped responding_freezing (hang).pdf", "whatishang-x64.zip"], ["wincrashreport", "Crashed Windows app reports", "WinCrashReport - Displays a report about crashed Windows application.pdf", "wincrashreport-x64.zip"], ["winprefetchview", "Prefetch file info ", "winprefetchview-x64  View the content of Windows Prefetch (.pdf", "winprefetchview-x64.zip"]]

print('\r\nLine 12:  print(str(len(utils_all_ls))) = ' + str(len(utils_all_ls)) + '\r\n')


def file_accessible(filepath, mode='r'): # Check if True = File is readable
    ''' Check if a file exists and is accessible. '''
    try:
        f = open(filepath, mode)
        f.close()
    except IOError as e:
        return False

    return True


for util_no in range(0, len(utils_all_ls)):

    #print('\r\nutil_no = ' + str(util_no) + '\r\n')
    old_dir = r"C:\Apps\Nirsoft\Process_Related_Utils"
    util_ls = utils_all_ls[util_no]
    nu_dir = os.path.join(old_dir, util_ls[0])
    print(nu_dir)

    if 1:        
        for i in range(2,4):
            old_file = os.path.join(old_dir, util_ls[i])

            if os.path.isfile(old_file):
                pass
            else:
                sys.exit('\r\nLine 53:  Error! Error!  File problem accessing file: "' + str(util_ls[i]) +'\r\n')

            # Check for read access to foo.txt
            if os.access(old_file, os.R_OK):  # True = This means the file exists AND you can read it.
                pass
            else:
                sys.exit('\r\nLine 53:  Error! Error!  File problem accessing file: "' + str(util_ls[i]) +'\r\n')

            if os.access(old_file, os.W_OK):  # Check for write access to the file, True = you can write to the file.  If You cannot write to the file. It may or may not exist. 
                pass
            else:
                sys.exit('\r\nLine 53:  Error! Error!  File problem accessing file: "' + str(util_ls[i]) +'\r\n')

#            if file_accessible(old_file): # check to see if file is readable 
#                pass
#            else:
#                sys.exit('\r\nLine 53:  Error! Error!  File problem accessing file: "' + str(util_ls[i]) +'\r\n')

        os.mkdir(nu_dir)

        for i in range(2,4):
            if os.path.exists(nu_dir):                           
                nu_file = os.path.join(nu_dir, util_ls[i])
                shutil.copy(old_file, nu_file)

                # extract the zipfiles into the newly created subdirectory
                if 'zip' in str(nu_file):
                    zip_ref = zipfile.ZipFile(nu_file, 'r') #(path_to_zip_file, 'r')   # http://stackoverflow.com/questions/3451111/unzipping-files-in-python
                    #print('\r\nLine 65:  "nu_dir" = ' + str(nu_dir) + '\r\n')
                    zip_ref.extractall(nu_dir) #(directory_to_extract_to)
                    zip_ref.close()

    """  Next, I'll create a python script with a button menu giving access to each of the .exe files and the PDF files.  """
Marc B. Hankin
  • 771
  • 3
  • 15
  • 31
  • You gloss over the creation of the PDF of the Nirsoft webpage(s) — and I don't see it being done in your script. How is that being done? Are you sure the original PDFs are OK? – martineau May 13 '16 at 22:54
  • Most of this code is not relevant to your problem. Try to make a [mcve] – Håken Lid May 13 '16 at 23:06
  • _"The PDF file always has the same number of bytes as the zipped utility in that subdirectory"._ Could it be that it has the same content as the "zipped utility" as well? Naming a file `.pdf` doesn't mean it's a valid pdf. – Håken Lid May 13 '16 at 23:11

0 Answers0