From 8ea6ea99652a46d4602be4503e216077ee010c65 Mon Sep 17 00:00:00 2001 From: Felipe Maza Date: Sun, 5 Feb 2017 08:11:33 +0100 Subject: [PATCH] Update filename with his CRC32 Sometimes with modify a file or reencode a video and we need add the new CRC32 to these file. --- README.md | 1 + python_crc32_hasher.py | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) mode change 100644 => 100755 python_crc32_hasher.py diff --git a/README.md b/README.md index d6fa569..c3f0b0d 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ Input can be individual files, and/or folders. Use * (any string), ? (one charac Options: - `--addcrc`: Adds CRC to filenames + - `--updatecrc`: Updates CRC to filenames - `-c out.sfv` or `--createsfv out.sfv`: Creates a SFV file - `-r` or `--recursive`: Also includes sub-folder - `-s` or --searchsubfolder : Also search sub-folder for matching filenames diff --git a/python_crc32_hasher.py b/python_crc32_hasher.py old mode 100644 new mode 100755 index b8ebb69..b44ff22 --- a/python_crc32_hasher.py +++ b/python_crc32_hasher.py @@ -23,10 +23,12 @@ import sys, os, zlib, hashlib, shutil, re, time, struct, multiprocessing, math programName = "Python CRC-32 Hasher" -version = "1.9" +version = "1.10" author = "dreamer2908" +contributors = ["felipem775"] addcrc = False +updatecrc = False force = False recursive = False searchSubFolder = False @@ -221,7 +223,7 @@ def processFile(fileName, fromFolder = False): elif sHash in fileName.upper(): result = "File OK!" st_ok += 1 - elif found: + elif found and not updatecrc: result = "File not OK! %s found in filename." % crc st_notok += 1 else: @@ -234,6 +236,15 @@ def processFile(fileName, fromFolder = False): except: result = "Renaming failed!" newName = fileName + elif updatecrc: + namae, ext = os.path.splitext(fileName) + newName = namae.replace(crc,sHash) + ext + try: + shutil.move(fileName, newName) + result = "CRC updated!" + except Exception as e: + result = "Renaming failed!" + newName = fileName else: result = "CRC not found!" st_notfound += 1 @@ -455,7 +466,7 @@ def removeNonAscii(original): # Parse paramenters def parseParams(): - global pathList, addcrc, createsfv, sfvPath, force, recursive, searchSubFolder, showChecksumResult, showFileInfo, showFullPath + global pathList, addcrc, updatecrc, createsfv, sfvPath, force, recursive, searchSubFolder, showChecksumResult, showFileInfo, showFullPath global enableMd5, enableSha1, enableSha256, enableSha512, enableEd2k, enableCrc, enableAll, enableMd4 global debug, waitBeforeExit @@ -469,6 +480,8 @@ def parseParams(): if arg == "addcrc": addcrc = True + elif arg == "updatecrc": + updatecrc = True elif arg == "createsfv" and i < len(sys.argv) - 1: createsfv = True sfvPath = sys.argv[i+1] @@ -606,6 +619,7 @@ def printReadme(): print(" Use Unix shell-style wildcard (*, ?) for the filename pattern.\n") print("Options:") print(" --addcrc Add CRC-32 to filenames.") + print(" --updatecrc Update CRC-32 to filenames.") print(" -c | --createsfv out.sfv Create a SFV file.") print(" -r | --recursive Also include sub-folder.") print(" -s | --searchsubfolder Also search sub-folder for matching filenames.") @@ -701,4 +715,4 @@ def doStuff(): initStuff() parseParams() checkSanity() -doStuff() \ No newline at end of file +doStuff()