locked
Batch File Rename RRS feed

  • Question

  • @echo off

    if not [%3]==[] (
        goto usage
    )


    if [%2]==[] (
        goto usage
    )

    if [%1]==[] (
        goto usage
    )

    CALL SET arg=%2%
    CALL SET ext=%%arg:~1%%

    if %arg%==* (
    forfiles /S /M %1 /C "cmd /c rename @file @fname
    ) else (
    forfiles /S /M %1 /C "cmd /c rename @file @fname%ext%"
    )

    if %ERRORLEVEL%==0 (
     echo Successfully renamed the file extensions.
      )
    goto :eof

    :usage
    echo Usage:
    echo TO rename file extension: batchFileRename.bat *.ext1 *.ext2"
    echo To strip file extension: bathFileRename.bat *.ext *

    HI, I WANT TO RENAME SOME FILE EXTENSIONS FROM .PDF_ TO .PDF, THE SCRIPT UP THERE IS THE TEMPLATE, WHERE DO I ADD THE .PDF_ AND .PDF? I'M A DUMMY, YEAH.

    Wednesday, July 29, 2020 2:18 PM

Answers

  • Don't use a batch file. Use PowerShell. Example:


    Get-ChildItem *.pdf_ | ForEach-Object {
      Rename-Item $_ ("{0}.pdf" -f [IO.Path]::GetFileNameWithoutExtension($_)) -WhatIf
    }
    

    Remove -WhatIf if the command does what you want.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Vector BCO Wednesday, July 29, 2020 9:53 PM
    • Marked as answer by Kwakugaara Thursday, July 30, 2020 7:38 PM
    Wednesday, July 29, 2020 2:29 PM

All replies

  •  

    HI, I WANT TO RENAME SOME FILE EXTENSIONS FROM .PDF_ TO .PDF, THE SCRIPT UP THERE IS THE TEMPLATE, WHERE DO I ADD THE .PDF_ AND .PDF? I'M A DUMMY, YEAH.

    i have a few questions: what you need to do?

    why you need this script for that?

    what is your problem with this script? did you try to read examples provided in "usage" block?


    The opinion expressed by me is not an official position of Microsoft

    • Edited by Vector BCO Wednesday, July 29, 2020 2:30 PM
    Wednesday, July 29, 2020 2:28 PM
  • Don't use a batch file. Use PowerShell. Example:


    Get-ChildItem *.pdf_ | ForEach-Object {
      Rename-Item $_ ("{0}.pdf" -f [IO.Path]::GetFileNameWithoutExtension($_)) -WhatIf
    }
    

    Remove -WhatIf if the command does what you want.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Vector BCO Wednesday, July 29, 2020 9:53 PM
    • Marked as answer by Kwakugaara Thursday, July 30, 2020 7:38 PM
    Wednesday, July 29, 2020 2:29 PM
  • I want to use the script to rename about a hundred files in a folder. From .pdf_ to.pdf
    Wednesday, July 29, 2020 2:34 PM
  • Yeah I've read the examples but can't figure out where to input them.
    Wednesday, July 29, 2020 2:35 PM
  • Okay. But please can I save the script into a file and run it with a few mouse clicks? And how do I do that?

    Wednesday, July 29, 2020 2:36 PM
  • Don't use a batch file. Use PowerShell. Example:


    Get-ChildItem *.pdf_ | ForEach-Object {
      Rename-Item $_ ("{0}.pdf" -f [IO.Path]::GetFileNameWithoutExtension($_)) -WhatIf
    }
    

    Remove -WhatIf if the command does what you want.


    -- Bill Stewart [Bill_Stewart]

    Okay. But please can I save the script into a file and run it with a few mouse clicks? And how do I do that?
    Wednesday, July 29, 2020 2:45 PM
  •  

    HI, I WANT TO RENAME SOME FILE EXTENSIONS FROM .PDF_ TO .PDF, THE SCRIPT UP THERE IS THE TEMPLATE, WHERE DO I ADD THE .PDF_ AND .PDF? I'M A DUMMY, YEAH.

    i have a few questions: what you need to do?

    why you need this script for that?

    what is your problem with this script? did you try to read examples provided in "usage" block?


    The opinion expressed by me is not an official position of Microsoft

    I want to use the script to rename about a hundred files in a folder. From .pdf_ to .pdf

    Wednesday, July 29, 2020 2:46 PM
  • did you saw asterisks * in the examples?

    looks like correct command should be batfilenam.bat *.pdf_ *.pdf, did you try that?

    i agreed with Bill, that new scripts should be written in powershe not batch files, but if you need to to this operation once you can simply copy/paste Bill sample into the console and get a result. 


    The opinion expressed by me is not an official position of Microsoft

    Wednesday, July 29, 2020 3:13 PM
  • I would also add: If you are not familiar with the command line and scripting, I think a better option is to find a program that can make the change for you.

    Sample search: bulk rename utility

    Of necessity, this forum can't perform basic step-by-step hand-holding of how to run a script from a command line. The following post is at the top of this forum:

    This forum is for scripting questions rather than script requests


    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 29, 2020 5:28 PM
  • I would also add: If you are not familiar with the command line and scripting, I think a better option is to find a program that can make the change for you.

    Sample search: bulk rename utility

    Of necessity, this forum can't perform basic step-by-step hand-holding of how to run a script from a command line. The following post is at the top of this forum:

    This forum is for scripting questions rather than script requests


    -- Bill Stewart [Bill_Stewart]

    Thanks, your first script worked. Sorry, I'm new to all this. Just needed a script to rename the files. Thanks for your help.

    Wednesday, July 29, 2020 8:54 PM
  • Kwakugaara, if you got expected results please mark comment as an answer which help you do that

    The opinion expressed by me is not an official position of Microsoft

    Wednesday, July 29, 2020 9:56 PM