-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdren.py
98 lines (84 loc) · 2.93 KB
/
hdren.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#-------------------------------------------------------------------------------
# Name: HdRen
# Purpose: rename files adding their header hexdump, for a quick & easy
# grouping by filetype
#
# Author: Marco Pontello - http://mark0.net
#
# Created: 02/11/2013
# Copyright: (c) 2015 Marco Pontello
# License: The MIT License (MIT) - http://opensource.org/licenses/MIT
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import os
import argparse
import glob
import fnmatch
PROGRAM_VER = "1.02"
PARAMS = {}
def header_intro():
"""Display the usual presentation, version, (C) notices, etc."""
print
print "HdRen - hexdump file renamer v%s - (C) 2015 By M.Pontello" % \
(PROGRAM_VER)
print
def get_cmdline():
"""
Evaluate command line parameters, usage & help.
"""
parser = argparse.ArgumentParser(
description="Rename a number of files \
adding their n bytes header hexdump.",
prefix_chars='-/+',
version = "DupDel v" + PROGRAM_VER)
parser.add_argument("filenames", action="store", nargs="+",
help="files to scan (can include path & wildcards)")
parser.add_argument("-l", "--len", type=int, default=4,
help="header's lenght", metavar="n")
parser.add_argument("-r", "--recurse", action="store_true",
help="recurse subdirs")
res = parser.parse_args()
PARAMS["files"] = res.filenames
PARAMS["hdrlen"] = res.len
PARAMS["recurse"] = res.recurse
def hexdump(filename, hdrlen):
f = open(filename, "rb")
buffer = f.read(hdrlen)
f.close()
buffer = buffer+chr(0x00)*hdrlen
buffer = buffer[:hdrlen]
text = ""
for b in buffer:
text += "%02x" % ord(b)
return text.upper()
def renfiles(filenames, hdrlen):
for filename in filenames:
hextext = hexdump(filename, hdrlen)
newfilename = os.path.join(os.path.dirname(filename),
hextext + "=" + os.path.basename(filename))
try:
os.rename(filename, newfilename)
print newfilename
except:
print "error: can't rename", filename
def main():
header_intro()
get_cmdline()
filenames = []
for filespec in PARAMS["files"]:
filepath, filename = os.path.split(filespec)
if not filepath:
filepath = "."
if not filename:
filename = "*"
for wroot, wdirs, wfiles in os.walk(filepath):
if not PARAMS["recurse"]:
wdirs[:] = []
for fn in fnmatch.filter(wfiles, filename):
filenames.append(os.path.join(wroot, fn))
filenames = sorted(set(filenames))
filenames = [os.path.abspath(filename) for filename in filenames]
hdrlen = PARAMS["hdrlen"]
renfiles(filenames, hdrlen)
if __name__ == '__main__':
main()