Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Done things getting too abstract for me

Jul
532
10
UNIMPORTANT BACKSTORY: So, I’m passing a filename to a subroutine that creates a BAT file [to be run once]. The filename has percent signs in it -- ugh. After an hour of fussing with setdos, which needs to be x-3 in some situations, x-4 in others.... I successfully achieve the goal of getting the % sign properly represented in the generated bat file.

THE REAL PROBLEM:
I have a (generated) BAT file that looks like this:

Code:
@Echo on

@setdos /x-4
rem This setdos /x-4 makes the % in the filename successfully pass to the process subroutine

gosub process "18_60% (Reprise).mp3"

goto :skip_subs
        :process [file]
                setdos /x-4
                echo call get-lyrics %file%  I AM DOING JUST-THE-ECHO UNTIL IT WORKS RIGHT
                 rem call get-lyrics %file%   CAN’T ACTUALLY DO IT UNTIL IT WORKS RIGHT
                setdos /x0
        return
:skip_subs
@setdos /x0
:END

So here’s the problem.

If i use setdos /x-4 before calling get-lyrics, the percent is missing! Because it evaluated it as a nested environment variable, even though it’s not an environment variable that exists:

call get-lyrics "18_60 (Reprise).mp3"

But if i use setdos /x-3 before calling get-lyrics, it doesn’t expand the environment variable to the filename at all:

call get-lyrics %file%

So:
/x-3 - stop all var expansion - is too little expansion: it prevents %file from expanding
/x-4 - stop NEXTED expansion - is too much expansion - %file is expanded, but it tries to expand percent signs in the filename.


How how how how how can I deal with this situation? I’m stuck.

My spouse wants me to get away from the computer.

Meanwhile I’m over here like "What I need is setdos /x- 3-and-a-half"
 
Several things:

1. % is an invalid filename character in Windows. Yes, I know it's possible to stuff it into a filename, but it's a really, really bad idea for any number of reasons.

2. I think that unless you also want to do variable substitution on your GOSUB parameters, you have your first SETDOS wrong. Try this:

Code:
@Echo on

@setdos /x-3
rem This setdos /x-3 makes the % in the filename successfully pass to the process subroutine

gosub process "18_60% (Reprise).mp3"

goto :skip_subs
        :process [file]
                setdos /x0
                setdos /x-4
                echo call get-lyrics %file%  I AM DOING JUST-THE-ECHO UNTIL IT WORKS RIGHT
                 rem call get-lyrics %file%   CAN’T ACTUALLY DO IT UNTIL IT WORKS RIGHT
                setdos /x0
        return
:skip_subs
@setdos /x0
:END

That will generate this output:

Code:
rem This setdos /x-3 makes the % in the filename successfully pass to the process subroutine
gosub process "18_60% (Reprise).mp3"
setdos /x0
setdos /x-4
echo call get-lyrics "18_60% (Reprise).mp3"  I AM DOING JUST-THE-ECHO UNTIL IT WORKS RIGHT
call get-lyrics "18_60% (Reprise).mp3"  I AM DOING JUST-THE-ECHO UNTIL IT WORKS RIGHT
rem call get-lyrics %file%   CANÆT ACTUALLY DO IT UNTIL IT WORKS RIGHT
setdos /x0
return
goto :skip_subs

If you do want to pass a variable to GOSUB, but don't want it expanded it after the first time, you'll need SETDOS /X-4 and/or SETDOS /X-3 where you are setting the variable.

3. Or the slightly simpler approach would be to use single backquotes around the GOSUB parameter (i.e., before and after the double quotes) which would prevent TCC from expanding anything inside. Then you don't need the first SETDOS /X3. But if you are also trying to pass a variable into the GOSUB argument list, that won't work.

4. Or you could replace the %'s with another character when you get the filename, then switch them back before doing your final CALL.

5. Did I mention that %'s in a filename are a really bad idea? CMD and PowerShell are going to have an even harder time dealing with them, as they don't have the TCC workarounds.

6. What you want here is the DWIM parser (Do What I Mean) -- i.e., "sometimes expand variables and sometimes don't, depending on rules that I can't define but I'll know it when I see it".

7. What I would do is do a mass renaming of my files to rid them of the noxious %'s, and then all your problems magically go away.
 
Ahh yes, a DWIM parser. That would be amazing :) :) :)

You’re right. In my case, because I was working on something and it was holding me up, I bypassed the situation by changing my % into the unicode/emoji double-width percent.
1735054335618.webp


I used to think those redundant unicode/emoji were silly, but now I really appreciate having a percent that isn’t a percent, a quote that isn’t a quote, and an apostrophe that isn’t an apostrophe (so much so that my ' key is remapped to the unicode/smart one and i have to alt-' to use the traditional one)

I’m marveled and amazed at your ability to work through this one. :)
 
Back
Top