0

I have the following batch script:

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

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.

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

4

1 回答 1

2

这会做你期望我想的。这是一个批处理 + Jscript 混合脚本。与纯批处理相比,JScript 对标准输入的读取和对标准输出的回显对特殊字符的容忍度要高得多。.bat使用扩展名保存它并通过以下任何方法运行它:

parser.bat feedfile.txt

parser.bat < feedfile.txt

type feedfile.txt | parser.bat

feedgenerator.exe | parser.bat

parser.bat (然后将内容粘贴到控制台中。点击Enter空白行停止。)

这是脚本。

@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

它似乎也适用于百分比、& 符号、克拉以及我能想到的任何其他内容。

于 2013-04-12T13:09:32.330 回答