• 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

yanklines

53 posts in this topic

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

By the way, does anyone know how to make the script case insesitive?

0

Share this post


Link to post
Share on other sites

Could you post the part of the script that you want to make case insensitive

0

Share this post


Link to post
Share on other sites

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...

0

Share this post


Link to post
Share on other sites

The duplicate lines I'm talking about are something like these:

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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Thank you very much. Fortunately in this case it doesn't matter whether casing is consistent or not.

0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

post-5386-0-34758500-1331603692_thumb.pn

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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:

  1. yanklinesCI.vbs
  2. yanklinesCS.vbs

could be handy. :unsure:

jaclaz

0

Share this post


Link to post
Share on other sites

Done. Thanks! :thumbup

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites
: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
0

Share this post


Link to post
Share on other sites

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

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.