New
#1
CMD/Batch help with Var Expansion.
I have a little problem of a batch,
Justa sootch, for some hooch, will you help a Paisano?
parameter's im slighting, variables im fighting! me!
HelpMeYo, HelpMeYo,
HelpMeYo, HelpMeYo,
HelpMe~ YouGotaHelpaBro!
So I've got some problems getting Batch, to allow me to parameter substitution on my own vars.
It normally works with command line parameters. which are given the var names 0 1 2 3 4 and so on at the start of a batch environment.
so something like "C:/Freddy Mercury/Bohemian/Rhapsody.exe" "C:/reload/rob zombie.ogg" "N:/ever/gona/give/u.up" "I:/love/ewe/u/love.me"
would result in the environment of the running batch having the vars 0 1 2 3 being the 4 above quoted elements.
Then you can use parameter expansion/substitution on them, to get just the path or file name or extension.
so for example with the above parameters passed...
echo %%~n3
echo %%~x3
echo %%~x2
echo %%~n2
would result in
love
me
up
rob zombie
eew... but anyway, i want to insert my own vars through this process inside the running batch file.
somehow that sounds more seedy now then its meant to be... but moving on..
here is my code.. which does not work.
Output:Code:@echo Off 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 if defined 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%
Code: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 operator in 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:
I've posted this problem over on "stack overflow" too. but immediately got downvoted into oblivion for not knowing batch code.Code: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 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.
Please assist?