Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: pball
Hey again. This time I would like to be able to search a file name for a certain value.
I'm using a for loop to check each file in a folder and would like to check if it has a certain value in it. If it does I'd like to do something to it.
Like
setlocal enabledelayedexpansion
for /f "tokens=*" %%x in ('dir /b *.ac3') do (
if (%%x has %var% in the file name) do something here
)as an outline of what I'd like. I hope you get the above is just the theory of what i want.

Thanks for the help.
I didn't really try too hard to use that and finally though of something I should of though about long ago.
I used to set a variable to the part of the file name where the number I was searching for was, problem being if there was more characters in front of the number I wanted it wouldn't work. Thus I just set two variables to the two locations the number could be then used if statements to set the conditions depending on the location of the number i was searching for. I doubt what I just wrote is understandable.
setlocal enabledelayedexpansion
for /f "tokens=*" %%x in ('dir /b *.ac3') do (
set yourvar=%%x
set yo=!yourvar:~2,1!
set yo2=!yourvar:~3,1!
if !yo!==T (
set C=!yourvar:~3,2!
set nam=!yourvar:~0,1!
)
if !yo2!==T (
set C=!yourvar:~4,2!
set nam=!yourvar:~0,2!
)
if !C!==%eng% ren "%%x" !nam!_eng.ac3
if !C!==%jap% ren "%%x" !nam!_jap.ac3
if not !C!==%eng% if not !C!==%jap% del "%%x"
)This basically looks for the value of eng or jap, which will be numbers. These numbers come after a T in the file name so it sets two variables to the two spots T could be, then sees which one is actually T.
the file names are like this
1 T80 blah blah.ac3
10 T80 blah blah.ac3So as long as the first number is under 3 digits it works.
I wonder if my long final thoughts and script posts ever help anyone, or just confuse them more.

Ok well I have a quick question to tag onto this topic.
I have this script for renaming audio files in a folder depending on what track number they are, usually 80 is eng and 81 is jap
This portion trys to locate the track in the file name.
yourvar is a file name
--------------------------
set yo=!yourvar:~2,1!
set yo2=!yourvar:~3,1!
if !yo!==T (
set C=!yourvar:~3,2!
set nam=!yourvar:~0,1!
)
if !yo2!==T (
set C=!yourvar:~4,2!
set nam=!yourvar:~0,2!
)
--------------------------
I have it look for the T which is before the number,The yo looks in the 3rd character while the yo2 looks in the 4th character. Then sets C to the number and nam to the one or two digit number at the beginning of the file name.Is there a way to loop and increment it so the 2 in this will go up each time?
set yo=!yourvar:~2,1!Then have the 3 and 1 also increment by one
set C=!yourvar:~3,2!
set nam=!yourvar:~0,1!
or just find the two digit number after the capital T in a file name, set that to a variable, and have another variable set to everything before the T minus the space before the T.so taking
Hey you 22 T80 2_0ch 192Kbps DELAY 0ms.ac3get you a var with 80 in it
and Hey you 22 in another with no space behind itThanks again

I always like to have CMD work for me.
CALL :parse Hey you 22 T80 2_0ch 192Kbps DELAY 0ms.ac3
@ECHO %title% - %num%
GOTO :EOF
:parse
SET element=%1
IF "%element%"=="%element:~-3%" IF "%element:~0,1%"=="T" (SET num=%element:~-2%& GOTO :EOF)
IF NOT "%title%"=="" (SET title=%title% %1) ELSE SET title=%1
SHIFT
GOTO parse

Thank very much this gets the output I need.
Took a while to get it to work in a for loop, stick the subprogram part out of the loop helps (stupid me).
Thank you so much.
If your bored could you explain the general idea please.
and as usual the finished script
setlocal enabledelayedexpansion
for /f "tokens=*" %%x in ('dir /b *.ac3') do (
set title=
call :parse %%xif !num!==%eng% ren "%%x" "!title!_eng.ac3"
if !num!==%jap% ren "%%x" "!title!_jap.ac3"
if not !num!==%eng% if not !num!==%jap% del "%%x"
):parse
SET element=%1
IF "%element%"=="%element:~-3%" IF "%element:~0,1%"=="T" (SET num=%element:~-2%& GOTO :EOF)
IF NOT "%title%"=="" (SET title=%title% %1) ELSE SET title=%1
SHIFT
GOTO parsebefore this part you enter and number for the eng and jap variable. Then this part will take the file which was talked about too much and see if it has one of those numbers in it then if it does rename it to the base name plus _eng or _jap depending on the number in it. Then deletes the other files.
one day i'll have to post my whole script all these little pieces go into.

If your bored could you explain the general idea please.
Meh, sure.We use CALL to change the parameter list to the name of the show. Then we just walk along the elements, looking for a three letter element beginning with 'T'. When we find it, our subfunction returns. Technically, 'The' would pass our checks, so you'll have to fix that bug.
Personally, I'd get rid of the &GOTO :EOF, then, on the next line, say ECHO %num% | findstr [0-9][0-9] >NUL &&GOTO :EOF on the next line, but then you'd have to reset num before running the loop as well.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |