2

I have a text file with a youtube link,

text file = url.txt , which consist,

https://www.youtube.com/watch?v=Videocode

my need is, by running a batch file how to get only that Videocode as output in another text file.

For example, if am running url.bat which need to convert "https://www.youtube.com/watch?v=Videocode" into "Videocode"

I hope you understand my need. Please gave me some solutions. Thanks in advance.

  • StackOverflow is a site where people ask for help with the code they've researched, written, run and experienced problems with. We are not a site of dedicated script writers with nothing else to do at the weekends but work for free for people who haven't bothered researching or attempting to write their own code. Do some research, write some code, test it and if you encounter issues post your script content into your question [by editing it](https://stackoverflow.com/posts/48354706/edit), and provide sufficient information for us to help you fix it. – Compo Jan 20 '18 at 10:52
  • I think you need something like this : [Regular expression for youtube links](https://stackoverflow.com/questions/3717115/regular-expression-for-youtube-links) to extract your "Videocode" and you should write it with a vbscript or a powershell and not with a batch script – Hackoo Jan 20 '18 at 11:09

2 Answers2

0

Taking the question as asked literally, a text file named url.txt containing a link in the format https://www.youtube.com/watch?v=Videocode, url.bat could just contain this:

@For /F "UseBackQ Tokens=2 Delims==&" %%A In ("C:\Users\niranja\Desktop\url.txt") Do @(Echo %%A)>"output.txt"

Change the path to your url.txt, C:\Users\niranja\Desktop\, to suit; or if it is in the same location as url.bat remove that path completely. The video ID you were looking for should be written to a file named output.txt in the same directory as url.bat.

Note: If question as written does not match your real intent, take a look at the link provided by Hackoo and start putting something together yourself!

Compo
  • 36,585
  • 5
  • 27
  • 39
0

Here is an idea with a vbscript using a Regex to extract the "Videocode"

Data = "https://www.youtube.com/watch?v=Videocode" & vbCrlf &_
"http://www.youtube.com/watch?v=iwGFalTRHDA" & vbCrlf &_
"http://www.youtube.com/watch?v=iwGFalTRHDA&feature=related" & vbCrlf &_
"http://youtu.be/iwGFalTRHDA" & vbCrlf &_
"http://youtu.be/n17B_uFF4cA" & vbCrlf &_
"http://www.youtube.com/embed/watch?feature=player_embedded&v=r5nB9u4jjy4" & vbCrlf &_
"http://www.youtube.com/watch?v=t-ZRX8984sc" & vbCrlf &_
"http://youtu.be/t-ZRX8984sc"
Data_Extracted = Extract(Data,"http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?")
WScript.echo Data_Extracted
'************************************************
Function Extract(Data,Pattern)
   Dim oRE,oMatches,Match,Line
   set oRE = New RegExp
   oRE.IgnoreCase = True
   oRE.Global = True
   oRE.Pattern = Pattern
   set oMatches = oRE.Execute(Data)
   If not isEmpty(oMatches) then
       For Each Match in oMatches  
           Line = Line & Match.SubMatches(0) & vbcrlf
       Next
       Extract = Line
   End if
End Function
'************************************************

EDIT : Using an hybrid code batch with a vbscript

@echo off
Title Extract Videocode from Youtube links
Set "Tmpvbs=%temp%\Tmpvbs.vbs"
Set "InputFile=URL.txt"
Set "OutPutFile=OutPutCode.txt"
Call :Extract "%InputFile%" "%OutPutFile%"
Start "" "%OutPutFile%" & exit
::****************************************************
:Extract <InputData> <OutPutData>
(
echo Data = WScript.StdIn.ReadAll
echo Data = Extract(Data,"http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/^)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?"^)
echo WScript.StdOut.WriteLine Data
echo '************************************************
echo Function Extract(Data,Pattern^)
echo    Dim oRE,oMatches,Match,Line
echo    set oRE = New RegExp
echo    oRE.IgnoreCase = True
echo    oRE.Global = True
echo    oRE.Pattern = Pattern
echo    set oMatches = oRE.Execute(Data^)
echo    If not isEmpty(oMatches^) then
echo        For Each Match in oMatches  
echo            Line = Line ^& Match.SubMatches(0^) ^& vbcrlf
echo        Next
echo        Extract = Line
echo    End if
echo End Function
echo '************************************************
)>"%Tmpvbs%"
cscript /nologo "%Tmpvbs%" < "%~1" > "%~2"
If Exist "%Tmpvbs%" Del "%Tmpvbs%"
exit /b
::**********************************************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70