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

yanklines

- - - - -

  • Please log in to reply
52 replies to this topic

#1
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,510 posts
  • OS:XP Pro x86
  • Country: Country Flag
I wonder if it's possible to run yanklines.vbs totally in background without any console windows popping up.

Here is the part of the code which is responsible for this behaviour:

'Create a persistent command prompt for the cscript output window and call the script with its original arguments    
                scriptCommand = "cmd.exe /k " & engineFolder & "cscript.exe """ & Wscript.ScriptFullName & """" & argString

By changing

cmd.exe /k
to

cmd.exe /c
I managed to get it closed as soon as it pops up. What I would like to do is to prevent any windows from being displayed while running the script.
post-47483-1123010975.png
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
allen2

allen2

    Not really Newbie

  • Member
  • PipPipPipPipPipPipPip
  • 1,812 posts
You need :
- to comment the line
RunMeWithCScript()
- to comment all lines with wscript.echo (unless you want popup for each line).
- to launch the script with wscript.exe instead of cscript if you're launching it from another script.

Edited by allen2, 08 August 2011 - 11:58 PM.


#3
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,510 posts
  • OS:XP Pro x86
  • Country: Country Flag
It's a late reply but...

as I don't know anything about VBS scripting I tried a different method:

1) I downloaded hidcon.
2) I changed

'Create a persistent command prompt for the cscript output window and call the script with its original arguments    
                scriptCommand = "cmd.exe /k " & engineFolder & "cscript.exe """ & Wscript.ScriptFullName & """" & argString
to
'Create a persistent command prompt for the cscript output window and call the script with its original arguments    
                scriptCommand = "hidcon.exe cmd.exe /k " & engineFolder & "cscript.exe """ & Wscript.ScriptFullName & """" & argString

Everything is run in background now.
post-47483-1123010975.png
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

#4
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,510 posts
  • OS:XP Pro x86
  • Country: Country Flag
By the way, does anyone know how to make the script case insesitive?
post-47483-1123010975.png
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
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag
Could you post the part of the script that you want to make case insensitive


GunSmokingMan



#6
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,510 posts
  • OS:XP Pro x86
  • Country: Country Flag
Well, I'd just like the script to remove all duplicates regardless of their case. I don't know which part of yanklines is responsible for this...
post-47483-1123010975.png
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
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,510 posts
  • OS:XP Pro x86
  • Country: Country Flag
The duplicate lines I'm talking about are something like these:

CopyFiles = System32.files
 CopyFiles = System32.Files
 CopyFiles = Fonts.Files
 CopyFiles = Fonts.files

post-47483-1123010975.png
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

#8
dencorso

dencorso

    Adiuvat plus qui nihil obstat

  • Super Moderator
  • 5,782 posts
  • OS:98SE
  • Country: Country Flag

Donator

Locate this:

Set objDict = CreateObject("Scripting.Dictionary")
j = 0
and change it thus:

Set objDict = CreateObject("Scripting.Dictionary")
objDict.CompareMode = 1 'Text i.e. case insensitive
j = 0
I'd also keep the "cmd /c" instead of "cmd /k" like you did in your first post, in addition to using hidcon.

PS: setting:

objDict.CompareMode = 0 'Binary i.e. case sensitive
would restore case sensitivity.

However, making it case insensitive does not ensure a consistent casing... Your example above results in:

CopyFiles = System32.files
CopyFiles = Fonts.Files
because always the first occurence is the one preserved, regardless of casing, so do take care.

#9
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,510 posts
  • OS:XP Pro x86
  • Country: Country Flag
Thank you very much. Fortunately in this case it doesn't matter whether casing is consistent or not.
post-47483-1123010975.png
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

#10
dencorso

dencorso

    Adiuvat plus qui nihil obstat

  • Super Moderator
  • 5,782 posts
  • OS:98SE
  • Country: Country Flag

Donator

Since I think the case-insensitive version is more useful than the previous one, I've updated my original Yanklines.VBS post, to reflect this. Thanks for calling my attention to the problem!

#11
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,036 posts
  • OS:none specified
  • Country: Country Flag

Since I think the case-insensitive version is more useful than the previous one, I've updated my original Yanklines.VBS post, to reflect this. Thanks for calling my attention to the problem!

With all due respect :), the idea of updating is generally to "better" in the sense of "adding features/choices" or correct "wrong" behaviour, wouldn't it be more desirable to have a /I command line parameter/switch (not entirely unlike the batch IF /I one)?
(or reversing the default a /CS one)?

jaclaz

#12
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag
Here is a script I wrote that will remove duplicate lines and blank lines from INI INF TXT file types. You can use this script 2 ways

1:\ Drag and Drop the file onto the script

2:\ Cmd Line

cscript %UserProfile%\Desktop\RemoveLines.vbs D:\MyTemp\Test1.Txt


This will then produce a file called New_FileName.Extension

Test File Contents

[Section 01 Test]
Line 01 Test
LINE 01 TEST
LINE 01 Test
Line 01 TEST
line 01 test
lINE 01 tEST
Line 01 Test
LINE 01 TEST
LINE 01 Test
Line 01 TEST
line 01 test
lINE 01 tEST
[Section 02 Test]
Line 02 Test

Line 02 Test
[Section 03 Test]

Line 03 Test
Line 02 Test


[Section 04 Test]

Line 04 Test
Line 03 Test

[Section 05 Test]
Line 05 Test 1

Line 05 Test 2
[Section 06 Test]



Line 06 Test
Line 03 Test


Results Of Script

[Section 01 Test]
Line 01 Test
[Section 02 Test]
Line 02 Test
[Section 03 Test]
Line 03 Test
[Section 04 Test]
Line 04 Test
[Section 05 Test]
Line 05 Test 1
Line 05 Test 2
[Section 06 Test]
Line 06 Test


Save As RemoveLines.vbs
Dim Dic :Set Dic = CreateObject("Scripting.Dictionary")
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
'-> Drag One File
  If WScript.Arguments.Count = 1 Then
'-> Process INF, INI,TXT
   Select Case LCase(Right(WScript.Arguments.Item(0),3))
    Case "inf","ini","txt"
    TextChange(WScript.Arguments.Item(0))
    Case Else 
'-> Not Correct File Type
    MsgBox "This file is not a valid file type : " & _
    Right(WScript.Arguments.Item(0),3) & vbCrLf & _
    "Drag And Drop These Types INF INI TXT",4128,_
    "Error Wrong File Type"
    WScript.Quit
   End Select 
'-> No File Drag
  ElseIf WScript.Arguments.Count = 0 Then
   ErrorMsg("Error File Total : " & WScript.Arguments.Count)
'-> To Many Files Drag
  ElseIf WScript.Arguments.Count >= 2 Then
   ErrorMsg("Error File Total : " & WScript.Arguments.Count)
  End If 
'-> Function To Handle To Many Or Zero Files
   Function ErrorMsg(N)
    MsgBox _
     "There must be only 1 Text File Type Drag And Drop" & vbCrLf & _
     "onto this script. " & N,4128, N
    WScript.Quit
   End Function
'-> Remove Duplicate Lines
   Function TextChange(File)
    Dim Ts, Tx
    Set File = Fso.GetFile(File)
    Set Ts = Fso.OpenTextFile(File.Path)
'-> 1 = Case Insensitive , 0 = Case Sensitive
    Dic.CompareMode = 1
     Do Until Ts.AtEndOfStream
      Tx = Ts.ReadLine 
      If Not Dic.Exists(Tx) Then Dic.Add Tx, Tx 
     Loop 
    Ts.Close 
    Set Ts = Fso.CreateTextFile( _
    Replace(File.Path,File.Name, "New_" & File.Name))
    For Each Tx In Dic.Keys
     If Not Tx = "" Then Ts.WriteLine Tx
    Next 
    Ts.Close
   End Function 

Sorry for the double attach file I am on Windows 8 and I can not remove one from the post,
the site not quite right on Win 8


GunSmokingMan



#13
dencorso

dencorso

    Adiuvat plus qui nihil obstat

  • Super Moderator
  • 5,782 posts
  • OS:98SE
  • Country: Country Flag

Donator


Since I think the case-insensitive version is more useful than the previous one, I've updated my original Yanklines.VBS post, to reflect this. Thanks for calling my attention to the problem!

With all due respect :), the idea of updating is generally to "better" in the sense of "adding features/choices" or correct "wrong" behaviour, wouldn't it be more desirable to have a /I command line parameter/switch (not entirely unlike the batch IF /I one)? (or reversing the default a /CS one)?

Yes, I considered that. However, I do think the case-insensitive behaviour is in line with the general behaviour of both DOS and Windows (FIND is an exception, there must be others...) and generally more useful. Now, since to restore the original case sensitive behaviour of the script is just a matter of changing a single "1" to "0" inside the script (and it's documented by a comment at that point), I decided adding a command-line parameter to control it would be an overkill, since it would entail many lines of code for a robust parsing of the command-line, which would add unneeded complexity to the code. That's just my 2 ¢, of course.

#14
CoffeeFiend

CoffeeFiend

    Coffee Aficionado

  • Super Moderator
  • 5,399 posts
  • OS:Windows 7 x64
  • Country: Country Flag
Tasks like this (or others like sorting data) is why I'm moving away from VBScript for scripting/admin/automating stuff, mainly towards PowerShell and C#. It took over 80 lines of VBScript to do, whereas in PowerShell it's a very simple one-liner:

gc in.txt|select -unique|out-file out.txt
I have nothing to say against your script (I only had a *very* quick glance). It's the scripting technology itself that's reached the point where new offerings do the job better and quicker most of the time. So I just wanted to point out how much quicker/simpler it is now.
Coffee: \ˈkȯ-fē, ˈkä-\. noun. Heaven in a cup. Life's only treasure. The meaning of life. Kaffee ist wunderbar. C8H10N4O2 FTW.

#15
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag
I really could not get the so called one line solution to work correctly
The only output I could was from my last try with Power Shell
Contents Of Test1.txt

[Section 01 Test]
Line 01 Test
LINE 01 TEST
LINE 01 Test
Line 01 TEST
line 01 test
lINE 01 tEST
Line 01 Test
LINE 01 TEST
LINE 01 Test
Line 01 TEST
line 01 test
lINE 01 tEST
[Section 02 Test]
Line 02 Test

Line 02 Test
[Section 03 Test]

Line 03 Test
Line 02 Test


[Section 04 Test]

Line 04 Test
Line 03 Test

[Section 05 Test]
Line 05 Test 1

Line 05 Test 2
[Section 06 Test]



Line 06 Test
Line 03 Test


PowerShell Output To New_Test1.txt

C:\Users\Gunsmokingman\Desktop\Test1.Txt

Attached Files




GunSmokingMan



#16
CoffeeFiend

CoffeeFiend

    Coffee Aficionado

  • Super Moderator
  • 5,399 posts
  • OS:Windows 7 x64
  • Country: Country Flag
You're missing the out-file at the end (which tells it to write to a file).

Then again it could be a one-liner in so many languages... A couple quick examples:

in C# (tested), with special thanks to the LINQ's Distinct extension method who does all of the work besides the file I/O (case insensitive -- just lose the StringComparer.CurrentCultureIgnoreCase for a case sensitive version):
File.WriteAllLines(args[1], File.ReadAllLines(args[0]).Distinct(StringComparer.CurrentCultureIgnoreCase));
or for those who still do VB in 2012 (not tested):
File.WriteAllLines(args(1), File.ReadAllLines(args(0)).Distinct(Of String)(StringComparer.CurrentCultureIgnoreCase))
Of course I'd add a couple simple guard clauses (args.Length != 2 to show usage, and File.Exist on the source file) and ideally also a try/catch block for the file IO (if this was more than a quickie) but essentially that's all of the relevant code right there.
Coffee: \ˈkȯ-fē, ˈkä-\. noun. Heaven in a cup. Life's only treasure. The meaning of life. Kaffee ist wunderbar. C8H10N4O2 FTW.

#17
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag
I did get it to work with this line
gc "C:\Users\Gunsmokingman\Desktop\Test1.Txt" | select -unique | out-file "C:\Users\Gunsmokingman\Desktop\New_Test1.Txt"

Output File Contents

[Section 01 Test]
Line 01 Test
LINE 01 TEST
LINE 01 Test
Line 01 TEST
line 01 test
lINE 01 tEST
[Section 02 Test]
Line 02 Test

[Section 03 Test]
Line 03 Test
[Section 04 Test]
Line 04 Test
[Section 05 Test]
Line 05 Test 1
Line 05 Test 2
[Section 06 Test]
Line 06 Test

For One Line it works ok, but what changes would be needed to make it case insensitive.


GunSmokingMan



#18
CoffeeFiend

CoffeeFiend

    Coffee Aficionado

  • Super Moderator
  • 5,399 posts
  • OS:Windows 7 x64
  • Country: Country Flag

what changes would be needed to make it case insensitive

More than you'd want really (I'd be using a hashset[string] instead, so more like 5 lines). That's why when the scope of the project start to fall outside of what powershell does best I switch to other tools. For example, see the C#/VB one-liners (both case insensitive) in the post directly above yours. There are plenty more options out there that don't need 80+ lines for such a simple task. My point isn't so much to solve this particular problem (yanklines already works for him, no point in me wasting time on a already solved problem) but rather that there are so many other options out there. Then again, if I was working on whatever he's doing (I believe merging inf files or somesuch) I'd look at solving the problem as a whole instead of just working on this script. I probably would have if the problem and solution were well defined.
Coffee: \ˈkȯ-fē, ˈkä-\. noun. Heaven in a cup. Life's only treasure. The meaning of life. Kaffee ist wunderbar. C8H10N4O2 FTW.

#19
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,036 posts
  • OS:none specified
  • Country: Country Flag

Yes, I considered that. However, I do think the case-insensitive behaviour is in line with the general behaviour of both DOS and Windows (FIND is an exception, there must be others...) and generally more useful. Now, since to restore the original case sensitive behaviour of the script is just a matter of changing a single "1" to "0" inside the script (and it's documented by a comment at that point), I decided adding a command-line parameter to control it would be an overkill, since it would entail many lines of code for a robust parsing of the command-line, which would add unneeded complexity to the code. That's just my 2 ¢, of course.

I see :thumbup: maybe then, having TWO files:
  • yanklinesCI.vbs
  • yanklinesCS.vbs
could be handy. :unsure:

jaclaz

#20
dencorso

dencorso

    Adiuvat plus qui nihil obstat

  • Super Moderator
  • 5,782 posts
  • OS:98SE
  • Country: Country Flag

Donator

Done. Thanks! :thumbup

#21
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag
Could you not add something like this to your script, it ask do you want it case sensitive or not case sensitive?
This would save having 2 script posted.
  If MsgBox( _
  "Would you like to make the script be Case senistive?" & vbCrLf & _
  "Examble Case Insenistive A1b, a1B, A1B are not dublicates" & vbCrLf & _
  "and only one entry would be applied" & vbCrLf & _
  "Examble Case Senistive A1b, a1B, A1B are dublicates would" & vbCrLf & _
  "be added as separate entries" & vbCrLf & _
  "Yes to make RemoveLines.vbs case Insenistive" & vbCrLf & _
  "No to make RemoveLines.vbs case senistive",4132,"Yes No senistive") = 6 Then
   '-> Case Insensitive
    Dic.CompareMode = 1
   Else
   '-> Case Sensitive
    Dic.CompareMode = 0
   End if



GunSmokingMan



#22
dencorso

dencorso

    Adiuvat plus qui nihil obstat

  • Super Moderator
  • 5,782 posts
  • OS:98SE
  • Country: Country Flag

Donator

Of course. But I think it'd be even more intrusive than adding a command-line switch to control whether it's case sensitive or not. Myself, I see case-sensitivity in yanklines more as a bug than as a feature. But I grant it may be useful in some select cases. All in all, I think the two versions of the script, as proposed by jaclaz, and which I've adopted already, by far the best solution.

#23
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,510 posts
  • OS:XP Pro x86
  • Country: Country Flag

You need :
- to comment the line

RunMeWithCScript()
- to comment all lines with wscript.echo (unless you want popup for each line).
- to launch the script with wscript.exe instead of cscript if you're launching it from another script.


I've finally managed to make the script fully silent. Thanks, allen2 :thumbup

This is what I did.

1) I commented the following lines:

WScript.Echo "usage: YankLines <input_filename> <output_filename>"
WScript.Echo("Record: " & strFirstField)
wscript.Echo "Total Records Writen:   " & objDict.count
wscript.Echo "Total Duplicates found: " & j

and then changed:

If scriptEngine = "WSCRIPT.EXE" Then
to

If scriptEngine = "CSCRIPT.EXE" Then

After doing so there's no visible output anymore.

This is a shrunken version of the whole script (all comments removed & case insensitive):

RunMeWithCScript()
If WScript.Arguments.Count <> 2 then 
WScript.Quit 
end If 
Const ForReading = 1, ForWriting = 2
Dim i, j 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInputFile = objFSO.OpenTextFile(WScript.Arguments.Item(0), ForReading)
Set objOutputFile = objFSO.OpenTextFile (WorkingDir & WScript.Arguments.Item(1), ForWriting, True)
Set objDict = CreateObject("Scripting.Dictionary")
objDict.CompareMode = 1
j = 0 
On Error Resume Next
While Not objInputFile.AtEndOfStream
arrinputRecord = split(objInputFile.Readline, "vbNewLine") 
strFirstField = arrinputRecord(0)                       
If objDict.Exists(strFirstField) then
j=j+1
Else
objDict.add strFirstField, strFirstField
End If
Wend
colKeys = objDict.Keys
For Each strKey in colKeys
objOutputFile.writeline objDict.Item(strKey)
Next
objInputFile.Close
objOutputFile.Close
Public Sub RunMeWithCScript()
Dim scriptEngine, engineFolder, Args, arg, scriptName, argString, scriptCommand
scriptEngine = Ucase(Mid(Wscript.FullName,InstrRev(Wscript.FullName,"\")+1))
engineFolder = Left(Wscript.FullName,InstrRev(Wscript.FullName,"\"))
argString = ""
If scriptEngine = "CSCRIPT.EXE" Then    
Dim Shell : Set Shell = CreateObject("Wscript.Shell")
Set Args = Wscript.Arguments
For each arg in Args
If instr(arg," ") > 0 Then arg = """" & arg & """"
argString = argString & " " & Arg
Next
scriptCommand = "cmd.exe /c " & engineFolder & "cscript.exe //U """ & Wscript.ScriptFullName & """" & argString
Shell.Run scriptCommand,,False
Wscript.Quit
Else
Exit Sub
End If
End Sub

Edited by tomasz86, 20 July 2012 - 01:42 AM.

post-47483-1123010975.png
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

#24
dencorso

dencorso

    Adiuvat plus qui nihil obstat

  • Super Moderator
  • 5,782 posts
  • OS:98SE
  • Country: Country Flag

Donator

:blink: But... but... but if you wished to have it run with wscript.exe, which is the default mode, you could as well just have deleted the first line, and all lines from "Public Sub RunMeWithCScript()" to the end -- the result would basically be the same, in 20 lines less. :angel

#25
tomasz86

tomasz86

    www.windows2000.tk

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,510 posts
  • OS:XP Pro x86
  • Country: Country Flag
As I said, I know nothing about VBS scripting :blushing:

You mean this, right?

If WScript.Arguments.Count <> 2 then 
WScript.Quit 
end If 
Const ForReading = 1, ForWriting = 2
Dim i, j 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInputFile = objFSO.OpenTextFile(WScript.Arguments.Item(0), ForReading)
Set objOutputFile = objFSO.OpenTextFile (WorkingDir & WScript.Arguments.Item(1), ForWriting, True)
Set objDict = CreateObject("Scripting.Dictionary")
objDict.CompareMode = 1
j = 0 
On Error Resume Next
While Not objInputFile.AtEndOfStream
arrinputRecord = split(objInputFile.Readline, "vbNewLine") 
strFirstField = arrinputRecord(0)                       
If objDict.Exists(strFirstField) then
j=j+1
Else
objDict.add strFirstField, strFirstField
End If
Wend
colKeys = objDict.Keys
For Each strKey in colKeys
objOutputFile.writeline objDict.Item(strKey)
Next
objInputFile.Close
objOutputFile.Close

post-47483-1123010975.png
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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users



How to remove advertisement from MSFN