使用批处理脚本传递文件名包含空格和逗号的sgm文件

时间:2015-01-13 11:07:01

标签: windows batch-file batch-processing

我有一些sgm文件,其中文件名包含空格和逗号。那么如何在命令提示符下逐行将所有sgm文件发送到转换程序?

  1. 如何在文件名中包含空格和逗号的目录中发送SGM文件列表?

  2. 如何忽略这些空格和逗号,如下所示:

  3. 示例SGM文件:

    Medco Health Solutions, Inc.  No Action, Interpretive and or Exemptive Letter of August 13, 2002.sgm
    
    MP Environmental Funding LLC, PE Environmental Funding LLC  No Action, Interpretive and or Exemptive Letter of September 19, 2007.sgm
    
    Nationwide Financial Services, Inc. and Provident Mutual Life Insurance Company  No Action, Interpretive and or Exemptive Letter of August 9, 2002.sgm
    

    我的运行规则:

    c:\Work> table2srt.bat "Medco Health Solutions, Inc.  No Action, Interpretive and or Exemptive Letter of August 13, 2002.sgm"
    

    输出

    Error cannot open Medco.tables
    

    非常感谢任何帮助!

    批次代码:(table2srt.bat)

    @echo off
    
    rem **************************************************
    rem    Check USAGE
    rem **************************************************
    if "%1" == ""  GOTO USAGE
    
    set rat=%4
    if "%4" == "" set rat=1.75
    
    set wid=%5
    if "%5" == "" set wid=78
    
    set maxw=%6
    if "%6" == "" set maxw=100
    
    
    echo.
    echo ***********************************************************
    echo Get all the tables from the file %1.sgm                   
    echo ***********************************************************
    echo.
    echo Program     : %VPISRC%\srt_tables.pl
    echo Input File  : %1.sgm
    echo Output File : %1.tables
    %OMNIDIR%\Perl\bin\perl.exe srt_tables.pl %1.sgm
    
    
    for %%F in (%1.tables) do (
    if %%~zF equ 0 goto NOTABLE
    )
    
    
    echo.
    echo *****************************************************
    echo Conversion of tables completed successfully         
    echo ***************************************************** 
    echo.
    echo *****************************
    echo Convert SGML Table to Perl
    echo *****************************
    echo   Program        : %VPISRC%\table2srt.om5
    echo   DTD            : %VPIDTD%\tabledoc.dtd
    echo   Input File     : %1.tables
    echo   Ratio          : %rat%
    echo   Ideal Width    : %wid%
    echo   Maximum Width  : %maxw%
    echo   Output File    : %1.prl
    
    echo.
    
    echo %VPIDTD%\decl.sgm              > %1.arg
    echo %VPIDTD%\tabledoc.dtd          >> %1.arg
    echo %1.tables                          >> %1.arg
    echo -s %VPISRC%\table2srt.om5          >> %1.arg
    echo -of %1.prl                     >> %1.arg
    echo -d ratio %rat%                 >> %1.arg
    echo -d max-width %maxw%            >> %1.arg
    echo -d ideal-width %wid%           >> %1.arg
    echo -d FN %1                       >> %1.arg
    echo -l %VPIDTD%/                   >> %1.arg
    echo -alog %1.log                   >> %1.arg
    echo -i %VPINCLD%\          >> %1.arg
    
    %OMNIDIR%\omnimark -f %1.arg
    
    if errorlevel 4 goto REPORT
    
    
    rem *********************************************************
    rem    Run the Perl script
    rem *********************************************************
    :PERL
    echo.
    echo Perform Perl Script....
    echo   Input File  : %1.prl
    echo   Output File : %1.srt
    echo.
    
    %OMNIDIR%\Perl\bin\perl %1.prl > %1.srt
    
    if errorlevel 1 goto BUMMER
    
    
    
    echo.
    echo Done.
    echo.
    
    rem *********************************************************
    rem    Merge SRT
    rem *********************************************************
    echo.
    echo Merge SRT Table back in
    echo   Program        : %VPISRC%\mrgsrt.om5
    echo   Input File     : %1.sgm
    echo   SRT File       : %1.srt
    echo   Output File    : %1.fnl
    echo   Log File       : %1.log
    echo.
    
    echo %1.sgm                      > %1.arg
    echo -s %VPISRC%\mrgsrt.om5    >> %1.arg
    echo -of %1.fnl                >> %1.arg
    echo -d FN %1                  >> %1.arg
    echo -log %1.log               >> %1.arg
    echo -i %VPINCLD%\      >> %1.arg
    
    %OMNIDIR%\omnimark -f %1.arg
    
    if errorlevel 1 goto BUMMER
    
    
    echo.
    echo Done.
    echo.
    
    
    rem *******************************************
    rem  SRT Cleanup 
    rem *******************************************
    echo Program     : %VPISRC%\srt_cleanup.pl
    echo Input File  : %1.fnl
    echo Output File : %1.sgm
    %OMNIDIR%\Perl\bin\perl.exe srt_cleanup.pl %1.fnl
    
    echo.
    echo *****************************************************
    echo Conversion of table to srt completed successfully   
    echo *****************************************************
    
    echo.
    echo *****************************************************
    echo  Secnal Conversion Completed with final
    echo  output file as %1.sgm  
    echo *****************************************************
    
    if exist %1.srt del %1.srt
    if exist %1.arg del %1.arg
    if exist %1.err del %1.err
    if exist %1.prl del %1.prl
    if exist %1.log del %1.log
    if exist %1.tables del %1.tables
    if exist %1.fnl del %1.fnl
    goto DONE
    
    
    
    :NOTABLE
    echo *************************************************************
    echo No Tables exists in this file  %1.sgm     
    echo *************************************************************
    echo.
    echo *****************************************************
    echo  Secnal Conversion Completed with final
    echo  output file as %1.sgm  
    echo *****************************************************
    goto DONE 
    
    :BUMMER
    echo Bummer....
    echo Check %1.log
    echo.
    goto DONE
    
    :REPORT
    echo.
    echo Bummer. Unexpected Conversion Errors.
    echo Check %1.err
    echo.
    goto DONE
    
    ::USAGE
    echo.
    echo USAGE is TABL2SRT (FILENAME) (SGM)
    echo    Optional: (RATIO) (IDEAL WIDTH) (MAXIMIM WIDTH)
    echo.
    goto DONE
    
    :DONE
    set rat=
    set wid=
    set maxw=
    

1 个答案:

答案 0 :(得分:1)

只是代码中的一个摘录。所有文件中都会出现同样的问题

%OMNIDIR%\Perl\bin\perl.exe srt_tables.pl %1.sgm


for %%F in (%1.tables) do (
if %%~zF equ 0 goto NOTABLE
)

%1包含带扩展名的带引号的文件名,因此解析器解释的是

c:\somewhere\Perl\bin\perl.exe srt_tables.pl "Medco Health .... 2002.sgm".sgm

for %%F in ("Medco Health .... 2001.sgm".tables) do (
if %%~zF equ 0 goto NOTABLE
)

应该是

"%OMNIDIR%\Perl\bin\perl.exe" srt_tables.pl "%~1"

传递完整的文件名,不带引号(此处为%~1而不是%1)并再次引用(以防万一您最初可能忘记了引号)。

perl程序属性是否会生成输出文件?使用它将正常工作,然后下一个代码应该是

for %%F in ("%~n1.tables") do (
    if %%~zF equ 0 goto NOTABLE
)

似乎(从您的代码中)perl程序生成一个名称相同但扩展名替换为.tables的文件,而不是使用%~1来检索带扩展名的名称,使用%~n1,仅检索文件名。当然,所有引用的空格都存在。

行为

echo %VPIDTD%\decl.sgm              > %1.arg

应该是

echo %VPIDTD%\decl.sgm              > "%~n1.arg"

所有批处理文件都需要进行等效更改。

进行所有更改后,命令行调用将处理所有文件

for %a in (*.sgm) do table2srt.bat "%a"