1.

Solve : Remove Text From File Name?

Answer»

I'm stumped on how to strip text from a file name and am hoping someone can help me here.

I need to be able to remove "_face01" (minus the quotation marks) from multiple files in a folder. The batch script can't kept in the DIRECTORY with the files, it will need to reference it. I've EXPERIMENTED with a few For loops and am not getting far.

This was my last effort:

@echo off
setlocal enabledelayedexpansion
set deletestring=_face01
for /f "delims==" %%F in (' "\\folder-path" " *.bmp ^| find "%deletestring%"' ) do (
set oldfilename=%%F
set newfilename=!oldfilename:%deletestring%=!
Ren "!oldfilename!" "!newfilename!"
)

I was honestly hoping for something simpler:

for /f "tokens=*" %%F in ( '"path-to-folder" *.bmp' ) do ren "%%F" "%%F:~0,-11%"

Neither of these seem to work.

Thanks!

MJ
I tried to do this a while back for some files and just bought Better File Rename INSTEAD which can CLIP to remove unwanted text from file names, or rename in whatever way you chose just about. http://www.publicspace.net/windows/BetterFileRename/This seems to work as long as the first letter of the file isn't '1'. Still working on this, so don't run it on your files quite yet.

Code: [Select]:: !a! = full file name
:: !b! = extension
:: %%G = Before _file01
@echo off
setlocal EnableDelayedExpansion


for /f "delims=" %%A in ('dir /b ^| findstr "_face"') do (
set a=
set b=
set a=%%A
for /f "tokens=2 delims=." %%B in ("%%A") do (
set b=%%B
)
for /f "tokens=1 delims=_file" %%G in ("%%A") do (
rename "!a!" %%G.!b!
echo %%A %%G.!b!
)
)
pause

commented out rename command so it will only list files, and what it would rename them.

(practiced on .txt files)

EDIT: FIXED the file, seems to work quite well.
Keep in mind that this clips everything after the "_face". Oh, and you can't have a period (.) anywhere in any of the filenames, or they will be messed up.
before:
Code: [Select]04/17/2013 07:39 PM 6 10_face01.txt
04/17/2013 07:39 PM 6 11_face01.txt
04/17/2013 07:39 PM 6 12_face01.txt
04/17/2013 07:39 PM 6 13_face01.txt
04/17/2013 07:39 PM 6 14_face01.txt
04/17/2013 07:39 PM 6 15_face01.txt
04/17/2013 07:39 PM 6 16_face01.txt
04/17/2013 07:39 PM 6 17_face01.txt
04/17/2013 07:39 PM 6 18_face01.txt
04/17/2013 07:39 PM 6 19_face01.txt
04/17/2013 07:39 PM 6 1_face01.txt
04/17/2013 07:39 PM 6 20_face01.txt
04/17/2013 07:39 PM 6 21_face01.txt
04/17/2013 07:39 PM 6 22_face01.txt
04/17/2013 07:39 PM 6 23_face01.txt
04/17/2013 07:39 PM 6 24_face01.txt
04/17/2013 07:39 PM 6 25_face01.txt
04/17/2013 07:39 PM 6 26_face01.txt
04/17/2013 07:39 PM 6 27_face01.txt
04/17/2013 07:39 PM 6 28_face01.txt
04/17/2013 07:39 PM 6 29_face01.txt
04/17/2013 07:39 PM 6 2_face01.txt
04/17/2013 07:39 PM 6 30_face01.txt
04/17/2013 07:39 PM 6 31_face01.txt
04/17/2013 07:39 PM 6 32_face01.txt
04/17/2013 07:39 PM 6 33_face01.txt
04/17/2013 07:39 PM 6 34_face01.txt
04/17/2013 07:39 PM 6 35_face01.txt
04/17/2013 07:39 PM 6 36_face01.txt
04/17/2013 07:39 PM 6 37_face01.txt
04/17/2013 07:39 PM 6 38_face01.txt
04/17/2013 07:39 PM 6 39_face01.txt
04/17/2013 07:39 PM 6 3_face01.txt
04/17/2013 07:39 PM 6 40_face01.txt
04/17/2013 07:39 PM 6 4_face01.txt
04/17/2013 07:39 PM 6 5_face01.txt
04/17/2013 07:39 PM 6 6_face01.txt
04/17/2013 07:39 PM 6 7_face01.txt
04/17/2013 07:39 PM 6 8_face01.txt
04/17/2013 07:39 PM 6 9_face01.txt
04/17/2013 07:37 PM 384 renam.bat
41 File(s) 624 bytes
2 Dir(s) 259,738,959,872 bytes free

after:
Code: [Select]04/17/2013 07:39 PM 6 1.txt
04/17/2013 07:39 PM 6 10.txt
04/17/2013 07:39 PM 6 11.txt
04/17/2013 07:39 PM 6 12.txt
04/17/2013 07:39 PM 6 13.txt
04/17/2013 07:39 PM 6 14.txt
04/17/2013 07:39 PM 6 15.txt
04/17/2013 07:39 PM 6 16.txt
04/17/2013 07:39 PM 6 17.txt
04/17/2013 07:39 PM 6 18.txt
04/17/2013 07:39 PM 6 19.txt
04/17/2013 07:39 PM 6 2.txt
04/17/2013 07:39 PM 6 20.txt
04/17/2013 07:39 PM 6 21.txt
04/17/2013 07:39 PM 6 22.txt
04/17/2013 07:39 PM 6 23.txt
04/17/2013 07:39 PM 6 24.txt
04/17/2013 07:39 PM 6 25.txt
04/17/2013 07:39 PM 6 26.txt
04/17/2013 07:39 PM 6 27.txt
04/17/2013 07:39 PM 6 28.txt
04/17/2013 07:39 PM 6 29.txt
04/17/2013 07:39 PM 6 3.txt
04/17/2013 07:39 PM 6 30.txt
04/17/2013 07:39 PM 6 31.txt
04/17/2013 07:39 PM 6 32.txt
04/17/2013 07:39 PM 6 33.txt
04/17/2013 07:39 PM 6 34.txt
04/17/2013 07:39 PM 6 35.txt
04/17/2013 07:39 PM 6 36.txt
04/17/2013 07:39 PM 6 37.txt
04/17/2013 07:39 PM 6 38.txt
04/17/2013 07:39 PM 6 39.txt
04/17/2013 07:39 PM 6 4.txt
04/17/2013 07:39 PM 6 40.txt
04/17/2013 07:39 PM 6 5.txt
04/17/2013 07:39 PM 6 6.txt
04/17/2013 07:39 PM 6 7.txt
04/17/2013 07:39 PM 6 8.txt
04/17/2013 07:39 PM 6 9.txt
04/17/2013 07:37 PM 384 renam.bat
41 File(s) 624 bytes
2 Dir(s) 259,738,959,872 bytes free

This should work. Replace c:\backup\ with your folder.

Code: [Select]@echo off
pushd "c:\backup\"
for /f "delims=" %%a in ('dir *.* /b /a-d') do call :next "%%a"
popd
goto :EOF
:next
set "name=%~1"
set "name=%name:_face01=%"
ren %1 "%name%"
Thanks guys! I used the code from Foxdrive and it worked like a charm. Much appreciated!

MJ



Discussion

No Comment Found