Reputation: 55
I'm looking for the way to split the File A into two (File B and File C) by windows batch file or vbscript. I greatlly appreciate if you can provide the sample code !!
File A
------------------------------
'H',123,'1st'
'D',123,'1st'
'D',123,'2nd'
'D',123,'3rd'
'H',456,'2nd'
'D',456,'1st'
'D',456,'2nd'
'D',456,'3rd'
------------------------------
File B
------------------------------
'H',123,'1st'
'H',456,'2nd'
------------------------------
File C
------------------------------
'D',123,'1st'
'D',123,'2nd'
'D',123,'3rd'
'D',456,'1st'
'D',456,'2nd'
'D',456,'3rd'
------------------------------
Upvotes: 0
Views: 2323
Reputation: 912
And for completeness or educational purposes, a VBS solution.
An output stream object for each found letter is kept as the value in a dictionary.
Option Explicit
Dim fso, dic, ts_in, ts_out, s, key, INFILE, OUTDIR
Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set dic = CreateObject("scripting.dictionary")
INFILE = "c:\temp\infile.txt"
OUTDIR = Left (INFILE, InstrRev (INFILE, "\")) 'same folder as INFILE's
Set ts_in = fso.OpenTextFile (INFILE)
Do Until ts_in.AtEndOfStream
s = ts_in.ReadLine
key = Replace (Left (s, InStr (s, ",")-1), "'", "")
If Not dic.Exists (key) Then
Set ts_out = fso.CreateTextFile (OUTDIR & key & ".txt")
dic.Add key, ts_out
End If
Set ts_out = dic(key)
ts_out.writeLine s
Loop
ts_in.Close
For Each ts_out In dic.Items
ts_out.Close
Next
Upvotes: 0
Reputation: 4055
You could also do it programmatically like this:
for /f "tokens=1-3 delims=," %%a in (File_A) do (
if "%%a"=="'H'" echo %%a,%%b,%%c>>File_B
if "%%a"=="'D'" echo %%a,%%b,%%c>>File_C
)
I believe that this method will probably be slower, but it will allow you more fine tuning of your conditions or manipulation of the data without having to learn REGEX (which FINDSTR
implements poorly).
Upvotes: 0