0

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.

@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%:

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.

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

call avconv -i "%url%" -codec copy -qscale 0 %outputdir%%filename%
2
  • Btw. Apple-HLS streams are transport streams avconv -i "http://example.com/i/index_0_av.m3u8" -c copy -qscale 0 "video_name.ts" Commented May 6, 2015 at 7:28
  • And in case of -c copy you do not need -qscale. Commented May 6, 2015 at 7:35

3 Answers 3

2

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

@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%
Sign up to request clarification or add additional context in comments.

Comments

0
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.

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

Comments

0
: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.

Edited to implement a simple input check

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.