Removing a character from the file name in a folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pillaisg
Posts: 4
Joined: 15 Nov 2015 08:44

Removing a character from the file name in a folder

#1 Post by pillaisg » 15 Nov 2015 08:49

Hi
I want to rename some files (Nearly 1000). I want to avoid the "-" from the files. The format of the files are in the below mentioned format "A-10-D110-J-9203_001". I want to convert this files to a "A10D110J9203_001".
Can anyone help pls

aGerman
Expert
Posts: 4189
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Removing a character from the file name in a folder

#2 Post by aGerman » 15 Nov 2015 09:33

This should work:

Code: Select all

@echo off &setlocal DisableDelayedExpansion
for /f "delims=" %%i in ('dir /a-d /b') do (
  set "file=%%i"
  setlocal EnableDelayedExpansion
  ren "!file!" "!file:-=!"
  endlocal
)

Regards
aGerman

pillaisg
Posts: 4
Joined: 15 Nov 2015 08:44

Re: Removing a character from the file name in a folder

#3 Post by pillaisg » 15 Nov 2015 10:22

Thanks for the post. Can u please let me know how to use this code. Please inform me the procedure also pls

aGerman
Expert
Posts: 4189
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Removing a character from the file name in a folder

#4 Post by aGerman » 15 Nov 2015 11:07

All you have to do is to save the batch file in the folder where your files are and to run it.

It replaces the - character with nothing in the variable "file" that was assigned from the output of DIR.

set /?
Environment variable substitution has been enhanced as follows:

%PATH:str1=str2%

would expand the PATH environment variable, substituting each occurrence of "str1" in the expanded result with "str2". "str2" can be the empty string to effectively delete all occurrences of "str1" from the expanded output. "str1" can begin with an asterisk, in which case it will match everything from the beginning of the expanded output to the first occurrence of the remaining portion of str1.

pillaisg
Posts: 4
Joined: 15 Nov 2015 08:44

Re: Removing a character from the file name in a folder

#5 Post by pillaisg » 15 Nov 2015 22:22

Thanks for the post... It worked fine in 10 min everything converted to the format I wanted.
I wish to know how should the code be changed for getting the following result.
"A-10-D110-J-9203-1". I want to convert this files to a "A10D110J9203_001".
As mentioned above after the last"-" there shall be three characters. and the last "-" shall be changed to "_" (underscore).
How should this be modified.
Which programing language has to be studied for writing this code.

aGerman
Expert
Posts: 4189
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Removing a character from the file name in a folder

#6 Post by aGerman » 16 Nov 2015 12:05

Batch should be sufficient to meet your needs.
The following code will only work if every file name can be devided (delimited by minus signs) into 6 parts as your example shows.

Code: Select all

@echo off &setlocal DisableDelayedExpansion
for /f "delims=" %%i in ('dir /a-d /b^|findstr /rb "[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*\.*[^-]*$"') do (
  set "name=%%~ni"
  set "ext=%%~xi"
  for /f "tokens=1-6 delims=-" %%j in ("%%~ni") do (set "p1=%%j%%k%%l%%m%%n" &set "p2=000%%o")
  setlocal EnableDelayedExpansion
  ren "!name!!ext!" "!p1!_!p2:~-3!!ext!"
  endlocal
)

Post Reply