How do I customize this script to recursively (bulk) rename file extensions of one type to another

Discussion in 'Scripting' started by anansay, Nov 17, 2020.

  1. anansay

    anansay MDL Junior Member

    Jul 13, 2020
    91
    7
    0
    I happened to chance upon Rename file extensions in bulk from command line [CMD] in which the author contributed the script below:

    Code:
    forfiles /S /M *.ext1 /C "cmd /c rename @file @fname.ext2"
    He did not provide much explanation and that is why I post my questions here. They are:

    1. What are the functions of the switches /S /M ?

    2. There is a /C in the command. Does it mean the whole of C:\ drive?

    3. What does /c do?

    My objective is very simple.

    In C:\Windows\System32\DriverStore\FileRepository there is a folder called abcde12345 with at least 5 sub-folders.

    After taking ownership of abcde12345 folder, I wish to rename in bulk in the sub-directories all the files of extensions cat, dll, exe, inf, pnf, sys to .cat.bak .dll.bak .exe.bak, .inf.bak, .pnf.bak, .sys.bak

    I will cd to \Windows\System32\DriverStore\FileRepository\abcde12345 and run my customized script (see below). Please correct it if it is wrong:

    Code:
    forfiles /S /M *.* "cmd /c rename @file @fname.*.bak"
    I appreciate your help in this.
     
  2. Carlos Detweiller

    Carlos Detweiller MDL Spinning Tortoise

    Dec 21, 2012
    4,184
    4,056
    150
    Official help forfiles /? (util is included in Windows)
    Code:
        /M    searchmask    Searches files according to a searchmask.
                            The default searchmask is '*' .
    
        /S                  Instructs forfiles to recurse into
                            subdirectories. Like "DIR /S".
    Again, forfiles /?
    Code:
        /C    command       Indicates the command to execute for each file.
                            Command strings should be wrapped in double
                            quotes.
    With the /C switch, you instruct cmd.exe to run a command/program and then return. Required here as REName is an internal cmd.exe command.
    Code:
    /C      Carries out the command specified by string and then terminates

    Code:
    forfiles /S /M *.* /C "cmd /c rename @file @fname.*.bak"
     
  3. anansay

    anansay MDL Junior Member

    Jul 13, 2020
    91
    7
    0
    I didn't know that forfiles is a Microsoft Windows utility. Thanks for pointing it out to me.

    According to Microsoft Docs' on forfiles, there are only five switches, that is to say, there is no difference between between /C and /c.

    Code:
    forfiles /S /M *.* /C "cmd /c rename @file @fname.*.bak"
    The above command could have been writtten as:

    Code:
    forfiles /S /M *.* /C "cmd /C rename @file @fname.*.bak"
     
  4. Carlos Detweiller

    Carlos Detweiller MDL Spinning Tortoise

    Dec 21, 2012
    4,184
    4,056
    150
    MS' tools and tools natively coded for Windows are usually not case sensitive. Please note that other tools with UNIX or Linux heritage differ in this regard, case is important there.