Pass a value from .txt to a HTML textbox (using Batch, Java, or PHP)
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)
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....
- Parse text file and look for lines that have _ABC_
- If line is false go to next line in text file (see 5th bullet), if line is true take the value upto the third underscore and capture it to be passed to the HTML text box, however convert the previous underscores to dashes and separate values by commas.
- So the text box code should appear like this with the aforementioned example:
- <input type="text" name="trackingId" size="100" value = "12-23-4, 12-36-5, 12-39-1">
- If line has _XYZ_ do the same steps but send it to the "XYZparser.html", if line has _UVW_ send it to the "UVWparser.html"
- User could change the numerics in the filelist.txt at any time but the suffixes will remain constant (ie: on a later run the user may not want to submit 12_23_4_ABC_, they may want to see 13_87_1_ABC_)
@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...
Amendment for revised specification
@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...