Welcome, guest | Sign In | My Account | Store | Cart

Searches your hard drive for all files specified and generates an HTML web page list of the files found and links to the file itself. This search is fast and very little code to complete the task...

Python, 77 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'****************************************************'
'Created by C. Nichols #B0)~                         '
'E-mail: oldnich@digitaldarknet.net                  '
'Website: www.digitaldarknet.net                     '
'Created: 10/25/2002                                 '
'Version: 0.1.4                                      '
'Desc: Slither for Micro$oft Windoze                 '
'Searches your hard drive for all files specified    '
'and generates an HTML web page list of the          '
'files found and links to the file itself.           '
'****************************************************'
'                                                    '
'                        #                           '
'                       0 0                          '
'~~~~~~~~~~~~~~~~~uuu~~~~U~~~~uuu~~~~~~~~~~~~~~~~~~~~'
"!!!!!!!!!!!!HERE'S LOOKING AT YOU KID!!!!!!!!!!!!!!!"
'****************************************************'
# Libs ----------------------------------------------
import sys,os,string,time
#Globals---------------------------------------------
Search_path = ''
Report_path = ''
File_type = ''
#----------------------------------------------------

def htmlGen(path, rpath):
    dirs = [path]
    F_path = rpath
    dir_list=[] 
    file_list=[]
    fCount=0
    print 'Building list, please wait...'
    while dirs:
        dir_list=dirs.pop(0)
        try:
            for items in os.listdir(dir_list+'\\'):
                if os.path.isdir(dir_list+'\\'+items):
                    dirs+=[dir_list+'\\'+items]
                elif os.path.isfile(dir_list+'\\'+items):
                    if items[-4:].lower()=='.'+File_type:
                        file_list.append(str(dir_list+'\\'+items))
        except:
            pass #Filters non-accessable directories.

    fCount=len(file_list) # Get file count        
    file_list.sort()
    
    #Create HTML-------------------------------------
    open(F_path,'w').write('<html>\n<title>Search Results</title>\n<body bgcolor="#ffffff" text="#000000">\n<table align="left" bgcolor="#ffffff" border="0">\n<th align="left"><h1><u>'+File_type+' file list - total files: '+str(fCount)+'</u></h1><th><tr>\n')           
    for files in file_list:
        open(F_path,'a').write('<td><a href="'+files+'">'+files.upper()+'</a></td><tr>\n')
    open(F_path,'a').write('\n</table>\n</body>\n</html>')
    print F_path+' file created successfully...'
    
#RUN AND GEN YOUR HTML FILE--------------------------    
try:
    if __name__ == '__main__':
        print 'Slither 0.1.4 by Mohawke #B0)~\n' 
        Search_p = raw_input('Enter drive to search (letter only): ')
        Search_p=Search_p.strip()
        Search_path = str(Search_p+':')
        Report_path = str(Search_path+'\\Slither Report')
        
        File_t=raw_input('Enter file extension (txt, etc): ')
        File_type=File_t.strip()
        
        if not os.path.exists(Report_path): os.mkdir(Report_path)
            
        File_p=raw_input('Enter file name without .ext: ')
        File_p=File_p.strip()
        File_path = str(Report_path+'\\'+File_p+'.html')
        
        htmlGen(Search_path, File_path)
        raw_input('Done... Hit <Return> to end.')

except:
    raw_input('Error, try again... Hit <Return> to end.')

3 comments

Peter Scott 20 years, 11 months ago  # | flag

CSS and extensibility. To make this recipe more extensible, it would be a good idea to have seperate functions for things like walking the directories, making the table, and inserting this into the rest of the HTML.

It would also be a good idea to have the table generating code put in CSS (like 'class="fileTable"') instead of hard-coded values (which are depricated anyway), and then if the thing gets extended users can have more control over appearance.

Peter Scott 20 years, 8 months ago  # | flag

My version. Actually, this recipe looks worse the more I look at it. It so offended my sense of aesthetics that I made my own version, at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/200131

Charles Nichols (author) 19 years, 4 months ago  # | flag

Alright, but who needs or would use something like this anyway? It's just an example of file searching on Windoze not a realistic application so people can have an html page of local files - who cares to have that?