Computing.Net > Forums > Programming > Search a file name, Batch

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Search a file name, Batch

Reply to Message Icon

Name: pball
Date: October 6, 2008 at 15:22:44 Pacific
OS: XP Pro
CPU/Ram: 2.8 ghz / 2 gig
Product: home made
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: October 6, 2008 at 15:53:59 Pacific
Reply:

Wildcards are your friend.

FOR %%a IN (*%var%*.ac3) DO @ECHO %%a


0

Response Number 2
Name: pball
Date: October 7, 2008 at 07:40:57 Pacific
Reply:

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

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


0

Response Number 3
Name: pball
Date: October 8, 2008 at 15:30:12 Pacific
Reply:

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

get you a var with 80 in it
and Hey you 22 in another with no space behind it

Thanks again


0

Response Number 4
Name: Razor2.3
Date: October 8, 2008 at 17:38:22 Pacific
Reply:

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


0

Response Number 5
Name: pball
Date: October 8, 2008 at 19:42:46 Pacific
Reply:

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 %%x

if !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 parse

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


0

Related Posts

See More



Response Number 6
Name: Razor2.3
Date: October 9, 2008 at 21:42:13 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Search a file name, Batch

Getting a file name into a variable www.computing.net/answers/programming/getting-a-file-name-into-a-variable/17233.html

Shorten a file name using a batch www.computing.net/answers/programming/shorten-a-file-name-using-a-batch/18629.html

System date in file names www.computing.net/answers/programming/system-date-in-file-names/11350.html