• Announcements

    • xper

      MSFN Sponsorship and AdBlockers!   07/10/2016

      Dear members, MSFN is made available via subscriptions, donations and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, become a site sponsor and ads will be disabled automatically and by subscribing you get other sponsor benefits.
tomasz86

Exclamation mark and delayed expansion problem

17 posts in this topic

SETLOCAL ENABLEDELAYEDEXPANSION

FOR /F %%A IN ("!") DO (

SET Line=%%A

SET Line=!Line:^^!={#}!

ECHO !Line!

)

Is there a simple way to make it work? Edited by tomasz86
0

Share this post


Link to post
Share on other sites
SETLOCAL ENABLEDELAYEDEXPANSIONFOR /F %%A IN ("!") DO (	SET Line=%%A	SET Line=!Line:^^!={#}!	ECHO !Line!)
Is there a simple way to make it work?
WHAT EXACTLY do you actually EXPECT that piece of batch to do? :unsure:

If you prefer what do you apply that batch to?

And what you would want to have returned by the batch?

jaclaz

0

Share this post


Link to post
Share on other sites

SETLOCAL ENABLEDELAYEDEXPANSIONFOR /F %%A IN ("!") DO (	SET Line=%%A	SET Line=!Line:^^!={#}!	ECHO !Line!)
Is there a simple way to make it work?

Yes there is; don't enable delayed expansion!

SETLOCAL DISABLEDELAYEDEXPANSIONFOR %%A IN (!) DO (	SET Line=%%A	CALL SET Line=%%Line:!={#}%%	CALL ECHO=%%Line%%)
0

Share this post


Link to post
Share on other sites

I've done it this way:

FOR /F %%A IN ("!") DO (	CALL :FIX %%A)PAUSEEXIT:FIXSET Line=%1SET Line=%Line:!={#}%ECHO %Line%GOTO :EOF:EOF
So I guess it's impossible with delayed expansion enabled? Using CALL is extremely slow when processing hundreds of lines like that.
0

Share this post


Link to post
Share on other sites

As usual and as jaclaz alluded to you have not provided us with the genuine use for your script.

There is no way at all that the file you have posted is what you are actually doing! You have decided that you know better than the people you are asking for help, and that is both incorrect and rude.

0

Share this post


Link to post
Share on other sites

I'm sorry for not providing the actual file but I don't think it will really change anything. I'm processing a HTML file and I wanted to replace all special characters with {#}, {##}, etc. This was the original script I tried to use:

SETLOCAL ENABLEDELAYEDEXPANSIONFOR /F "delims=" %%A IN (index.html) DO (	SET Line=%%A	SET Line=!Line:{#}=^&!	SET Line=!Line:{##}=^?!	SET Line=!Line:{###}=^<!	SET Line=!Line:{####}=^>!	SET Line=!Line:{#####}=^^!!	SET Line=!Line:{######}=^|!	ECHO !Line!)
but I encountered the problem just in the very beginning with the first line of the file which is:

<!DOCTYPE HTML>

0

Share this post


Link to post
Share on other sites

So are you trying to suggest now that the file you've just posted is what you are actually using?

I would strongly suggest that is not the case

0

Share this post


Link to post
Share on other sites

My two cents worth, VBS would most like be better at processing the HTML line by line without the worry of special characters.

0

Share this post


Link to post
Share on other sites

GSM, you are very correct.

You may notice that the file they are pretending to be their finished product is only a small portion of a much larger script. The characters they are replacing are only those which may pose further parsing in a batch file problems. I would be certain based upon the previous help they have received that the intention is to run this routine through to a holding file perform several other batch parsing operations on it then attempt to return the original characters back in later.

Despite being aware VBS and small executables such as SED and GSAR and perhaps even lesser known ones like John Stockton's SUBS, tomasz86 is a windows 2000 user and is therefore unable to accept the easy way. :whistle:

0

Share this post


Link to post
Share on other sites

For processing of HTML characters I would choose to use regular expressions. You can use vbscript for this or any other scripting engine like perl or AutoIT.

If you insist on using a batch file you better start writing the script from scratch. Copying from online examples can help you, but you need to understand each line of code.

Edited by Acheron
0

Share this post


Link to post
Share on other sites

If he was to post the full script that he is using I probably might post the VBS equivalent.
Example VBS Script To Process A Text Line By Line

Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")'-> File To Work WithDim Text :Text = "Demo Count Down.hta"'-> Check To See If File Exists  If Fso.FileExists(Text) Then   ReadListText(Fso.GetFile(Text))  Else     WScript.Echo "Missing : " & Text  End If '-> Function To Process Line By Line Text Files  Function ReadListText(T)   Dim i, j, Ts    Set Ts = Fso.OpenTextFile(T.Path, 1)   i = Ts.ReadAll       For Each j In Split(i,vbCrLf)       WScript.Echo j      Next    End Function
0

Share this post


Link to post
Share on other sites

So are you trying to suggest now that the file you've just posted is what you are actually using?

I would strongly suggest that is not the case

There's no larger script this time. The script posted above is the only one I tried to use on the HTML file but then realised that it didn't work with exclamation marks. That's why I posted the question :)

Once again, this is a completely different thing than the other script about merging updates. It's got nothing to do with that. I'm writing a script to process a HTML file in order to remove some parts from it. I've got no other issues with it. I just wanted to ask whether it was possible to process exclamation marks with delayed expansion enabled but it seems that the simplest way to solve the problem will be just disable it and use CALL. Anyhow, thank you for the answer (post #3).

0

Share this post


Link to post
Share on other sites

So are you trying to suggest now that the file you've just posted is what you are actually using?

I would strongly suggest that is not the case

There's no larger script this time. The script posted above is the only one I tried to use on the HTML file but then realised that it didn't work with exclamation marks. That's why I posted the question :)

Once again, this is a completely different thing than the other script about merging updates. It's got nothing to do with that. I'm writing a script to process a HTML file in order to remove some parts from it. I've got no other issues with it. I just wanted to ask whether it was possible to process exclamation marks with delayed expansion enabled but it seems that the simplest way to solve the problem will be just disable it and use CALL. Anyhow, thank you for the answer (post #3).

The script you posted echoes replacements to the console window only, it most certainly does not remove parts of the html file. A html file without its opening and closing tag symbols isn't really a html file.

You asked for help with a specific problem when the problem you had was something different. I gave you a solution to replace a single character in a single character string with three specific characters. You then rejected my solution because you'd decided not to inform us that you were intending to replace several different characters in an entire file with strings of varying lengths. Our Members do not have to be rocket scientists to see that the characters you were intending to replace were those which pose problems to batch script parsing, and that the process you posted if not currently part of a larger script is intended to be so.

You are thinking through a task in small pieces without first looking at the entire picture. Whilst it may not be part of the merging .inf files topic, it certainly has the hall marks of taking light years to achieve a goal which could have been achieved quickly with the full information from the start.

0

Share this post


Link to post
Share on other sites

I didn't ask for help with removing parts from the HTML file because I can do it myself. The only problem I had was that exclamation marks were absent from the final file due to delayed expansion enabled, and that's why I asked the original question. You actually gave the answer already which is:

Yes there is; don't enable delayed expansion!

and I modified the script so that it works with delayed expansion disabled. I was just curious whether it was possible to somewhat escape the exclamation mark while leaving delayed expansion enabled.

Edited by tomasz86
0

Share this post


Link to post
Share on other sites

To me it is still the SAME issue already seen on the good ol' "merge and join" thread. :unsure:

In there I used for simplicity a pre (and post) processing with gsar for substituting characters that created issues in the batch processing.

I don't see in which way this is different.

In my perverted mind, if you can do without an external tool/app, that is very good :), but in this specific case a small .exe like gsar is better (IMHO) than any complexity in batch code and once you have it anyway it is better to re-use it as much as you can (economy of scale ;), the cost - in bytes - of the tool is amortized faster).

jaclaz

0

Share this post


Link to post
Share on other sites

The only problem I had was that exclamation marks were absent from the final file due to delayed expansion enabled, and that's why I asked the original question.

And there we are; the question you asked had nothing to do with a file, your posted scripts do not read from nor do they write to a file.

Do not presume to know more than those off whom you require help, not only is it rude and prevents those people from providing you further help in future, it is also wrong, (your knowledge of the subject is unlikely to ever reach the level of some of our Members).

BTW - just in case you are interested, here is a vbscript, place it along side your INDEX.HTML and double click it, the resulting REPLACED.TXT should be what you required.

Const ForReading = 1, ForWriting = 2strScriptFile = Wscript.ScriptFullNameSet objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.GetFile(strScriptFile)strFolder = objFSO.GetParentFolderName(objFile)Set objFile = objFSO.OpenTextFile(strFolder & "\INDEX.HTML", ForReading)Do    strLine = strLine & objFile.ReadLine & vbCrLf       Loop While Not objFile.AtEndOfStreamobjFile.ClosestrLine = Left(strLine, Len(strLine) - 2)strLine = replace_char(strLine)If Not (objFSO.FileExists(strFolder & "\REPLACED.TXT")) Then    objFSO.CreateTextFile(strFolder & "\REPLACED.TXT")End IfSet objFile = objFSO.OpenTextFile(strFolder & "\REPLACED.TXT", ForWriting)objFile.Write(strLine)objFile.CloseFunction replace_char(strRemove)    Dim arrWrapper(1)    Dim arrReplace(5)    Dim arrReplaceWith(5)    arrWrapper(0) = arrReplace    arrWrapper(1) = arrReplace       ' Replace    arrWrapper(0)(0) = "&"    arrWrapper(0)(1) = "?"    arrWrapper(0)(2) = "<"    arrWrapper(0)(3) = ">"    arrWrapper(0)(4) = "!"    arrWrapper(0)(5) = "|"        ' With    arrWrapper(1)(0) = "{#}"    arrWrapper(1)(1) = "{##}"    arrWrapper(1)(2) = "{###}"    arrWrapper(1)(3) = "{####}"    arrWrapper(1)(4) = "{#####}"    arrWrapper(1)(5) = "{######}"    For N = 0 To 5        strRemove = Replace(strRemove, arrWrapper(0)(N), arrWrapper(1)(N), 1, -1, 0)    Next    replace_char = strRemoveEnd Function
0

Share this post


Link to post
Share on other sites

Yzöwl excellent script.

Here an modified script that I posted earlier, I made it remove all my VBS comments in my HTA

for the demo.

Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")'-> File To Work WithDim Text :Text = "Demo Count Down.hta"'-> Check To See If File Exists  If Fso.FileExists(Text) Then   ReadListText(Fso.GetFile(Text))  Else     WScript.Echo "Missing : " & Text  End If '-> Function To Process Line By Line Text Files  Function ReadListText(T)   Dim i, j, Ts, V1    Set Ts = Fso.OpenTextFile(T.Path, 1)   i = Ts.ReadAll       For Each j In Split(i,vbCrLf)'-> Skips Comment Line For Output    If Not Left(j,3) = "'->" Then      WScript.Echo j    End If      Next    End Function

Here is the code in HTA to change

<SCRIPT LANGUAGE='VBScript'>'-> Resize And Place In Approx Center Of Screen Dim Wth, Hht :Wth = int(401) :Hht = int(155)  window.ResizeTo Wth, Hht  MoveTo ((Screen.Width / 2) - (Wth / 2)),((Screen.Height / 2) - (Hht / 2))'-> Varibles For Runtime  Dim S1, Tm1'-> Window Onload    Function Window_OnLoad()'-> Set The Timer At 90   S1 = 90   Counter()  End Function '-> Counter For Count Down  Function Counter()    If S1 = 0 Then'-> Code Here For Action When Timer Reaches Zero        window.clearTimeout(Tm1)     Tx1.innerHTML="000"    ' CreateObject("Wscript.Shell").Run("shutdown.exe -s -f -t 00"),0,False     CreateObject("Wscript.Shell").Run("shutdown.exe -r -f -t 01"),0,False     window.close()    End If'-> Add Zero To Count Down    If Len(S1) = 1 Then S1 = "00" & S1    If Len(S1) = 2 Then S1 = "0" & S1   Tx1.style.color="#9A0000" :Tx1.innerHTML=S1   S1 = S1 - 1   Tm1=window.setTimeout("Counter()",1000,"VBScript")  End Function  </SCRIPT>
Script output with comments removed

<SCRIPT LANGUAGE='VBScript'> Dim Wth, Hht :Wth = int(401) :Hht = int(155)  window.ResizeTo Wth, Hht  MoveTo ((Screen.Width / 2) - (Wth / 2)),((Screen.Height / 2) - (Hht / 2))  Dim S1, Tm1  Function Window_OnLoad()   S1 = 90   Counter()  End Function   Function Counter()    If S1 = 0 Then     window.clearTimeout(Tm1)     Tx1.innerHTML="000"    ' CreateObject("Wscript.Shell").Run("shutdown.exe -s -f -t 00"),0,False     CreateObject("Wscript.Shell").Run("shutdown.exe -r -f -t 01"),0,False     window.close()    End If    If Len(S1) = 1 Then S1 = "00" & S1    If Len(S1) = 2 Then S1 = "0" & S1   Tx1.style.color="#9A0000" :Tx1.innerHTML=S1   S1 = S1 - 1   Tm1=window.setTimeout("Counter()",1000,"VBScript")  End Function  </SCRIPT>
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.