Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Remove parts of a filename in all folders and subfolders using .bat  (Read 4036 times)

0 Members and 1 Guest are viewing this topic.

SamiSamami

    Topic Starter


    Newbie

    • Experience: Familiar
    • OS: Windows 7
    I've been searching for a couple of hours for a way to remove parts of a filename with batch script and I got to this old thread:

    http://www.computerhope.com/forum/index.php?topic=74465.0

    When I use the following script everything works fine in the same folder:

    Code: [Select]
    @echo off
    setlocal enabledelayedexpansion
    set deletestring=ExampleName

    for /f "delims==" %%F in ('dir /b  ^| find "%deletestring%"') do (
    set oldfilename=%%F
    set newfilename=!oldfilename:%deletestring%=!
    Ren "!oldfilename!" "!newfilename!"
    )

    However I want it to search through all folders and sub-folders and remove that ExampleName. it's not possible with that script. I'm pretty sure that it has something to do with the dir /b ... I tried messing with the codes but since I'm not a coder I have no idea how to solve it.

    Any help would be appreciated

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: Remove parts of a filename in all folders and subfolders using .bat
    « Reply #1 on: February 11, 2017, 11:54:59 PM »
    Did you happen to read the help for the DIR command.
    Code: [Select]
    /S          Displays files in specified directory and all subdirectories.
    Also you do not need to pipe to the FIND command.  You can use your delete string with the DIR command. And, if you are going to use the DIR command I would highly suggest you use the /A-D option. If you pipe to the FIND command you could in theory find your search string in a directory name regardless of using the /A-D option.

    But the easiest way to do this would be to use the FOR /R command.
    Code: [Select]
    FOR /R %%F in (*%deletestring%*) DO ......