Windows batch file: rename files (possibly in multiple folders) based on input file (of target filenames)

01
2014-07
  • cMP

    I am a Batch-newbie...

    This "tool" is to automate the slimming down of Windows (XP) by disabling certain system driver, DLL and EXE files. Instead of outright deletion, I wish to rename-in-place, thus "removing" them from the OS, but not losing sight of where they belong (should any need to be "restored"). Renaming is accomplished by appending a new suffix to the existing filename (eg: "wdmaud.drv.group_1") The renaming suffix should be another input variable.

    The target-list is approx. 1100 files long (divided into various groups/phases), so manual renaming is out of the question. Each group will be processed in a separate run of the batch file, varying the target-list input file for each execution.

    Target-list is plain text file, one filename per line (no other data in the files). Number of entries per group varies. Target list will look like this:

        -- example start --
        netapi.dll
        netcfgx.dll 
        netdde.exe 
        netevent.dll 
        nic1394.sys
        -- example end --
    

    Filenames may be in UPPER, lower, or MiXeD case. The files may be present in more than one folder in the C:\Windows hierarchy - or may not be present at all. If a file is not found anywhere in the system, it's name should be written to a text file, one-entry-per-line.

    The specific folders of interest are:

        C:\WINDOWS\
        C:\WINDOWS\system\
        C:\WINDOWS\system32\
        C:\WINDOWS\system32\dllcache
        C:\WINDOWS\system32\drivers
    

    ...but may change as development proceeds.

    Based on a reply at stackoverflow.com, I've got started thus:

        @echo off
    
        set suffix=GROUP_1
        set targetlist=GROUP_1.txt
        set dirlist=folders.txt
    
        for /f "tokens=*" %%f in (%targetlist%) do (
          for /f "tokens=*" %%d in (%dirlist%) do (
            if exist "%%d\%%f" ren "%%d\%%f.%%suffix"
               echo %%f found in %%d >> foundlist.txt
          )
        )
    

    ==============================================================================

        :: -----------------------------------------------------------------::
        :: Batch Process to Rename-In-Place System Files from an Input List ::
        :: -----------------------------------------------------------------::
    
        @echo off
        :: >> clear files from previous run <<
        if exist RENAMED_files.txt  DEL RENAMED_files.txt
        if exist NOTFound_files.txt DEL NOTFound_files.txt
    
        ::  >> file rename-suffix reflects step name <<
        set suffix=Steppe_01
    
        ::  >> target file list to rename <<
        set targetlist=Steppe_01_files.txt
    
        ::  >> list of folders to search  <<
        set dirlist=folders.txt
    
        ::  >> PROCESS <<
    
        for /f "tokens=*" %%f in (%targetlist%) do (
          echo. >> NOTFound_files.txt
          for /f "tokens=*" %%d in (%dirlist%) do (
            if NOT exist "%%d\%%f"          echo %%f  not in %%d >> NOTFound_files.txt
            if exist     "%%d\%%f"          REN "%%d\%%f" "%%f.%suffix%"
            if exist     "%%d\%%f.%suffix%" echo renamed  %%f  in %%d >> RENAMED_files.txt
          )
        )
    
        ::  >> end of process <<
    
  • Answers
    Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

    Related Question

    bash - Batch rename files that do not match pattern
  • Atul Kakrana

    I do not have programming experience and mostly use one-liner or sometimes more to get the job done. I am having problem to batch rename the files that do not match a particular pattern.

    Sample file-names from directory:

    Meeting_Packages.pdf
    13_textfile0
    19_textfile0
    23_textfile0
    29_textfile0
    33_textfile1
    45_textfile0
    5_textfile3
    Membership.pdf
    13_textfile1
    19_textfile1
    23_textfile1
    29_textfile1
    34_textfile0
    46_textfile0
    6_textfile0
    xyz2009.pdf
    13_textfile2
    19_textfile2
    23_textfile2
    29_textfile2
    34_textfile1
    47_textfile0
    6_textfile1
    meeting.ics
    

    I want to rename the files e.g. Meeting_Packages.pdf, Membership.pdf, meeting.ics and xyz2009.pdf to the file from where they came (input file). Actually it is output of ripmime with mails as xx_textfilex and others are attachments. I want to name attachments as the original input file

    My Code:

    #!/bin/bash
    
    FILES=*.mime
    for f in $FILES
    do
        echo "Processing $f"
        #rip mails into attachments and text files also add a prefix to text files
        ripmime -i $f -d ~/test/ripmime --prefix 
        #Remove white spaces from files 
        rename 's/ /_/g' ~/test/ripmime/*
        #rename attachments as original input files
        rename 's/\[^0-9]/'$f/ ~/test/ripmime/* 
    done
    

    My problem is the last rename line where I try to filter files other than xx_textfilex and rename. I tried different regular expressions but could not do that. I can select and rename the textfiles by:

    rename 's/textfiles/'$f/ ~/test/ripmime/*
    

    but I need the inverse of that and rename files other then textfiles.

    How can I do this?


  • Related Answers
  • Simon Sheehan

    I use https://gist.github.com/995151

    rename 's/\.bak$//' *.pdf

  • Darthenius

    You can combine ls and grep -v:

    ls | grep -v ".*textfile.*" | while read filename; do
      # rename $filename to something
    done