1

I want to set "var3=%(title)s.%(ext)s" But windows keeps on interpreting %(title)s.%(ext)s as (ext)s instead of %(title)s.%(ext) is there a way to "set" in a literal way without cmd interpreting it?

EDIT Somebody wanted to see the whole script

cd C:\youtube-dl
youtube-dl -U 
echo Remember to press enter only AFTER update is complete
pause
set /p "var1=Enter URL: " %=% pause
set "var2=%date:/=-%" 
set /p var3=%(title)s.%(ext)s
youtube-dl "%var1%" -ci -o C:\Users\Admin\Desktop\Music\Download_dl\%var2%\%var3% -f best -x --    no-mtime --add-metadata --write-thumbnail  
cd C:\Users\Admin\Desktop\Music\Download_dl\%var2%
del *.mp4

2 Answers2

2

In batch files, % has special meanings. In your code

set "var3=%(title)s.%(ext)s"
          ^.........^

the indicated percent signs are delimiting the name of a variable called (title)s.. And this variable is not defined (i suppose from your output), so what you get is

set "var3=(ext)s"

You need to escape the percent signs to get the desired result

set "var3=%%(title)s.%%(ext)s"
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • This is what happens when i use that [Input] C:\Users\Admin>set "var3=%%(title)s.%%(ext)s" C:\Users\Admin>%var3% '%%' is not recognized as an internal or external command, operable program or batch file. – user3407161 Mar 11 '14 at 17:32
  • @user3407161 Please show your whole script. Are you just calling `%var3%` as a command? If you want to display its value you need to use `echo %var3%`; otherwise, the command line is trying to execute the value of `%var3%`. – David Ruhmann Mar 11 '14 at 18:36
  • @user3407161, the code posted is intended for usage in batch files. From command line, the double percentage signs are not needed. Anyway, in the new posted code, changin your `set /p var3=...` into `set "var3=%%(title)s.%%(ext)s"` and changing to `-o "c:\...."` (with quotes) should solve the problems. – MC ND Mar 11 '14 at 19:07
  • @DavidRuhmann I'm trying to use it as a part of **bold** C:\Users\Admin\Desktop\Music\Download_dl\%var2%\%var3% – user3407161 Mar 11 '14 at 19:18
1

To preserve most special characters in batch, enclose them within quotations. This will prevent most from being lost or translated into their special meaning. However, for % in batch files, it must be escaped by itself %%.

@echo off
setlocal
cd C:\youtube-dl
youtube-dl -U
echo Remember to press enter only AFTER update is complete
pause
set /p "var1=Enter URL: " %=% pause
if defined var1 set "var1=%var1:"=%"
set "var2=%date:/=-%"
set "var3=%%(title)s.%%(ext)s"
youtube-dl "%var1%" -ci -o "C:\Users\Admin\Desktop\Music\Download_dl\%var2%\%var3%" -f best -x --    no-mtime --add-metadata --write-thumbnail  
cd "C:\Users\Admin\Desktop\Music\Download_dl\%var2%"
del *.mp4
endlocal

Changes

  • scoped variables with setlocal/endlocal
  • removed poison character quotation from user input
  • enclosed literal strings in quotations
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
  • Why did you "@echo off" "setlocal" and "if defined var1 set "var1=%var1: "=% though? Sorry i'm new to this... But the script works now – user3407161 Mar 11 '14 at 20:17
  • The `@echo off` prevents the commands from displaying when running, `setlocal` is good practice to limit your variables to your script and not affect the current running session of the command line, and `if defined var1 set "var1=%var1:"=%"` removes double quotation characters (if any) from the user input to prevent a script crash. – David Ruhmann Mar 11 '14 at 20:21
  • Sorry i didn't quite understand how the if defined... function worked – user3407161 Mar 11 '14 at 21:04
  • 1
    @user3407161 `if defined var1` checks to see if the variable is empty, `set "var1=%var1:"=%"` sets the variable var1 to the value of the expression, `%var1:"=%` does a character replace on the value of var1 replacing double quotations for nothing `"=` (blank after the equal). Summary: if var1 is not empty, remove all double quotations and save it back into var1. – David Ruhmann Mar 11 '14 at 21:10