Remove text from specific word onwards
Moderator: DosItHelp
Remove text from specific word onwards
Hi, does anyone know of a way to loop through the files in a directory and truncate a filenames from and including a specific word (in the example below it's 1234)? The example below illustrates what I would like to achieve...
original filenames in directory:
c:\some random text 1234 some random text.txt
c:\more random text 1234 more random text.txt
etc...
new filenames in directory:
c:\some random text.txt
c:\more random text.txt
I've been using Bulk Rename Utility to do this but as I need to do this quite often it would be easier to have a batch file to double click rather than launching the program. Any help would be greatly appreciated. Thanks!
cheers,
rrenis.
original filenames in directory:
c:\some random text 1234 some random text.txt
c:\more random text 1234 more random text.txt
etc...
new filenames in directory:
c:\some random text.txt
c:\more random text.txt
I've been using Bulk Rename Utility to do this but as I need to do this quite often it would be easier to have a batch file to double click rather than launching the program. Any help would be greatly appreciated. Thanks!
cheers,
rrenis.
Re: Remove text from specific word onwards
See here: viewtopic.php?f=3&t=6108
The code for this type of task varies considerably depending on the text being renamed and any answer you receive may not work and it may actually destroy the filenames in a very messy way.
The code for this type of task varies considerably depending on the text being renamed and any answer you receive may not work and it may actually destroy the filenames in a very messy way.
Re: Remove text from specific word onwards
Hi foxidrive, thanks for your reply. I may stick to Bulk Rename Utility but just wondered whether it could be acheived in a batch file in DOS. Sorry I used a generic example - just used it as an example. In use the '1234' example would be '720p'. Essentially I'm just trying to tidy up file names by keeping the media name and removing everything else - the common denominator here being '720p'. Thanks.
Cheers, Chris.
Cheers, Chris.
Re: Remove text from specific word onwards
rrenis wrote:Hi foxidrive, thanks for your reply. I may stick to Bulk Rename Utility but just wondered whether it could be acheived in a batch file in DOS. Sorry I used a generic example - just used it as an example. In use the '1234' example would be '720p'. Essentially I'm just trying to tidy up file names by keeping the media name and removing everything else - the common denominator here being '720p'. Thanks.
Cheers, Chris.
Hi Chris,
Please provide some real world examples of your file names and also provide examples of what those file names should be renamed to.
Thanks
Re: Remove text from specific word onwards
Hi Squashman, here's an example I found whilst googleing, posted by 'dbenham' on the Stackoverflow website. It prints the result in the command window and I can see the result is what I'm after although I'm not sure how to alter it in order to have it rename the files instead of printing the result...
Here's a real world example of the filename change required...
Original Filename:
From.Dusk.Till.Dawn.1996.720p.BrRip.x264.YIFY.mp4 (although the text to the right of 720p may not always be consistent)
New Filename:
From Dusk Til Dawn 1996.mp4
Hope this makes more sense. Thanks for your help.
Cheers,
rrenis.
edit: Thanks squashman - will do in future
Code: Select all
@echo off
setlocal
for %%F in (*) do call set "var=%%F" & call :procName
exit /b
:procName
::truncate at the 1st occurance of 720p
set "var=%var:720p="&rem %
::print the result
set var
exit /b
Here's a real world example of the filename change required...
Original Filename:
From.Dusk.Till.Dawn.1996.720p.BrRip.x264.YIFY.mp4 (although the text to the right of 720p may not always be consistent)
New Filename:
From Dusk Til Dawn 1996.mp4
Hope this makes more sense. Thanks for your help.
Cheers,
rrenis.
edit: Thanks squashman - will do in future
Last edited by Squashman on 16 Sep 2016 07:00, edited 1 time in total.
Reason: MOD EDIT: Please use code tags.
Reason: MOD EDIT: Please use code tags.
Re: Remove text from specific word onwards
Give this a try.
Code: Select all
@echo off
setlocal
for %%F in (*.mp4) do call set "var=%%F" & call :procName
exit /b
:procName
set "file=%var%"
::truncate at the 1st occurance of 720p
set "var=%var:720p="&rem %
::rename the file
IF NOT "%file%"=="%var%" rename "%file%" "%var%"
exit /b
Re: Remove text from specific word onwards
You would change similar to this:
Code: Select all
@echo off
setlocal
for %%F in (*720p*.*) do call :procName "%%~xF" "%%F"
exit /b
:procName
set "var=%~2"
::truncate at the 1st occurrence of 720p
set "var=%var:720p="&:%
if "%var%" neq "" ren %2 "%var%%~1"
Re: Remove text from specific word onwards
Thank you so much for your time and help Squashman and Compo!! Not sure why but the code provided by Squashman didn't alter the filenames but I tried Compo's after and it worked so once again thank you both for helping me out. This will be a big time saver!!
Cheers,
rrenis
Cheers,
rrenis
Re: Remove text from specific word onwards
BTW, I also note from an earlier post that you were also hoping to replace "." with " ", you could also achieve that thus:I'll let you play with how best to implement that.
Code: Select all
set "var=%var:.= %"
Re: Remove text from specific word onwards
Hi Compo - thanks for the tip, will see if I can get it working. Once again your help is much appreciate. Thanks!
Cheers,
rrenis
Cheers,
rrenis
Re: Remove text from specific word onwards
Hi Compo, not too sure how to impliment your suggestion but I'd been googling after your first response and adapted the code as follows. Whilst it's probably not the right way to go about it - it does work so I thought I'd post back so that anyone coming across this tread could use it. Thanks.
Cheers,
rrenis
edit: sorry, can't get the [code] working to display it properly
Code: Select all
@echo off
setlocal enabledelayedexpansion
for %%F in (*720p*.*) do call :procName "%%~xF" "%%F"
for /r %%f in (*.*) do (
set fn=%%~nf
if not [!fn!]==[] if not ["%%~nxf"]==["!fn:.=-!%%~xf"] ren "%%~f" "!fn:.= !%%~xf"
)
exit /b
:procName
set "var=%~2"
::truncate at the 1st occurrence of 720p
set "var=%var:.720p="&:%
if "%var%" neq "" ren %2 "%var%%~1"
Cheers,
rrenis
edit: sorry, can't get the [code] working to display it properly
Re: Remove text from specific word onwards
What happens if you insert the line one line up from the bottom?
Re: Remove text from specific word onwards
Thanks Compo - I was thinking it was to replace the existing!! Doh! That works great and is much more elegant than what I had posted earlier! Thanks.
Cheers,
rrenis
Cheers,
rrenis
Re: Remove text from specific word onwards
rrenis wrote:edit: sorry, can't get the [code] working to display it properly
Bulletin Board tags are just like any other language (HTML,XML) that uses them. You need an opening tag and a closing tag. The closing tag uses a forward slash.
Re: Remove text from specific word onwards
Hi squashman, sorry - missed the slash. Thanks for letting me know.
Cheers,
rrenis
Cheers,
rrenis