批处理文件:将用户输入的字符串分解为数组

时间:2021-08-01 22:05:23

I am writing a simple batch file that copies a video from a web server using a user submitted url. I would like to extract parts of the user submitted string and set them as a variables. Below is my example code.

我正在编写一个简单的批处理文件,它使用用户提交的URL从Web服务器复制视频。我想提取部分用户提交的字符串并将它们设置为变量。下面是我的示例代码。

@echo OFF
SET outputdir= %~dp0output\
SET libav= %~dp0libav\win64\usr\bin
CD %libav%
SET /p code= "Paste the download code from your browser: "

The user will be inputting a string that is in this exact format for %code%:

用户将输入一个格式为%code%的字符串:

avconv -i "http://example.com/i/index_0_av.m3u8" -codec copy -qscale 0 video_name.mp4

I want to use a space as the delimiter. I need to set the 3rd item as %url% and 8th item as %filename%. Basically I am trying to do what explode() does in PHP.

我想用空格作为分隔符。我需要将第3项设置为%url%,将第8项设置为%filename%。基本上我正在尝试做在PHP中的explode()。

The goal is to run the following as the final line in the file.

目标是将以下内容作为文件中的最后一行。

call avconv -i "%url%" -codec copy -qscale 0 %outputdir%%filename%

3 个解决方案

#1


Although there are several ways to do what you want in Batch, the version below do it emulating PHP's explode:

虽然有几种方法可以在批处理中执行您想要的操作,但下面的版本可以模拟PHP的爆炸:

@echo off
setlocal EnableDelayedExpansion

set code=avconv -i "http://example.com/i/index_0_av.m3u8" -codec copy -qscale 0 video_name.mp4

rem Explode "code" string into "item" array
set i=0
for %%a in (%code%) do (
   set /A i+=1
   set "item[!i!]=%%~a"
)

rem Set 3rd item as url and 8th item as filename
set url=%item[3]%
set filename=%item[8]%

echo call avconv -i "%url%" -codec copy -qscale 0 %outputdir%%filename%

#2


For /f "tokens=3" %A in ("%CODE%") Do Echo %A

Which is same as

哪个是一样的

For /f "tokens=3" %A in ("avconv -i http://example.com/i/index_0_av.m3u8 -codec copy -qscale 0 video_name.mp4") Do Echo %A

I removed your quotes. Quotes only needed with spaces. URLs can't have spaces.

我删除了你的报价。报价只需要空格。网址不能包含空格。

In a batch file use %%A ratherthan %A at command prompt.

在批处理文件中,在命令提示符下使用%% A ratherthan%A。

If you need quotes see UseBackQ option where single quotes enclose string.

如果需要引号,请参阅UseBackQ选项,其中单引号括起字符串。

#3


:loop
SET /p "code=Paste the download code from your browser: "
for /f "tokens=3,8,9 delims= " %%a in ("%code%") do (
  if "%%b"=="" echo too few parameters & goto :loop
  if not "%%c"=="" echo too much parameters & goto :loop
  set "url=%%~a"
  set "filename=%%b"
)
echo %url%, %filename%

Note: the tilde in %%~a removes the quotes. Leave out the tilde, if you want to keep the quotes.

注意:%% ~a中的波浪号将删除引号。如果你想保留报价,请忽略波浪号。

Edited to implement a simple input check

编辑实现简单的输入检查

#1


Although there are several ways to do what you want in Batch, the version below do it emulating PHP's explode:

虽然有几种方法可以在批处理中执行您想要的操作,但下面的版本可以模拟PHP的爆炸:

@echo off
setlocal EnableDelayedExpansion

set code=avconv -i "http://example.com/i/index_0_av.m3u8" -codec copy -qscale 0 video_name.mp4

rem Explode "code" string into "item" array
set i=0
for %%a in (%code%) do (
   set /A i+=1
   set "item[!i!]=%%~a"
)

rem Set 3rd item as url and 8th item as filename
set url=%item[3]%
set filename=%item[8]%

echo call avconv -i "%url%" -codec copy -qscale 0 %outputdir%%filename%

#2


For /f "tokens=3" %A in ("%CODE%") Do Echo %A

Which is same as

哪个是一样的

For /f "tokens=3" %A in ("avconv -i http://example.com/i/index_0_av.m3u8 -codec copy -qscale 0 video_name.mp4") Do Echo %A

I removed your quotes. Quotes only needed with spaces. URLs can't have spaces.

我删除了你的报价。报价只需要空格。网址不能包含空格。

In a batch file use %%A ratherthan %A at command prompt.

在批处理文件中,在命令提示符下使用%% A ratherthan%A。

If you need quotes see UseBackQ option where single quotes enclose string.

如果需要引号,请参阅UseBackQ选项,其中单引号括起字符串。

#3


:loop
SET /p "code=Paste the download code from your browser: "
for /f "tokens=3,8,9 delims= " %%a in ("%code%") do (
  if "%%b"=="" echo too few parameters & goto :loop
  if not "%%c"=="" echo too much parameters & goto :loop
  set "url=%%~a"
  set "filename=%%b"
)
echo %url%, %filename%

Note: the tilde in %%~a removes the quotes. Leave out the tilde, if you want to keep the quotes.

注意:%% ~a中的波浪号将删除引号。如果你想保留报价,请忽略波浪号。

Edited to implement a simple input check

编辑实现简单的输入检查