逃避参数文件批处理脚本双引号 [英] escape double quotes in param file to batch script

查看:267
本文介绍了逃避参数文件批处理脚本双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下批处理脚本:

从互联网上复制它。需要帮助按我的要求修改剧本。

Copied it from internet. Need help to modify the script per my requirement.

@ECHO OFF
IF "%~1"=="" GOTO :EOF
SET "filename=%~1"
SET fcount=0
SET linenum=0
FOR /F "usebackq tokens=1-30 delims=|" %%a IN ("%filename%") DO ^
CALL :process "%%a" "%%b" "%%c" "%%d" "%%e" "%%f" "%%g" "%%h" "%%i" "%%j" "%%k" "%%l" "%%m" "%%n" "%%o" "%%p" "%%q" "%%r" "%%s" "%%t" "%%u" "%%v" "%%w" "%%x" "%%y" "%%z" "%%?" "%%_" "%%@" "%%\"
GOTO :EOF

:trim
SET "tmp=%~1"
:trimlead
IF NOT "%tmp:~0,1%"==" " GOTO :EOF
SET "tmp=%tmp:~1%"
GOTO trimlead

:process
SET /A linenum+=1
IF "%linenum%"=="1" GOTO picknames

SET ind=0
:display
IF "%fcount%"=="%ind%" (ECHO.&GOTO :EOF)
SET /A ind+=1
CALL :trim %1
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO !tmp!
ENDLOCAL
SHIFT
GOTO display

:picknames
IF %1=="" GOTO :EOF
CALL :trim %1
SET /a fcount+=1
echo %tmp%
SET "f%fcount%=%tmp%"
SHIFT
GOTO picknames

我有我提供的参数上面的批处理脚本如下饲料文件。

I have the following feed file that I supply as parameter to the batch script above.

aa"aaaaa|bbbbbbbb|cccccc
ddddddd|eeeeeeee|ffffff
ggggggg|hhhhhhhh|iiiiii
jjjjjjj|kkkkkkkk|llllll
mmmmmmm|nnnnnnnn|oooooo

注意饲料文件有双引号。当我尝试运行批处理脚本这打破了脚本。 plz帮助。请在处理其他特殊字符也有帮助。

Notice that the feed file has double quotes. This breaks the script when I try to run the batch script. plz help. Please help in handling other special characters also.

另外,上面的脚本从其他线路把第一行不同。我想它也是治疗的第一线为任何其他线路。 plz帮助

Also, the script above treats the first line differently from other lines. I want it to treat the first line also as any other line. plz help

推荐答案

这会做你所期望的,我认为。这是一个批次的Jscript +混合脚本。标准输入和呼应到标准输出的JScript中的阅读有很多更宽容的特殊字符比纯批次。以的.bat 扩展名保存,并通过以下任何一种方法的运行它:

This'll do what you expect I think. It's a batch + Jscript hybrid script. JScript's reading of stdin and echoing to stdout are a lot more tolerant of special characters than pure batch. Save it with a .bat extension and run it via any of the following methods:

parser.bat feedfile.txt

parser.bat< feedfile.txt

键入feedfile.txt | parser.bat

feedgenerator.exe | parser.bat

parser.bat (然后粘贴内容到控制台。按的<大骨节病>输入在空行的停下来。)

下面的脚本。

@if (@a==@b) @end /*

:: batch portion

@ECHO OFF
setlocal

if exist "%~1" (
    cscript /nologo /e:jscript "%~f0" < "%~1"
) else (
    cscript /nologo /e:jscript "%~f0"
)

exit /b

:: JScript portion */

while (!WSH.StdIn.AtEndOfLine) {
    var line=WSH.StdIn.ReadLine();
    WSH.Echo(line.split('|').join('\r\n'));
    WSH.Echo();
}

输出示例:

C:\Users\me\Desktop>test.bat < test.txt
aa"aaaaa
bbbbbbbb
cccccc

ddddddd
eeeeeeee
ffffff

ggggggg
hhhhhhhh
iiiiii

jjjjjjj
kkkkkkkk
llllll

mmmmmmm
nnnnnnnn
oooooo

这似乎与百分比,&符号,克拉工作也很好,和其他任何我能想到的扔。

It seems to work just as well with percents, ampersands, carats, and whatever else I could think to throw in.

这篇关于逃避参数文件批处理脚本双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆