Remove text from specific word onwards

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rrenis
Posts: 8
Joined: 16 Sep 2016 04:16

Remove text from specific word onwards

#1 Post by rrenis » 16 Sep 2016 04:25

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove text from specific word onwards

#2 Post by foxidrive » 16 Sep 2016 05:29

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.

rrenis
Posts: 8
Joined: 16 Sep 2016 04:16

Re: Remove text from specific word onwards

#3 Post by rrenis » 16 Sep 2016 05:59

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.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Remove text from specific word onwards

#4 Post by Squashman » 16 Sep 2016 06:29

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

rrenis
Posts: 8
Joined: 16 Sep 2016 04:16

Re: Remove text from specific word onwards

#5 Post by rrenis » 16 Sep 2016 06:37

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...

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.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Remove text from specific word onwards

#6 Post by Squashman » 16 Sep 2016 07:32

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

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Remove text from specific word onwards

#7 Post by Compo » 16 Sep 2016 07:36

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"

rrenis
Posts: 8
Joined: 16 Sep 2016 04:16

Re: Remove text from specific word onwards

#8 Post by rrenis » 16 Sep 2016 07:50

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

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Remove text from specific word onwards

#9 Post by Compo » 16 Sep 2016 08:04

BTW, I also note from an earlier post that you were also hoping to replace "." with " ", you could also achieve that thus:

Code: Select all

set "var=%var:.= %"
I'll let you play with how best to implement that.

rrenis
Posts: 8
Joined: 16 Sep 2016 04:16

Re: Remove text from specific word onwards

#10 Post by rrenis » 16 Sep 2016 08:12

Hi Compo - thanks for the tip, will see if I can get it working. Once again your help is much appreciate. Thanks!

Cheers,
rrenis

rrenis
Posts: 8
Joined: 16 Sep 2016 04:16

Re: Remove text from specific word onwards

#11 Post by rrenis » 16 Sep 2016 08:16

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.

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

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Remove text from specific word onwards

#12 Post by Compo » 16 Sep 2016 09:02

What happens if you insert the line one line up from the bottom?

rrenis
Posts: 8
Joined: 16 Sep 2016 04:16

Re: Remove text from specific word onwards

#13 Post by rrenis » 16 Sep 2016 09:09

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Remove text from specific word onwards

#14 Post by Squashman » 16 Sep 2016 09:15

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.

rrenis
Posts: 8
Joined: 16 Sep 2016 04:16

Re: Remove text from specific word onwards

#15 Post by rrenis » 16 Sep 2016 09:22

Hi squashman, sorry - missed the slash. Thanks for letting me know.

Cheers,
rrenis

Post Reply