usebackq без задержки - PullRequest
       44

usebackq без задержки

0 голосов
/ 17 мая 2018

Следующее создает нумерованный список ссылок из удаленного каталога, например

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, что делает ссылку бесполезной. Для ссылки требуется !, поскольку она является частью ссылки.

Вторая проблема : я пытаюсь интегрировать это в программу, и я не могу использовать findstr, потому что он будет содержать ссылку и имя файла в одной строке, и когда имена файлов содержат в скобках программа вылетает. Поэтому я должен использовать usebackq, потому что он позволяет мне получить только ссылку, без необходимости иметь дело с именами файлов.

Findstr выведет список Link!1 Filename (вся строка)

Usebackq Позвольте мне просто получить Link!1

Я не могу использовать Findstr, потому что, когда имена файлов содержат круглые скобки, программа аварийно завершает работу, что может быть решено только с помощью delayedexpansion.

Это следующий пост, на котором я застрял: ( Показывает программу )
https://stackoverflow.com/questions/49564553/create-a-numbered-list-based-on-a-given-list-of-strings#=

Здесь вы можете увидеть метод findstr и узнать, как он вызывает сбои, когда имена файлов содержат круглые скобки, что можно исправить с помощью delayedexpansion, но при этом удаляется символ !, который необходим, поскольку он является частью ссылки.

Редактировать: Кажется, теперь работает, спасибо

Рабочий код

@echo off

:start:


megals --reload /Root/

set /p var1="dir? " & megals /Root/%%var1%%

for /f "tokens=1,* delims=:" %%A in ('megals -n /Root/%%var1%% ^|findstr
/n "." ') do (
set Link[%%A]=%%B
Echo %%A %%B
)

setlocal DisABLEDELAYEDEXPANSION

set /a c=0

FOR /F "tokens=1 usebackq" %%i in (`megals -n -e /Root/%%var1%%`) do (
set /a c+=1
call set "string[%%c%%]=%%i"
)

set /P number="Enter number: "

FOR /F "tokens=*" %%g IN ('call echo %%string[%number%]%%') do (SET VAR2=%%g)
echo %Var2%
echo.

Megadl %VAR2% & echo. && goto :start:

pause

https://megatools.megous.com/man/megals.html#_megatools

Ответы [ 2 ]

0 голосов
/ 17 мая 2018

Простое решение не использует отложенное расширение переменной среды, например, с помощью команды 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 /?

Смотри также:

0 голосов
/ 17 мая 2018

Вы действительно должны заключить в кавычки все команды набора.
Использование альтернативного типа отложенного расширения с вызовом:

@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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...