Delete a filename after finding a pattern

Microsoft Windows xp professional w/serv...
December 15, 2010 at 04:28:22
Specs: Windows XP, 3.199 GHz / 1526 MB
Hi.

I would like to rename some filenames (in a batch file) like in the example bellow:

The.Big.Bang.Theory.S04E09.720p.HDTV.x264-IMMERSE.srt
The.Big.Bang.Theory.S04E09.HDTV.XviD-FQM.srt

rename to

The Big Bang Theory S04E09 720p.srt
The Big Bang Theory S04E09.srt

Can someone help please?
Thanks


See More: Delete a filename after finding a pattern

Report •

#1
December 15, 2010 at 19:59:24
Try:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

FOR /f "tokens=*" %%a IN ('DIR /b "*.srt"') DO (
	SET Var=%%a
	ECHO !Var!| FIND "720p"
	IF "!Errorlevel!"=="0" (
		FOR /f "tokens=1-6 delims=." %%i IN ('ECHO !Var!') DO (
			SET Var=%%i %%j %%k %%l %%m %%n
		)
	) ELSE (
		FOR /f "tokens=1-5 delims=." %%i IN ('ECHO !Var!') DO (
			SET Var=%%i %%j %%k %%l %%m
		)
	)
	REN "%%a" "!Var!.srt"
)

Can I put a question here?


Report •

#2
December 16, 2010 at 01:02:24
Whoa, works like a charm. Very thanks.

Only one small thing. I was planning to use this script to other TV series, and for videos files also. In those cases the output filename from the script is not the expected.

More files examples:
How.I.Met.Your.Mother.S06E05.HDTV.XviD-LOL.avi
The.simpsons.s22e04.hdtv.lol.avi
CSI.Miami.S09E09.720p.HDTV.X264-DIMENSION.mkv

Is there a quick fix for this?

Thanks


Report •

#3
December 16, 2010 at 04:38:52
You can change it to fit each tv show, but there isn't really a way to have it know automatically. You will need to change the number of variables for each.

For example, How I Met Your Mother need the first six words, or, if it contains 720p it needs seven.

So, just add (or remove) variables, and change the number of tokens (think of them as words). You will also have to change the file extensions.

Ex.
----
@ECHO OFF
SETLOCAL EnableDelayedExpansion

FOR /f "tokens=*" %%a IN ('DIR /b "*.avi"') DO (
SET Var=%%a
ECHO !Var!| FIND "720p"
IF "!Errorlevel!"=="0" (
FOR /f "tokens=1-6 delims=." %%i IN ('ECHO !Var!') DO (
SET Var=%%i %%j %%k %%l %%m %%n %%o
)
) ELSE (
FOR /f "tokens=1-6 delims=." %%i IN ('ECHO !Var!') DO (
SET Var=%%i %%j %%k %%l %%m %%n
)
)
REN "%%a" "!Var!.avi"
)
----

This will work for How I Met Your Mother. You would have to do this for every single show you have.

Sorry if it seems like a lot of work, but I'm not sure of a way around this. If another user is able to help, they will.

Can I put a question here?


Report •

Related Solutions

#4
December 16, 2010 at 05:39:43
Thanks again for your quick reply.

I've a few ideas now.

- For the file extensions I think that using something like this, it should work (in my tests it worked):
('DIR /b "*.avi","*.mkv","*.srt")
Update: I see now that the problem could be in the renaming part in the end of the script, but maybe I could read the actual file extension and use it in the saving process, right?

- For the tokens issue the problem seems bigger, but maybe I can do some variable sets, like:
If filename = ??something?? set token_numbers=x
If filename = ??something?? set token_numbers=y
If filename = ??something?? set token_numbers=z

in the ??something?? part could something like the following:
dir /b how?i?met?your?mother*

would it work?

I got this ideas, but the problem is implement them :)


Report •

Ask Question