Bulk renaming of files based on lookup

前端 未结 9 464
忘掉有多难
忘掉有多难 2020-12-11 05:55

I have a folder full of image files such as

  • 1500000704_full.jpg
  • 1500000705_full.jpg
  • 1500000711_full.jpg
  • 1500000712_full.jpg
相关标签:
9条回答
  • 2020-12-11 06:28

    Here's a fun little hack:

    paste -d " " lookupfile.txt lookupfile.txt | cut -d " " -f 2,3 | sed "s/\([ ]\|$\)/_full.jpg /g;s/^/mv /" | sh
    
    0 讨论(0)
  • 2020-12-11 06:32

    Here's a simple Python 2 script to do the rename.

    #!/usr/bin/env python
    
    import os
    
    # A dict with keys being the old filenames and values being the new filenames
    mapping = {}
    
    # Read through the mapping file line-by-line and populate 'mapping'
    with open('mapping.txt') as mapping_file:
        for line in mapping_file:
            # Split the line along whitespace
            # Note: this fails if your filenames have whitespace
            new_name, old_name = line.split()
            mapping[old_name] = new_name
    
    suffix = '_full'
    
    # List the files in the current directory
    for filename in os.listdir('.'):
        root, extension = os.path.splitext(filename)
        if not root.endswith(suffix):
            # File doesn't end with this suffix; ignore it
            continue
        # Strip off the number of characters that make up suffix
        stripped_root = root[:-len(suffix)]
        if stripped_root in mapping:
            os.rename(filename, ''.join(mapping[stripped_root] + suffix + extension))
    

    Various bits of the script are hard-coded that really shouldn't be. These include the name of the mapping file (mapping.txt) and the filename suffix (_full). These could presumably be passed in as arguments and interpreted using sys.argv.

    0 讨论(0)
  • 2020-12-11 06:38
    #!/bin/bash
    
    for FILE in *.jpg; do
        OLD=${FILE%.*}  # Strip off extension.
        NEW=$(awk -v "OLD=$OLD" '$2==OLD {print $1}' map.txt)
        mv "$OLD.jpg" "$NEW.jpg"
    done
    
    0 讨论(0)
  • 2020-12-11 06:38

    Read in the text file, create a hash with the current file name, so files['1500000704'] = 'SH103239' and so on. Then go through the files in the current directory, grab the new filename from the hash, and rename it.

    0 讨论(0)
  • 2020-12-11 06:44
    import os,re,sys
    
    mapping = <Insert your mapping here> #Dictionary Key value entries (Lookup)
    
    for k,v in mapping:
        for f in os.listdir("."):
            if re.match('1500',f): #Executes code on specific files
                os.rename(f,f.replace(k,v))
    
    0 讨论(0)
  • 2020-12-11 06:45

    This will work for your problem:

    #!/usr/bin/perl
    while (<DATA>) {
        my($new, $old) = split;
        rename("$old.jpg", "$new.jpg")
            || die "can't rename "$old.jpg", "$new.jpg": $!";
    }
    __END__
    SH103239 1500000704
    SH103240 1500000705
    SH103241 1500000711
    SH103242 1500000712
    SH103243 1500000714
    SH103244 1500000744
    SH103245 1500000745
    SH103252 1500000802
    SH103253 1500000803
    SH103254 1500000804
    

    Switch to ARGV from DATA to read the lines from a particular input file.

    Normally for mass rename operations, I use something more like this:

    #!/usr/bin/perl
    # rename script by Larry Wall
    #
    # eg:
    #      rename 's/\.orig$//'  *.orig
    #      rename 'y/A-Z/a-z/ unless /^Make/'  *
    #      rename '$_ .= ".bad"'  *.f
    #      rename 'print "$_: "; s/foo/bar/ if <STDIN> =~ /^y/i'  *
    #      find /tmp -name '*~' -print | rename 's/^(.+)~$/.#$1/'
    
    ($op = shift) || die "Usage: rename expr [files]\n";
    
    chomp(@ARGV = <STDIN>) unless @ARGV;
    
    for (@ARGV) {
        $was = $_;
        eval $op;
        die if $@;  # means eval `failed'
        rename($was,$_) unless $was eq $_;
    }
    

    I’ve a more full-featured version, but that should suffice.

    0 讨论(0)
提交回复
热议问题