提问人:ThumpieBunnyEve 提问时间:6/5/2023 最后编辑:ThumpieBunnyEve 更新时间:6/6/2023 访问量:40
已解决:如何在批处理字符串上使用变量扩展来循环访问数组?
Solved: How do i loop through arrays using variable expansion on strings in batch?
问:
在批处理中,对于没有有形文件结构等效的字符串,我无法在数组元素上使用“变量扩展”,例如 %%~x(从字符串中剔除扩展名之外的所有扩展名)。
这些路径和文件名是无形的。没有实际的文件夹路径和文件可以镜像其中任何一个。最终的预期用途是创建一个元素数组,以便一次性杀死所有元素。
我尝试过构建一堆for循环来完成这项工作,但我做错了什么。我没有牛肉,但有时看起来很棘手。所以,这里有一个非基于FOR的例子,说明我正在尝试做什么并期望看到:For
法典:
@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%
输出:
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>
预期输出:
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>
我也在“堆栈溢出”上发布了这个问题。但立即因为不知道批处理代码而被否决。 我的意思是。。这就是重点,如果你寻求帮助,你承认你需要帮助更好地学习它。也许它只是那边的巨魔。
TLDR:试图使用数组 Var 进行 var.expansion/substitution 操作,而不仅仅是典型的 %1 %2 %3,它似乎被限制在内。 %~n 和 %~nx 以及 %%~p 等内容的定义位于 FOR /?奇怪的是,帮助表。尽管它们显然可以在没有 CALL 或 FOR 本身的情况下使用。
如果有人有聪明的无檐小便帽来旋转他们的螺旋桨,请为我称重并弯曲那块灰色的肌肉。 我一直在从楼梯上摔下来,一整天都不见了。
. . . . ::::现在已经有几天了,正在寻找答案。 . . . .
有些人表示在上述状态下它太复杂了,无法理解,所以我用更少的代码来重述它。 1号线, 2、工作、 第 3 行,返回与 var. extension 替换的使用相关的错误。
无论我是否对 var 使用数组,都存在同样的问题。 法典:
echo %~nx0 running.
set I=B:\CmdBrainStorming\NonExistantFile.doh
echo %~xI
输出:
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>
预期输出:
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>
我希望这个例子有助于进一步说明我正在尝试做什么。 我不能删除太多,以使其他人更容易理解这个问题。 请注意,我在开头使用 %~nx0。它起作用了。但我后来使用 %~习 它没有。
....::::一段时间后::::....
我从“https://www.tenforums.com/”上的人那里得到了一些帮助 “Das10”和“garlin”用我能够从中闪耀很多的例子来衡量。这是我遇到的上述问题的答案。
@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
这个版本是一个使用数组的例子。 当然,我们可以只使用快捷方式,并使用 %1、%2、%3、%4 等来终止文件列表。但为了练习和我的易用性和对文件内部的访问。我对这里使用的数组版本感到满意,用于关闭以某种方式挂起或冻结的程序集。当任务管理器是我所能访问的全部时,我从任务管理器运行这些文件。并结束数组中列出的软件。强行。
在这种情况下,Grounded.exe 和 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
答: 暂无答案
评论