Reputation: 23
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
Upvotes: 0
Views: 239
Reputation: 24486
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
type feedfile.txt | parser.bat
feedgenerator.exe | parser.bat
parser.bat
(then paste content into the console. Hit Enter on a blank line to stop.)
Here's the script.
@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();
}
Example output:
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.
Upvotes: 2