Scripting in Python for Windows

Having a private project, which required the handling of thousands of photo/image files, I was stuck with 2 problems:

  • many files where improperly named, that resulted in wrong file sorting;
  • batch renaming of several files under Windows is very complex and sometimes gives unexpected results
After long fight with MS DOS BAT file scripting (with rename command and weird wildcards rules), I decided to use Python scripting to implement the functionality I needed. The results are mostly in github, and README.md explains lot of things.

So I have batch script directory C:\SRV, included in PATH, with several very simple BAT files, like:

set PYTHONPATH=C:\Prjs\nanolib;%PYTHONPATH%
py -m nano_file_utils.filename_processing %*
which calls Python script in C:\Prjs\nanolib\nano_file_utils\filename_processing.py

For Python specific details go to github, here are just top level explanation of 3 commands I created and used for my purposes.


NF4PO - Numberize Filenames 4or Proper Ordering

Has no arguments, processes files in current directory. Detects common pattern of several file names, with numeric field, updates this numeric field by left padding with zeros, so all file names has equal length and proper ordering. Example:
Initial file names and sortingAfter running NF4PO
abc1.ext
abc10.ext
abc11.ext
abc12.ext
abc2.ext
abc3.ext
abc4.ext
abc5.ext
abc6.ext
abc7.ext
abc8.ext
abc9.ext
abc01.ext
abc02.ext
abc03.ext
abc04.ext
abc05.ext
abc06.ext
abc07.ext
abc08.ext
abc09.ext
abc10.ext
abc11.ext
abc12.ext


renamer - goes through the files in a directory and renames files according to the Python written logic

This command requires Python programming. It has single argument - extension of files to be renamed, w/o dot. How to use it:
  • cd to required directory
  • edit file ...\nanolib\scripts\renamer.py, function new_name() that generates new filename from old filename
  • run RENAMER.BAT ext
Example of function new_name() - to remove leading 6 characters of filenames:
def new_name(old):
  file_base_name, file_extension = os.path.splitext(old)
#  prefix = file_base_name[:1]
  prefix = file_base_name[6:]
  return prefix + file_extension
This command renames only one type of files during one run, for example, RENAMER JPG renames only JPG files.

ren-lambda - goes through the files in a directory, filter and renames files according to the given Python lambdas

The script works in the current directory, it accepts 2 command line arguments:
  • 1st argument - filter-expression - lambda expression to select files that should be renamed
  • 2nd argument - new-name-expression - lambda expression that generates new file name from old filename's base and ext
Both command line arguments are expressions in Python syntax, that take 2 lambda arguments:
  • base - base name of the filename
  • ext - extension of filename without . (dot)
filter-expression returns boolean true if the file should be renamed.
new-name-expression returns string as new name, constructed from 'base' and 'ext'

YES, there is some mess with the term 'argument'. MS DOS Command REN-LAMBDA.BAT accepts 2 command line arguments, each of this argument is lambda expression, which takes 2 lambda arguments.

Actually the reality is even more complicated, because both lambda expressions get not 2 but 6 arguments: base, ext, Base, Ext, BASE, EXT which correspond to lower case, original and upper case variations of filename.

Example: For all files with extension .png or .PNG or .pNg set the extension to .JPG
ren-lambda "ext == '.png'" "Base + '.JPG'"
Hollow filter and action:
ren-lambda "1==2" "Base + Ext"
Example of useful filter:
ren-lambda "Base.startswith('IMG')" "Base + Ext"




Home  
Terms and Conditions (c) 2005, 2006, 2008, 2009, 2011, 2012, ..., 2023 NAN