以下内容从远程目录(如
)创建编号的链接列表D3D11_USAGE_STAGING
1 Link!1
2 Link!2
3 Link!3
4 Link!4
5 Link!5
第一个问题:所有链接都包含一个@echo off
setlocal ENABLEDELAYEDEXPANSION
megals --reload /Root/
set /p var1="enter folder name: " & megals /Root/var1
set /a c=0
FOR /F "tokens=1 usebackq" %%i in (`megals -n -e /Root/%%var1%%`) do (
set /a c=c+1
echo !c! %%i
set string[!c!]=%%i
)
set /P number=Enter number:
echo !string[%number%]!
pause
字符,该字符会被delayedexpansion删除,导致链接无效。链接需要!
,因为它是链接的一部分。
第二个问题:我正在尝试将其集成到一个程序中,我无法使用!
,因为它会列出相同的链接和文件名line,当文件名包含括号时,程序崩溃。所以我必须使用usebackq,因为它让我只需要链接,而无需处理文件名。
findstr
将列出Findstr
(整行)
Link!1 Filename
让我得到Usebackq
我无法使用Link!1
,因为当文件名包含括号时,程序将崩溃,这只能通过delayedexpansion来解决。
这是此后的后续帖子,我被卡住了:(显示程序)
https://stackoverflow.com/questions/49564553/create-a-numbered-list-based-on-a-given-list-of-strings#=
你可以在那里看到findstr方法,以及当文件名包含括号时它会如何导致崩溃,这可以通过delayedexpansion修复,但是删除了Findstr
字符,这是必不可少的,因为它是链接的一部分。< / p>
编辑:现在似乎正在运作,谢谢
工作代码
!
答案 0 :(得分:1)
你真的应该引用你的整套命令 通过呼叫使用备用延迟扩展类型:
@echo off
setlocal DisABLEDELAYEDEXPANSION
megals --reload /Root/
set /p var1="enter folder name: " & megals /Root/var1
set /a c=0
FOR /F "tokens=1 usebackq" %%i in (`megals -n -e /Root/%%var1%%`) do (
set /a c+=1
call echo %%c%% %%i
call set "string[%%c%%]=%%i"
)
set /P number=Enter number:
call echo %%string[%number%]%%
pause
答案 1 :(得分:1)
简单的解决方案是不使用延迟环境变量扩展,例如使用命令 CALL 。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
megals.exe --reload /Root/
rem Prompt user for folder name and verify that the user has really entered
rem a folder name and remove double quotes to prevent an exit of batch file
rem execution on further processing because of an invalid command line syntax.
:EnterFolder
set "FolderName="
set /P "FolderName=Enter folder name: "
rem Has the user entered anything at all?
if not defined FolderName goto EnterFolder
rem Remove all double quotes from folder name string?
set "FolderName=%FolderName:"=%"
rem Is anything left from folder name after removing double quotes?
if not defined FolderName goto EnterFolder
megals.exe "/Root/%FolderName%"
rem Get first space/tab separated string of each line output by megals
rem assigned to an environment variable which form an array of strings.
rem Redefine end of line character from semicolon to vertical bar as it
rem is impossible that a line starts with a vertical bar. Command CALL
rem is used to double process the SET command line by Windows command
rem processor which is the alternate solution for delayed expansion.
echo/
set "Count=0"
for /F "eol=|" %%I in ('megals.exe -n -e "/Root/%FolderName%"') do (
set /A Count+=1
call echo %%Count%% %%~I
call set "string[%%Count%%]=%%~I"
)
if %Count% == 0 echo There is nothing! & goto EndBatch
echo/
rem Prompt user for number name and verify that the user has really entered
rem a decimal interpreted number which must be in range of 1-%Count%.
:EnterNumber
set "Number="
set /P "Number=Enter number (1-%Count%): "
rem Has the user entered anything at all?
if not defined Number goto EnterNumber
rem Remove all double quotes from number string?
set "Number=%Number:"=%"
rem Is anything left from number string?
if not defined Number goto EnterNumber
rem Contains the number string any non digit character?
for /F "delims=0123456789" %%I in ("%Number%") do goto EnterNumber
rem Remove all leading zeros from number string to avoid interpreting
rem the entered number as octal number on the two IF comparisons below.
:LeadingZeros
if not %Number:~0,1% == 0 goto CheckNumber
set "Number=%Number:~1%"
if defined Number goto LeadingZeros
rem The number was 0 which is less than 1.
goto EnterNumber
rem Check number in range of 1 to %Count% which requires to convert the
rem number strings on both sides of the comparison operators to signed
rem 32-bit integers by Windows command processor in background.
:CheckNumber
if %Number% GTR %Count% goto EnterNumber
if %Number% LSS 1 goto EnterNumber
rem Output the string according to entered number by forcing Windows command
rem processor again to double processing the command line because of CALL.
call echo %%string[%number%]%%
rem Restore previous environment with discarding all the
rem environment variables defined by this batch file.
:EndBatch
endlocal
pause
另一种解决方案是使用像OutputAndSet
这样的子程序。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
megals.exe --reload /Root/
rem Prompt user for folder name and verify that the user has really entered
rem a folder name and remove double quotes to prevent an exit of batch file
rem execution on further processing because of an invalid command line syntax.
:EnterFolder
set "FolderName="
set /P "FolderName=Enter folder name: "
rem Has the user entered anything at all?
if not defined FolderName goto EnterFolder
rem Remove all double quotes from folder name string?
set "FolderName=%FolderName:"=%"
rem Is anything left from folder name after removing double quotes?
if not defined FolderName goto EnterFolder
megals.exe "/Root/%FolderName%"
rem Get first space/tab separated string of each line output by megals
rem assigned to an environment variable which form an array of strings.
rem Redefine end of line character from semicolon to vertical bar as it
rem is impossible that a line starts with a vertical bar. Command CALL
rem is used to double process the SET command line by Windows command
rem processor which is the alternate solution for delayed expansion.
echo/
set "Count=0"
for /F "eol=|" %%I in ('megals.exe -n -e "/Root/%FolderName%"') do call :OutputAndSet "%%~I"
if %Count% == 0 echo There is nothing! & goto EndBatch
echo/
rem Prompt user for number name and verify that the user has really entered
rem a decimal interpreted number which must be in range of 1-%Count%.
:EnterNumber
set "Number="
set /P "Number=Enter number (1-%Count%): "
rem Has the user entered anything at all?
if not defined Number goto EnterNumber
rem Remove all double quotes from number string?
set "Number=%Number:"=%"
rem Is anything left from number string?
if not defined Number goto EnterNumber
rem Contains the number string any non digit character?
for /F "delims=0123456789" %%I in ("%Number%") do goto EnterNumber
rem Remove all leading zeros from number string to avoid interpreting
rem the entered number as octal number on the two IF comparisons below.
:LeadingZeros
if not %Number:~0,1% == 0 goto CheckNumber
set "Number=%Number:~1%"
if defined Number goto LeadingZeros
rem The number was 0 which is less than 1.
goto EnterNumber
:OutputAndSet
set /A Count+=1
echo %Count% %~1
set "string[%Count%]=%~1"
goto :EOF
rem Check number in range of 1 to %Count% which requires to convert the
rem number strings on both sides of the comparison operators to signed
rem 32-bit integers by Windows command processor in background.
:CheckNumber
if %Number% GTR %Count% goto EnterNumber
if %Number% LSS 1 goto EnterNumber
rem Output the string according to entered number by forcing Windows command
rem processor again to double processing the command line because of CALL.
call echo %%string[%number%]%%
rem Restore previous environment with discarding all the
rem environment variables defined by this batch file.
:EndBatch
endlocal
pause
要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
rem /?
set /?
setlocal /?
另见: