ZTree.com  | ZEN  | About...  

 Index   Back

String Substitution Explanation   [Help!]

By: Art Kocsis     So Calif  
Date: Jun 14,2017 at 09:16
In Response to: Set tst=%lst:%ext%=% hard to understand this. (Ryan)

> > Then test if extracted string appears in a specified list (the source
> > string is not changed if the target string is not found, deletes it
> > if found):
> >
> > Set lst=MP2,MP3,OGG,WAV
> > Set tst=%lst:%ext%=%
> > If "%tst%" NEQ "%lst%" GoTo ValidExt
> > If "%tst%" EQU "%lst%" GoTo InvalidExt
> >
> > Or in one statement:
> >
> > If "%lst%" NEQ "%lst:%ext%=%" GoTo ValidExt
> >
> > So, testing...
> >
> > Well, that would have been nice if one could use variables within
> > string substitution. However, Windows command console does not
> > support variables, only literals.
> >
> > Starting over:
> >
> > Setlocal
> > Set fil=%5
> > If "%fil%" NEQ "%fil:MP2=%" GoTo ValidExt
> > If "%fil%" NEQ "%fil:MP3=%" GoTo ValidExt
> > If "%fil%" NEQ "%fil:OGG=%" GoTo ValidExt
> > If "%fil%" NEQ "%fil:WAV=%" GoTo ValidExt
> > GoTo InvalidExt
> >
> > This should work and it's not too bad (and actually is probably
> > quicker!). I just have a strong predilection for generalized
> > variables rather than hard coding literals.
>
> -------------------------------------------------------------------------------
> Set fil=%5
> Set ext=%fil:~-3%
>
> Set lst=MP2,MP3,OGG,WAV
>
> Set tst=%lst:%ext%=%
>
> If "%tst%" NEQ "%lst%" echo no found & goto sss
> If "%tst%" EQU "%lst%" echo found & goto sss
> :sss
> pause
>
>
> doesnt work, because it reports always found
> -------------------------------------
> Set fil=%5
> Set ext=%fil:~-3%
>
> Set lst=MP2,MP3,AVI,mp4
>
> Set tst=%lst:%ext%=%
>
> If "%lst%" NEQ "%lst:%ext%=%" GoTo ValidExt
> echo not found & goto end
>
> :ValidExt
> echo found
> goto end
>
> :end
> pause
>
> doesnt work, because it reports always not found
> --------------------------------------
>
> i have a trouble understanding this, it seems so high powered.
>
> Set tst=%lst:%ext%=%

Yes, it doesn't work. that is just what I said:

"Windows command console does not support variables
[in string substitution], only literals."

That's why I posted the second solution using literal file extension strings.

You really need to test and try things out in a command console window (with echo on), to see what is going on. Did you read the references that I posted? They have detailed explanations and good examples.

As to how it works: Taking one of the literal tests, for example:

If "%fil%" NEQ "%fil:MP2=%" GoTo ValidExt

The string substitution on the right hand side replaces all occurrences of MP2 in the source string %fil% with a nul string - effectively deleting all MP2s, if any exist. The comparison tests the original vs the edited string. If MP2 did not appear in %fil%, no changes would have been made and the operands would be equal. Contrariwise, if the MP2 string did appear it would have been deleted and the operands would be unequal.

What I was trying to do (which didn't work, as I said in the original post), was to use a variable instead of the (for example), MP2 literal. Then only a single instance of the literal list and only one set of If statements would have been needed - making for easy code maintenance.

Art


Namaste', Art

Press any key to exit,
Press any other key to continue...
- unknown

768 views      
Thread locked
 

Messages in this Thread

 
96,640 Postings in 12,231 Threads, 350 registered users, 37 users online (0 registered, 37 guests)
Index | Admin contact |   Forum Time: Apr 18, 2024 - 6:20 pm UTC  |  Hits:62,916,617  (11,041 Today )
RSS Feed