1

So, i have html file with a line like:

<script data-cfasync="false" type="text/javascript"> fid="RandonString"; v_width=620; v_height=490;</script>

My task is to find fid="RandomString" and copy everything between "" to a text file without using any external software. RandonString is 2-100 characters long.

4
  • I'd suggest using VBScript/JScript (via Windows Scripting Host) instead of BAT/CMD since you're working with a file that contains special characters. It's not that you can't do it with BAT/CMD. It's just that it'll be difficult and trouble some. Commented Oct 19, 2012 at 13:01
  • ok... Any suggestion how make it with VBScript. Commented Oct 19, 2012 at 13:06
  • Check here: technet.microsoft.com/en-us/library/ee176792.aspx Commented Oct 19, 2012 at 13:18
  • Thanks. But i only need this one script so it's bit too much to start learning from the basics. Commented Oct 19, 2012 at 14:32

1 Answer 1

4

It's not all that hard, here's a 5 line solution:

set "x=<script data-cfasync="false" type="text/javascript"> fid="RandonString"; v_width=620; v_height=490;</script>"
set "x=%x:*fid=%"
set "x=%x:";="&rem %
set  x=%x:~2%
echo %x%

An explanation of what is going on.

You have to deal with 5 special characters, the <, >, =, " in your string, and the & character used to trim trailing data.

Lines 1-3: <> are both redirection characters, so to deal with them, it's required that the whole variable be surrounded by doublequotes ("). BUT you don't want the double quotes to be added to the variable itself.

Line 1 By putting the first quote before the variable to be set ( "x=), and the second one after the data to be set (<script data-cfasync="false" type="text/javascript"> fid="RandonString"; v_width=620; v_height=490;</script>" ), the command SET recognises that the quotes are not to be included in the variable. Thus variable data with special characters can be set without errors. (Putting the quotes inside the variable data will work too, but adds 2 special characters to the variable data and makes dealing with other search & replace commands more difficult.)

Line 2 The next step is to remove everything up to and including fid, *fid matches everything up to fid, the =% replaces it with nothing.

Line 3 The next step is to to remove everything after ";, this requires a little hack. The command processor can be tricked into doing this by adding ="&rem % to the search and replace. The '=' tells the command processor to replace ;" with the following characters, but the next character is ", which makes the preceding set command a quoted command, and means that the special & character is not quoted, leaving it available to be interpreted. This essentially puts everything after the & on a separate line, and so the search and replace command replaces "; with nothing. The REM statement is there to make sure the data that comes after the matched "; is not interpreted as a command and also means that any redirection characters will be ignored.

So what the command processor sees is:

set x=="RandonString
rem "; v_width=620; v_height=490;</script>

Which sets x to ="RandonString

Line 4 Now we have a problem since %x% begins with =", and both = and " are special characters, with = being especially hard to match. But, luckily we know that the string now starts with =", so the solution is simple. We simply skip the first two characters by telling the command processor to start with the 2nd character (character 0 currently = =, character 1 currently = ", so character 2 = R). Therefore, since Line 2 removed everything (including any redirection characters) up to and including fid, and Line 3 removed everything including "; to the end of the string (including any redirection characters),
%x:~2% = RandonString. With all the redirection characters removed, the variable does not need to be quoted at all.

Line 5 Just echo's the variable x.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.