Jump to content

Recommended Posts

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.

Link to comment
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.

Link to comment
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.

Link to comment
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

Link to comment
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

Link to comment
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.

Link to comment
Share on other sites

  • 4 months later...

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
Link to comment
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
Link to comment
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

Link to comment
Share on other sites

Is there any simple way to modify yanklines to treat all lines with spaces/tabs at the beginning/end as equal (regardless of the number of spaces)?

I mean sth like this:

"abc.txt"
"abc.txt" <-spaces / tabs
"abc.txt" <-spaces / tabs
spaces-> "abc.txt"

Edited by tomasz86
Link to comment
Share on other sites

Not easily, no. This kind of thing is better done with sed. sed is unix, of course, but there are countless ports of sed for Win32. I like the cygwin version, but that needs also cygwin1.dll, so probably something based on mingw may suit you better. And sed is very powerful, so I do think you should invest the time needed to get familiar with it.

Link to comment
Share on other sites

I guess I'll need to study sed later on but at the moment I've managed to get through it using the following method.

1) This is a real-life example:


AppPatch.Help.Files=10,"Help"<-no spaces
AppPatch.Help.Files=10,"Help" <-spaces
Com.Files=11,com<-no spaces
Com.Files=11,com <-spaces
ADMT.FIles=65622<-no spaces
ADMT.FIles=65622 <-spaces

2) The cmd script:


FOR /F %%I IN ('FINDSTR/V "," 1.inf') DO ECHO>>temp.txt %%I
FOR /F tokens^=1-2^ delims^=^", %%I IN ('FINDSTR ^"^"^" 1.inf') DO ECHO>>temp.txt %%I"%%J"
FOR /F "tokens=1,2 delims=, " %%I IN ('FINDSTR "," 1.inf ^| FINDSTR/V ^"^"^"') DO ECHO>>temp.txt %%I,%%J

3) Result:


ADMT.FIles=65622<-no spaces
ADMT.FIles=65622<-no spaces
AppPatch.Help.Files=10,"Help"<-no spaces
AppPatch.Help.Files=10,"Help"<-no spaces
Com.Files=11,com<-no spaces
Com.Files=11,com<-no spaces

Link to comment
Share on other sites

I've managed to get through it using the following method.

FOR /F %%I IN ('FINDSTR/V "," 1.inf') DO ECHO>>temp.txt %%I
FOR /F tokens^=1-2^ delims^=^", %%I IN ('FINDSTR ^"^"^" 1.inf') DO ECHO>>temp.txt %%I"%%J"
FOR /F "tokens=1,2 delims=, " %%I IN ('FINDSTR "," 1.inf ^| FINDSTR/V ^"^"^"') DO ECHO>>temp.txt %%I,%%J

Not only does your 'fix' not show that your request of white space from the beginning of lines has been fulfilled you apear to have been a little carried away with a specific character, ^.

Link to comment
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.
×
×
  • Create New...