Complex question so let me try to explain....
I have a text file (named filelist.txt) with a list of values like so:
12_23_4_ABC_|
12_35_6_XYZ_|
12_36_5_ABC_|
12_37_9_UVW_|
12_38_8_XYZ_|
12_39_1_ABC_|
(pipe ( | ) is the delimiter)
I have a HTML page (named ABCparser.html) that has a text box where I can type values and then hit "Submit", however this particular page only accepts the values that prefix _ABC_
(there is another HTML page that accepts the _XYZ_
and _UVW_
), instead of me hardcoding the prefixed values in the value = ""
portion in each time on each page, is there a way to capture it from the text file (.txt) and pass it to the HTML page? I already have a JavaScript function on the page to auto-hit the "Submit" button. I would prefer to do this in Batch if possible since my main program is written in that, or if someone could help me provide it in Java or even PHP that I could call off from Batch.
The text box code:
<input type="text" name="trackingId" size="100" value = "">
There is a catch though. I need to pass cleaned up data to the HTML text box....
So objectives:
_ABC_
<input type="text" name="trackingId" size="100" value = "12-23-4, 12-36-5, 12-39-1">
_XYZ_
do the same steps but send it to the "XYZparser.html", if line has _UVW_
send it to the "UVWparser.html" @ECHO OFF
SETLOCAL enabledelayedexpansion
FOR /f "delims==" %%i IN ('set val 2^>nul') DO (SET %%i=)
FOR /f "tokens=1-4delims=_" %%a IN (filelist.txt) DO (
SET val_%%d=!val_%%d!, %%a-%%b-%%c
)
FOR /f "tokens=2,3delims=_=" %%i IN ('set val 2^>nul') DO (
SET list=%%j
SET list=!list:~2!
>%%iparser.html ECHO ^<input type="text" name="trackingId" size="100" value = "!list!"^>
ECHO generated %%i parser
)
Five-minute task. First clear out any environment variables named VAL*, grab the list and build the strings for the VALUE data in variables val_prefix
When the text file has been processed, look for VAR* in the environment, grab the part following the underscore for use in the output filename, remove the first two characters of the data item, which will be comma-space and generate the line to the file. Report the production of the HTML file - can be used to trigger whatever needs to be triggered.
Took longer to document than to program...
@ECHO OFF SETLOCAL enabledelayedexpansion FOR /f "delims==" %%i IN ('set val 2^>nul') DO (SET %%i=) FOR /f "tokens=1-5delims=_|" %%a IN (filelist.txt) DO ( IF "%%e"=="" (SET val_%%d=!val_%%d!, %%a-%%b-%%c) ELSE ( SET val_%%d_%%e=!val_%%d_%%e!, %%a-%%b-%%c) ) FOR /f "tokens=1,2delims==" %%i IN ('set val 2^>nul') DO ( SET html=%%i SET html=!html:*_=!parser.html SET list=%%j SET list=!list:~2! >!html! ECHO ^<input type="text" name="trackingId" size="100" value = "!list!"^> ECHO generated !html! )
Nearly the same as OP's version. Minor difference in the setting of %%e
, but that's a matter of style.
Simpler method of generating the HTML files though - with a bit of sneakiness. The FOR
simply splits the name from the value. Dealing with the name, it will be of the format val_something
so applying it to a variable (string operations can't be performed directly on metavariables) then !html:*_=!
will take var_something
and change all of the characters up to and including the first _
(the character or string between the *
and =
) to those characters after the =
- which in this case is nothing, so the leading ..._
is deleted (well, OK, since we know that all var..
variables will start var_
we could just as easily have used !html:~4!
- matter of general vs specific technique)
So - whether the parser.html
is added here or not is a matter of choice.
...just a matter of another approach for interested parties...
Peter's answer really helped! I did do some modifications based on some additional use cases. Here is my answer....
@ECHO ON
SETLOCAL enabledelayedexpansion
FOR /f "delims==" %%i IN ('set val 2^>nul') DO (SET %%i=)
FOR /f "tokens=1-5delims=_" %%a IN (filelist.txt) DO (
IF "%%e"=="|" (
SET val_%%d=!val_%%d!, %%a-%%b-%%c
) ELSE (
SET val_%%d_%%e=!val_%%d_%%e!, %%a-%%b-%%c
)
)
FOR /f "tokens=2-4delims=_=" %%i IN ('set val 2^>nul') DO (
IF "%%k"=="" (
SET list=%%j
SET list=!list:~2!
>%%i_parser.html ECHO ^<input type="text" name="trackingId" size="100" value = "!list!"^>
ECHO generated %%i parser
) ELSE (
SET list=%%k
SET list=!list:~2!
>%%i_%%j_parser.html ECHO ^<input type="text" name="trackingId" size="100" value = "!list!"^>
ECHO generated %%i_%%j parser
)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.