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

Author Topic: replace characters in the file name  (Read 6784 times)

0 Members and 1 Guest are viewing this topic.

uavaus

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Unknown
    replace characters in the file name
    « on: September 06, 2012, 09:29:29 PM »
    Hi!

    I created a batch file that loops through a list of files in a folder, strips a part of each one and renames it (see example below)

    Batch file:
    for %%i in (*.pdf) do (set fName=%%i)
    ren %fName% %fName:~-10%

    Original list:
    John Smith_12_123456.pdf
    Brian  Smith_10_456789.pdf
    Joshua  Smith_17_765436.pdf
    Kate  Noname_09_786526.pdf

    Output:
    123456.pdf
    456789.pdf
    765436.pdf
    786526.pdf

    However, if there is a file with brackets in the folder (ex.: John_(Paul)_Smith_12_123456.pdf , the batch file does not work and throws an error message. How can I fix it?

    Is there a way to replace brackets for a different character (underscore would be ideal)?

     Thank you.

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: replace characters in the file name
    « Reply #1 on: September 06, 2012, 11:17:52 PM »
    This should work (untested):

    Code: [Select]
    @echo off
    setlocal EnableDelayedExpansion
    for /f "delims=" %%i in (' dir *.pdf /b ') do (
    set "fName=%%i"
    ren "!fName!" "!fName:~-10!"
    )

    uavaus

      Topic Starter


      Newbie

      • Experience: Beginner
      • OS: Unknown
      Re: replace characters in the file name
      « Reply #2 on: September 07, 2012, 04:40:52 AM »
      This should work (untested):

      Code: [Select]
      @echo off
      setlocal EnableDelayedExpansion
      for /f "delims=" %%i in (' dir *.pdf /b ') do (
      set "fName=%%i"
      ren "!fName!" "!fName:~-10!"
      )

      Thank you so much mate! This script does exactly what I wanted it to do.