2

Heythere. I need your guys help with file renaming using a .bat file. I got multiple files that I need to be cropped after specified character number. For exmample, I want to crop name of the files after their fifth character, this way

filename.exe > filen.exe
anothername.exe > anoth.exe
absolutelydifferentname.exe > absol.exe

And, if possible, I'd like to know how to do the opposite. I mean, leaving the certain # of characters at the end, cropping from the beginning of the filename.

Thank you.

get8p
  • 709
  • 2
  • 8
  • 12
  • 2
    What happens if you have `filename.exe` and `filename2.exe`? You can't have two of `filen.exe`.. – Blorgbeard Jun 18 '12 at 03:51
  • 1
    http://stackoverflow.com/questions/636381/what-is-the-best-way-to-do-a-substring-in-a-batch-file might be of use, since you need to find substrings. Blorgbeard's comment is something you need to consider how to handle though. – Shoaib Jun 18 '12 at 03:58

3 Answers3

4

If you want to do it in a batch file, the following should work:

@echo off

setlocal ENABLEDELAYEDEXPANSION

for %%i in (<Directory name here>\*) do (
  set filename=%%~ni
  ren "%%~i" "!filename:~0,5!%%~xi"
)

endlocal

If you wish to change the number of characters used to construct the final filenames, change the "5" in ren "%%~i" "!filename:~0,5!%%~xi".

To take the last 5 characters try: ren "%%~i" "!filename:~-5!%%~xi"

For all except the first 5 characters: ren "%%~i" "!filename:~5!%%~xi"

Iridium
  • 23,323
  • 6
  • 52
  • 74
  • Thanks a bunch, @Iridium. Edited this way `setlocal EnableDelayedExpansion for %%i in (*.exe) do ( set filename=%%~ni ren "%%~i" "!filename:~0,5!%%~xi" )` it seems to be doing good. I'm also wondering if you know the way to make it trim characters from the beginning instead of the end? – get8p Jun 18 '12 at 05:05
  • I've edited my answer to include your suggestion (knew there must be a way, but didnt' check setlocal). I have also added the way to take the last X characters – Iridium Jun 18 '12 at 05:06
  • Yep, that does it. Thanks mate, you're the best! – get8p Jun 18 '12 at 05:09
  • You don't need a final `endlocal`, though; it's implied by the end of the batch. – Joey Jun 18 '12 at 05:17
  • This solution works fine as long as no file name contains `!` character. I posted a [modified version](http://stackoverflow.com/a/11081678/1012053) that removes that limitation. – dbenham Jun 18 '12 at 11:18
2

The Iridium solution will fail if any file name contains the ! character because expansion of %%i will fail if value contains ! and delayed expansion is enabled. This is easily fixed by toggling delayed expansion on and off within the loop.

The explicit disabling of delayed expansion at the top is normally not needed. But it is possible for delayed expansion to already be enabled at the start of a batch file, so I included it just to be safe.

@echo off
setlocal disableDelayedExpansion
for %%F in ("<Directory name here>\*") do (
  set "full=%%F"
  set "name=%%~nF"
  set "ext=%%~xF"
  setlocal enableDelayedExpansion
  ren "!full!" "!name:~0,5!!ext!"
  endlocal
)
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • +1, And if you use the extened `SET` syntax it would be also safe against invisible spaces :-) – jeb Jun 18 '12 at 11:48
0

Just incase this is a once-off, or some other manual scenario, have a look at CKRename

Michael Rodrigues
  • 5,057
  • 3
  • 25
  • 51