how do i correct filenames with !'s in EnableDelayedExpansion?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

how do i correct filenames with !'s in EnableDelayedExpansion?

#1 Post by nnnmmm » 02 Sep 2017 09:17

Code: Select all

@ECHO OFF
SET P1=C:\PROGRAMS\WINAMP\WINAMP.EXE
SET FSPEC=*.MP3 *.WAV
REM ----------------------------------------
SetLocal EnableExtensions EnableDelayedExpansion

   SET K1=0
   FOR %%V IN (%FSPEC%) DO (
      SET /A K1=K1+1
   )

   IF %K1% EQU 0 (
      ECHO No files found
      GOTO END
   )
   IF %K1% GEQ 32768 (
      ECHO The total number of files must be less than 32768.
      PAUSE
      GOTO END
   )
   REM ----------------------------------------

   SET N=%K1%
   SET /A RND = %RANDOM%
   SET /A RND = %RANDOM%
   SET /A RND = %RANDOM%
   SET /A RND = %RANDOM%
   SET /A RND = %RANDOM%
   SET /A RND = %RANDOM%
   SET /A RND = %RANDOM% * N / 32768 + 1

   REM ----------------------------------------

   SET K3=0
   FOR %%V IN (%FSPEC%) DO (
      SET /A K3=!K3!+1

      IF !K3! EQU %RND% (
      SET ANS="%%V"
      START %P1% !ANS!
      )
   )
:END


!ANS! ruins the filenames with "!"'s, i often use them as a "1st line marker"
how can i correct this?
this batch finds the total number of files and randomly selects the one for winamp to play it.
why so many random's in a batch? because if it is one, a batch only selects the same one.

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

Re: how do i correct filenames with !'s in EnableDelayedExpansion?

#2 Post by Squashman » 02 Sep 2017 09:23

Just use the FOR variable directly. No need to set it to an environmental variable.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do i correct filenames with !'s in EnableDelayedExpansion?

#3 Post by aGerman » 02 Sep 2017 09:33

Didn't I already link this post?
viewtopic.php?t=6866
Disable delayed expansion for variable assignments, enable it if you want to work with the variable values. You only need delayed expansion in the body of your FOR loop. Thus, initially disable it at the beginning of your code and only enable it where you need it.

Code: Select all

...
SetLocal EnableExtensions DisableDelayedExpansion
...
   SET K3=0
   FOR %%V IN (%FSPEC%) DO (
      SET /A "K3 += 1"
      SET ANS="%%V"
      setlocal EnableDelayedExpansion
      IF !K3! EQU %RND% (
         START !P1! !ANS!
      )
      endlocal
   )
...

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: how do i correct filenames with !'s in EnableDelayedExpansion?

#4 Post by nnnmmm » 02 Sep 2017 13:29

Code: Select all

   SetLocal EnableExtensions EnableDelayedExpansion
   FOR %%V IN (%FSPEC%) DO (
          SET /A K3=!K3!+1
             
      IF !K3! EQU %RND% (
           SetLocal EnableExtensions DisableDelayedExpansion
           SET ANS="%%V"
           SetLocal EnableExtensions EnableDelayedExpansion
           START %P1% !ANS!
      )
  )


or

Code: Select all

   SetLocal EnableExtensions EnableDelayedExpansion
   FOR %%V IN (%FSPEC%) DO (
          SET /A K3=!K3!+1
             
      IF !K3! EQU %RND% (
           SetLocal EnableExtensions DisableDelayedExpansion
           START %P1% "%%V"
           EndLocal
      )
  )


i got it, endlocal stops the missing operator messages. i think i understood clearer, thanks.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do i correct filenames with !'s in EnableDelayedExpansion?

#5 Post by aGerman » 02 Sep 2017 14:42

In your second code you used EnableDelayedExpansion and DisableDelayedExpansion in the wrong order.

~~~

Setlocal opens a new subenvironment (scope for environment variables). The newly created subenvironment inherits the variables and their values from the parent environment.

Endlocal closes the subenvironment. New variables that were defined in the subenvironment are now out of scope and thus, undefined again. Inherited variables that were updated in the subenvironment are reset to their former value.

Subenvironments can be nested were the endlocal commands close the subenvironments in reverse order of their creation by the setlocal commands. (Similar to opening and closing parentheses.)

To every setlocal should belong an endlocal command. At the end of a script file all still missing enlocal commands are implicitely executed.

~~~

It was no accident that I used the += operator to increment variable K3.
1) The SET /A syntax allows you to use variable names instead of their value. So
set /a "K3=K3+1"
would work without the need to expand K3 in the script. However
set /a "K3 += 1"
works the same and is the preferred oparation.
2) The exclamation mark is the "Logical NOT" operator in a SET /A evaluation. So you should avoid it unless you want to negate a value.

Steffen

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: how do i correct filenames with !'s in EnableDelayedExpansion?

#6 Post by nnnmmm » 03 Sep 2017 00:33

>In your second code you used EnableDelayedExpansion and DisableDelayedExpansion in the wrong order.

Code: Select all

AA=
    SetLocal DisableDelayedExpansion
    FOR %%V IN (%FSPEC%) DO (
          SET /A K3=!K3!+1     
       IF !K3! EQU %RND% (
           SetLocal EnableDelayedExpansion
           START %P1% "%%V"
           EndLocal
       )
   )

Code: Select all

if i put AA= like the above, K3 will be corrupted, and then
SetLocal EnableDelayedExpansion will corrupt the %%V below
START %P1% "%%V"

therefore i had to do it like the way i did like below
SetLocal EnableDelayedExpansion
FOR %%V IN (%FSPEC%) DO (
     SET /A K3=!K3!+1   


SetLocal EnableDelayedExpansion corrupts %%V, but i dont care about %%V corruption at this line, because i only need the K3 to get the right order number from the %%V list. then once that order number is found, i dont care about the K3 anymore, but only the correct name from %%V,
SetLocal DisableDelayedExpansion just deliver the correct filename from %%V, so i execute the final line.
START %P1% "%%V"
and Endlocal killed any leftover of FOR %%V in () that i dont need anymore

i thought you showed me, use these two SetLocal EnableDelayedExpansion and SetLocal DisableDelayedExpansion like some kind of memory resident command until the other shows up, or it will rule it through all its way.

>The exclamation mark is the "Logical NOT" operator in a SET /A (K3=!k3!+1 you mean here?) evaluation. So you should avoid it unless you want to negate a value.
i need the number preserved, i dont know any other way to achieve it

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: how do i correct filenames with !'s in EnableDelayedExpansion?

#7 Post by nnnmmm » 03 Sep 2017 00:48

i just checked SET /A K3+=1 that it worked the same way as SET /A K3=!K3!+1
but when i see K3+=1, it makes me wonder if EnableDelayedExpansion or Disable, where is the line?
but when i see SET /A K3=!K3!+1, i know, there is EnableDelayedExpansion somewhere, because of that "!"

i tried to make sub and call for this code, and tried to send the ! ! and %% then make an output somewhere else, like the way you showed me but i couldnt make it work, because i realized that this one already had EnableDelayedExpansion claimed in the main code, the one you showed me was in sub.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do i correct filenames with !'s in EnableDelayedExpansion?

#8 Post by aGerman » 03 Sep 2017 13:30

You can't force me to write Batch code more ambiguous than it is by design. So I told you what the right syntax is. If you expect Batch should have a different syntax then I tell you that I expect Basic should have a += operator. In both cases it doesn't make any sense to discuss about.

Steffen

Post Reply