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
Removing a character from the file name in a folder
Moderator: DosItHelp
Re: Removing a character from the file name in a folder
This should work:
Regards
aGerman
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
Re: Removing a character from the file name in a folder
Thanks for the post. Can u please let me know how to use this code. Please inform me the procedure also pls
Re: Removing a character from the file name in a folder
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 /?
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.
Re: Removing a character from the file name in a folder
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.
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.
Re: Removing a character from the file name in a folder
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.
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
)