0

In batch, I'm failing to use "variable expansion" such as %%~x (culls all but an extension from a string) on array elements, for strings that have no tangible file structure equivalent.

These paths and file names are intangible. There are no actual folder paths and files that mirror any of these. Eventual intended use is to create an array of elements to task-kill all at once.

I've tried structuring a bunch of for loops to do this job, but I'm doing something wrong. I've no beef with For, but it's a tricky tangle to behold sometimes. so, here is a non FOR based example of what I'm trying to do and expecting to see:

Code:

@echoOff
echo %~nx0 running.
set uhray[0]=A:\BatchPlayground\NoFile.exe
set uhray[1]=B:\CmdBrainStorming\NonExistantFile.doh
set uhray[2]=C:\SomeOtherPlace\FictionalFile.lalala
SETLOCAL ENABLEDELAYEDEXPANSION
set z=0
:SymLoop
ifdefined uhray[%z%](
    echo uhray[%z%]=!uhray[%z%]!
    echo %%~x!uhray[%z%]!
    set/a "z+=1"
    GOTO :SymLoop
)
set z=
ENDLOCAL

REM .
REM .
REM .
REM  everything below here causes a substitution invalid error in the script even when its in a REMARK.@,@?
REM    echo %%~x!uhray[%z%]!
REM    set q=%uhray[!z!]%...
REM    call echo %%~xq
REM    echo %%~x!uhray[%z%]!
REM    call echo %~x!uhray[%z%]!
REM    call echo %%~x!uhray[%z%]!
REM    set Q=!uhray[%z%]!
REM    echo %%~x%Q%

Output:

C:\BatchPlayground>C:\BatchPlayground\Substitutetutelage2.bat
Substitutetutelage2.bat running.
uhray[0]= A:\BatchPlayground\NoFile.exe
%~xA:\BatchPlayground\NoFile.exe
uhray[1]= B:\CmdBrainStorming\NonExistantFile.doh
%~xB:\CmdBrainStorming\NonExistantFile.doh
uhray[2]= C:\SomeOtherPlace\FictionalFile.lalala
%~xC:\SomeOtherPlace\FictionalFile.lalala
The following usage of the path operatorin batch-parameter
substitution is invalid:%~x!uhray[%z%]!


For valid formats type CALL /?or FOR /?
The syntax of the command is incorrect.

C:\BatchPlayground>

Expected Output:

C:\BatchPlayground>C:\BatchPlayground\Substitutetutelage2.bat
Substitutetutelage2.bat running.
uhray[0]= A:\BatchPlayground\NoFile.exe
exe
uhray[1]= B:\CmdBrainStorming\NonExistantFile.doh
doh
uhray[2]= C:\SomeOtherPlace\FictionalFile.lalala
lalala

C:\BatchPlayground>

I've posted this problem over on "stack overflow" too. but immediately got downvoted into oblivion for not knowing batch code. i mean.. that is kinda the point, if your asking for help, your admitting you need help learning it better. Maybe its just troll'y over there.

TLDR: trying to get var.expansion/substitution operational with array Vars, and not just the typical %1 %2 %3 it seems to be restricted to. The definitions for things like %~n and %~nx and %%~p are inside the FOR /? help sheet oddly enough. Though they can clearly be used without a CALL or FOR itself.

If anyone has the smart beanie to spin their propeller on this, please weigh in and flex that grey muscle for me. I've been falling up stairs on this one all the dog-gone-day.

. . . . ::::Its been a few days now looking for answers.:::: . . . .

Some expressed it was too complicated to understand in its above state, so im re-stating it with a lot less code involved. lines 1, and 2, work, line 3, returns an error relating to the usage of var. extension substitution.

The same problem exists whether i use an array or not for the var. Code:

echo %~nx0 running.
set I=B:\CmdBrainStorming\NonExistantFile.doh
echo %~xI

Output:

C:\BatchPlayground>C:\BatchPlayground\Substitutetutelage3.bat

C:\BatchPlayground>echo Substitutetutelage3.bat running.
Substitutetutelage3.bat running.

C:\BatchPlayground>set I=B:\CmdBrainStorming\NonExistantFile.doh
The following usage of the path operatorin batch-parameter
substitution is invalid:%~xI


For valid formats type CALL /?or FOR /?

C:\BatchPlayground>echo %~xI

C:\BatchPlayground>

Expected Output:

C:\BatchPlayground>C:\BatchPlayground\Substitutetutelage3.bat

C:\BatchPlayground>echo Substitutetutelage3.bat running.
Substitutetutelage3.bat running.

C:\BatchPlayground>set I=B:\CmdBrainStorming\NonExistantFile.doh

C:\BatchPlayground>echo %~xI
doh

C:\BatchPlayground>

I hope this example helps further illustrate what i am trying to do. Not too much more i can remove to make this problem easier to understand for others. Note i use %~nx0 in the beginning. and it works. but i use %~xI later and it does not.

....::::some time later::::....

I got some help on this one from the people on "https://www.tenforums.com/" "Das10", and "garlin" weighed in with examples i was able to gleam a lot from. Here is the answer to the above problems i was having.

@echo Off
echo %~nx0 running.
set uhray[0]=A:\BatchPlayground\NoFile.exe
set uhray[1]=B:\CmdBrainStorming\NonExistantFile.doh
SETLOCAL ENABLEDELAYEDEXPANSION 
set z=0
:SymLoop
    if defined uhray[%z%] (
    For %%a in (!uhray[%z%]!) do (
        echo %%~xa
        )
    set /a "z+=1"
    GOTO :SymLoop
)
set z=
ENDLOCAL

and this version is an example using an array with it. Of course we could just use a shortcut and use %1 %2 %3 %4 and so on to kill a list of files. but for practice and for my ease of use and access to the files innards. I am pleased with the array version used here, to close programs Sets, that are hanging or frozen in some way. I run these files from the task manager when task-manager is all i can access. and it ends the software listed in the arrays. forcibly.

in this case, Grounded.exe and Maine-Win64-Shipping.exe

@echo Off
echo %~nx0 running.
set uhray[0]=C:\Games\utilities\Steam\steamapps\common\Grounded\Grounded.exe
set uhray[1]=C:\Games\utilities\Steam\steamapps\common\Grounded\Maine\Binaries\Win64\Maine-Win64-Shipping.exe
SETLOCAL ENABLEDELAYEDEXPANSION
set debug=true
set z=0
:SymLoop
    if defined uhray[%z%] (
    For %%a in (!uhray[%z%]!) do (
        if %debug% equ true echo operating on uhray[%z%] containing: %%a
rem     echo full path name and extension  %%a
rem     echo %%~pa
rem     echo %%~na
rem     echo %%~xa
rem     echo name and extension: %%~nx

        if %debug% equ true echo @Taskkill
        Taskkill /T /F /IM "%%~nxa" > nul
        Taskkill /T /F /IM "%%a" > nul
        Taskkill /T /F /IM "%%~pa" > nul
        if %debug% equ true echo @wmic
rem     wmic process where "name='%%a'" delete > nul     Error: description = invalid query
        wmic process where "name='%%~nxa'" delete > nul
        if %debug% equ true echo @task_List-Find-Kill
        set "$process=%%~nxa*"
        for %%b in (%$process%) do tasklist >nul | find "%%b" && taskkill /F /IM %%b /T > nul
        if %debug% equ true echo @killed
        echo.   
        echo %%~na should be closed now.
        echo.
        echo.
        echo.
        echo.
        )
    
    set /a "z+=1"
    GOTO :SymLoop
)
set z=
set "$process="
ENDLOCAL
echo %~nx0 closing in 10 seconds.
Timeout /T 10 >NUL

0 Answers0