Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

Exclamation mark and delayed expansion problem

- - - - - batch

  • Please log in to reply
16 replies to this topic

#1
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • Joined 27-November 10
  • OS:XP Pro x86
  • Country: Country Flag
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, 27 July 2013 - 09:45 AM.

Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages


How to remove advertisement from MSFN

#2
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,562 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

SETLOCAL ENABLEDELAYEDEXPANSION

FOR /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

#3
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,540 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator



SETLOCAL ENABLEDELAYEDEXPANSION

FOR /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 DISABLEDELAYEDEXPANSION

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


#4
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • Joined 27-November 10
  • OS:XP Pro x86
  • Country: Country Flag
I've done it this way:
FOR /F %%A IN ("!") DO (
	CALL :FIX %%A
)
PAUSE
EXIT

:FIX
SET Line=%1
SET 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.
Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#5
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,540 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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.



#6
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • Joined 27-November 10
  • OS:XP Pro x86
  • Country: Country Flag
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 ENABLEDELAYEDEXPANSION

FOR /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>


Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#7
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,540 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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



#8
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,419 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag
My two cents worth, VBS would most like be better at processing the HTML line by line without the worry of special characters.


GunSmokingMan



#9
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,540 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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:



#10
Acheron

Acheron

    Friend of MSFN

  • Member
  • PipPipPipPipPip
  • 988 posts
  • Joined 28-June 04
  • OS:XP Pro x86
  • Country: Country Flag

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, 27 July 2013 - 01:42 PM.

Say no to bloatware. Download Nero Lite!

#11
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,419 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag

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 With
Dim 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



GunSmokingMan



#12
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • Joined 27-November 10
  • OS:XP Pro x86
  • Country: Country Flag

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).
Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#13
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,540 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

 

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.



#14
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,520 posts
  • Joined 27-November 10
  • OS:XP Pro x86
  • Country: Country Flag
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, 28 July 2013 - 01:18 AM.

Posted Image
Unofficial Service Pack 5.2 for MS Windows 2000 <- use this topic if you need help with UURollup, Update Rollup 2 and other unofficial packages

#15
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,562 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag
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

#16
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,540 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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 = 2

strScriptFile = Wscript.ScriptFullName
Set 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.AtEndOfStream
objFile.Close
strLine = Left(strLine, Len(strLine) - 2)
strLine = replace_char(strLine)
If Not (objFSO.FileExists(strFolder & "\REPLACED.TXT")) Then
    objFSO.CreateTextFile(strFolder & "\REPLACED.TXT")
End If
Set objFile = objFSO.OpenTextFile(strFolder & "\REPLACED.TXT", ForWriting)
objFile.Write(strLine)
objFile.Close

Function 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 = strRemove
End Function


#17
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,419 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag
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 With
Dim 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>



GunSmokingMan







Also tagged with one or more of these keywords: batch

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users