批处理脚本 - 双引号,消失行

时间:2013-12-05 11:39:45

标签: batch-file echo

我一直在寻找SO的答案,但没有找到任何决定性的解决方案。我几乎拥有一切,所以我想我错过了什么。

我正在创建一个批处理脚本,它将文件名作为参数并从中创建一个avisynth脚本。

见下文:

echo.#BEGIN------------------------------------------------------------------------- >> %~n1-timecode.avs
echo.#Found at: http://superuser.com/questions/113666/how-can-i-burn-a-timecode-into-a-movie-file >> %~n1-timecode.avs
echo.global    xPos = 10 >> %~n1-timecode.avs
echo.global    yPos = 10 >> %~n1-timecode.avs
echo.global subsize = 40 >> %~n1-timecode.avs
echo.global subfont = "Arial" >> %~n1-timecode.avs
echo.#------------------------------------------------------------------------------ >> %~n1-timecode.avs
echo.function SubtitleTime( obj ) >> %~n1-timecode.avs
echo.{  >> %~n1-timecode.avs
echo.  obj = ScriptClip( obj, "Subtitle(  >> %~n1-timecode.avs
echo.  \   String( chr(32) ) >> %~n1-timecode.avs
echo.  \ + String( RightStr( String( ((int(current_frame/Framerate)/60)/60) ), 2 ) ) >> %~n1-timecode.avs
echo.  \ + String( chr(58) ) >> %~n1-timecode.avs
echo.  \ + String( RightStr( String( String( 0 ) + String( (int(current_frame/Framerate)/60)-(((int(current_frame/Framerate)/60)/60)*60) ) ), 2 ) ) >> %~n1-timecode.avs
echo.  \ + String( chr(58) ) >> %~n1-timecode.avs
echo.  \ + String( RightStr( String( String( 0 ) + String( (int(current_frame/Framerate))-(((int(current_frame/Framerate))/60)*60) ) ), 2 ) ) >> %~n1-timecode.avs
echo.  \ + MidStr( String( (current_frame/Framerate) -  (int(current_frame/Framerate)) ), 2, 4 ) >> %~n1-timecode.avs
echo.  \ , font=subfont, size=subsize, x=xPos, y=yPos) >> %~n1-timecode.avs
echo.  \ ") >> %~n1-timecode.avs
echo.  return obj >> %~n1-timecode.avs
echo.} >> %~n1-timecode.avs
echo.#------------------------------------------------------------------------------ >> %~n1-timecode.avs
echo.DirectshowSource("%1").SubtitleTime >> %~n1-timecode.avs
echo.#END--------------------------------------------------------------------------- >> %~n1-timecode.avs

但以下两行会产生问题:

echo.  obj = ScriptClip( obj, "Subtitle(  >> %~n1-timecode.avs
echo.  \ ") >> %~n1-timecode.avs

我尝试过双引号,反斜杠,^ ......它们都不起作用 - 要么显示双引号,要么显示行消失。任何人都可以给我一个如何解决这个问题的提示?

2 个答案:

答案 0 :(得分:2)

违规行中的不平衡报价将重定向转换为字符串文字而不是重定向操作。修复原始代码的最快方法是逃避违规报价。

echo.  obj = ScriptClip( obj, ^"Subtitle(  >> %~n1-timecode.avs
echo.  \ ^") >> %~n1-timecode.avs

但是你的代码在输出的每一行的末尾都有额外的空格。

你可以像MC ND那样将重定向移到前面,这样可以安全地消除额外的空间。

请注意,如果在重定向之前删除任何空格,即使最后重定向也可以消除空间,但这在全局声明行中不起作用,因为最后的数字将被视为重定向。

但是有一个更简单的解决方案,它运行得更快,因为它只需要打开并定位输出文件的文件位置一次: - )

>> %~n1-timecode.avs (
    echo.#BEGIN-------------------------------------------------------------------------
    echo.#Found at: http://superuser.com/questions/113666/how-can-i-burn-a-timecode-into-a-movie-file
    echo.global    xPos = 10
    echo.global    yPos = 10
    echo.global subsize = 40
    echo.global subfont = "Arial"
    echo.#------------------------------------------------------------------------------
    echo.function SubtitleTime( obj )
    echo.{
    echo.  obj = ScriptClip( obj, "Subtitle(
    echo.  \   String( chr(32) )
    echo.  \ + String( RightStr( String( ((int(current_frame/Framerate)/60)/60) ), 2 ) )
    echo.  \ + String( chr(58) )
    echo.  \ + String( RightStr( String( String( 0 ) + String( (int(current_frame/Framerate)/60)-(((int(current_frame/Framerate)/60)/60)*60) ) ), 2 ) )
    echo.  \ + String( chr(58) )
    echo.  \ + String( RightStr( String( String( 0 ) + String( (int(current_frame/Framerate))-(((int(current_frame/Framerate))/60)*60) ) ), 2 ) )
    echo.  \ + MidStr( String( (current_frame/Framerate) -  (int(current_frame/Framerate)) ), 2, 4 )
    echo.  \ , font=subfont, size=subsize, x=xPos, y=yPos)
    echo.  \ ")
    echo.  return obj
    echo.}
    echo.#------------------------------------------------------------------------------
    echo.DirectshowSource("%1").SubtitleTime
    echo.#END---------------------------------------------------------------------------
)

有时您的输出可能包含可能需要转义的特殊字符。在这种情况下,做这样的事情可能更容易,避免任何逃避的需要:

>> %~n1-timecode.avs (
  for /f "tokens=* delims=:" %%L in ('findstr /b ::: "%~f0"') do echo(%%L
)

:::#BEGIN-------------------------------------------------------------------------
:::#Found at: http://superuser.com/questions/113666/how-can-i-burn-a-timecode-into-a-movie-file
:::global    xPos = 10
:::global    yPos = 10
:::global subsize = 40
:::global subfont = "Arial"
:::#------------------------------------------------------------------------------
:::function SubtitleTime( obj )
:::{
:::  obj = ScriptClip( obj, "Subtitle(
:::  \   String( chr(32) )
:::  \ + String( RightStr( String( ((int(current_frame/Framerate)/60)/60) ), 2 ) )
:::  \ + String( chr(58) )
:::  \ + String( RightStr( String( String( 0 ) + String( (int(current_frame/Framerate)/60)-(((int(current_frame/Framerate)/60)/60)*60) ) ), 2 ) )
:::  \ + String( chr(58) )
:::  \ + String( RightStr( String( String( 0 ) + String( (int(current_frame/Framerate))-(((int(current_frame/Framerate))/60)*60) ) ), 2 ) )
:::  \ + MidStr( String( (current_frame/Framerate) -  (int(current_frame/Framerate)) ), 2, 4 )
:::  \ , font=subfont, size=subsize, x=xPos, y=yPos)
:::  \ ")
:::  return obj
:::}
:::#------------------------------------------------------------------------------
:::DirectshowSource("%1").SubtitleTime
:::#END---------------------------------------------------------------------------

答案 1 :(得分:1)

@echo off

>  "%~n1-timecode.avs"    break
>> "%~n1-timecode.avs"    echo.#BEGIN-------------------------------------------------------------------------
>> "%~n1-timecode.avs"    echo.#Found at: http://superuser.com/questions/113666/how-can-i-burn-a-timecode-into-a-movie-file
>> "%~n1-timecode.avs"    echo.global    xPos = 10
>> "%~n1-timecode.avs"    echo.global    yPos = 10
>> "%~n1-timecode.avs"    echo.global subsize = 40
>> "%~n1-timecode.avs"    echo.global subfont = "Arial"
>> "%~n1-timecode.avs"    echo.#------------------------------------------------------------------------------
>> "%~n1-timecode.avs"    echo.function SubtitleTime( obj )
>> "%~n1-timecode.avs"    echo.{
>> "%~n1-timecode.avs"    echo.  obj = ScriptClip( obj, "Subtitle(
>> "%~n1-timecode.avs"    echo.  \   String( chr(32) )
>> "%~n1-timecode.avs"    echo.  \ + String( RightStr( String( ((int(current_frame/Framerate)/60)/60) ), 2 ) )
>> "%~n1-timecode.avs"    echo.  \ + String( chr(58) )
>> "%~n1-timecode.avs"    echo.  \ + String( RightStr( String( String( 0 ) + String( (int(current_frame/Framerate)/60)-(((int(current_frame/Framerate)/60)/60)*60) ) ), 2 ) )
>> "%~n1-timecode.avs"    echo.  \ + String( chr(58) )
>> "%~n1-timecode.avs"    echo.  \ + String( RightStr( String( String( 0 ) + String( (int(current_frame/Framerate))-(((int(current_frame/Framerate))/60)*60) ) ), 2 ) )
>> "%~n1-timecode.avs"    echo.  \ + MidStr( String( (current_frame/Framerate) -  (int(current_frame/Framerate)) ), 2, 4 )
>> "%~n1-timecode.avs"    echo.  \ , font=subfont, size=subsize, x=xPos, y=yPos)
>> "%~n1-timecode.avs"    echo.  \ ")
>> "%~n1-timecode.avs"    echo.  return obj
>> "%~n1-timecode.avs"    echo.}
>> "%~n1-timecode.avs"    echo.#------------------------------------------------------------------------------
>> "%~n1-timecode.avs"    echo.DirectshowSource("%1").SubtitleTime
>> "%~n1-timecode.avs"    echo.#END---------------------------------------------------------------------------