Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Re: split textfile to more textfiles

Reply
 
Old 08-19-2009   #1 (permalink)
Eric


 
 

Re: split textfile to more textfiles

You can read/create/write files using a FileSystemObject.
http://www.devguru.com/technologies/VBScript/14048.asp
Using OpenTextFile and reading one line at a time with ReadLine, check for
your name tags to start new files.
http://www.devguru.com/technologies/VBScript/14118.asp
http://www.devguru.com/technologies/VBScript/13934.asp

"info" <info@xxxxxx> wrote in message
news:74d83$4a8c1367$5ed32112$16973@xxxxxx
Quote:

> Can anybody help me with a vbs script.
>
> I want to read a textfile. In the text file there are computernames.
> I want to split the textfile in three parts and save this to another text
> files.
>
>
> A example.
>
>
> name : computers.txt
>
> computer03
> computer04
> computer05
> computer06
>
>
>
> split to : computersplit01.txt
> computer01
> computer02
>
> split to : computersplit02.txt
> computer03
> computer04
>
> split to : computersplit03.txt
> computer05
> computer06
>
>
> Many thanks for your help
>


My System SpecsSystem Spec
Old 08-19-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: split textfile to more textfiles


"info" <info@xxxxxx> wrote in message
news:74d83$4a8c1367$5ed32112$16973@xxxxxx
Quote:

> Can anybody help me with a vbs script.
>
> I want to read a textfile. In the text file there are computernames.
> I want to split the textfile in three parts and save this to another text
> files.
>
>
> A example.
>
>
> name : computers.txt
>
> computer03
> computer04
> computer05
> computer06
>
> split to : computersplit01.txt
> computer01
> computer02
>
> split to : computersplit02.txt
> computer03
> computer04
>
> split to : computersplit03.txt
> computer05
> computer06
>
> Many thanks for your help
>
This sounds like an assignment from your scripting class. Let's have a look
at what you have so far, what the rules are for your split and where you
have a specific problem.


My System SpecsSystem Spec
Old 08-19-2009   #3 (permalink)
Eric


 
 

Re: split textfile to more textfiles

That is what I just explained, without actually writing the script for you.
If you want the new files to replace the original file, use the DeleteFile
method of the FileSystemObject at the end.

"info" <info@xxxxxx> wrote in message
news:1e77a$4a8c1bee$5ed32112$19448@xxxxxx
Quote:

> No, I want to split the orginal computer textfile in 3 different files.
>
> "Eric" <someone@xxxxxx> schreef in bericht
> news:efAkCAOIKHA.1340@xxxxxx
Quote:

>> You can read/create/write files using a FileSystemObject.
>> http://www.devguru.com/technologies/VBScript/14048.asp
>> Using OpenTextFile and reading one line at a time with ReadLine, check
>> for your name tags to start new files.
>> http://www.devguru.com/technologies/VBScript/14118.asp
>> http://www.devguru.com/technologies/VBScript/13934.asp
>>
>> "info" <info@xxxxxx> wrote in message
>> news:74d83$4a8c1367$5ed32112$16973@xxxxxx
Quote:

>>> Can anybody help me with a vbs script.
>>>
>>> I want to read a textfile. In the text file there are computernames.
>>> I want to split the textfile in three parts and save this to another
>>> text files.
>>>
>>>
>>> A example.
>>>
>>>
>>> name : computers.txt
>>>
>>> computer03
>>> computer04
>>> computer05
>>> computer06
>>>
>>>
>>>
>>> split to : computersplit01.txt
>>> computer01
>>> computer02
>>>
>>> split to : computersplit02.txt
>>> computer03
>>> computer04
>>>
>>> split to : computersplit03.txt
>>> computer05
>>> computer06
>>>
>>>
>>> Many thanks for your help
>>>
>>
>>
>
>

My System SpecsSystem Spec
Old 08-19-2009   #4 (permalink)
Pegasus [MVP]


 
 

Re: split textfile to more textfiles


"info" <info@xxxxxx> wrote in message
news:2bdda$4a8c24d4$5ed32112$27401@xxxxxx
Quote:

> here is the script.
> computers.txt must be split to 3 different files and save it to diffent
> files.
> I could make 3 same scripts,but then ik have no control over what computer
> is online or not.
> We have a customer with 25000 clients and i want data from this clients
> but FAST.
> Are the computers online Ok than save it to a 1 of the 3 files, if not
> then save it to different notonline.txt
> I want split the computers.txt to 3 files and in the future to 10 or more
> files
>
> Can you help me
In your script the file computers.txt is an input file. You use it to obtain
a list of PCs to be scanned. I am unable to see where you attempt to split
it or what rules you apply to split the file.

I also note that your script is somewhat fragile because you use relative
file names. If you want to make your script robust then you must use
absolute names that include File + Directory names:
Set oTF = oFSO.OpenTextFile("input\computers.txt",ForReading,True)
Set oFiletxt = oFilesys.CreateTextFile("Ping-LastLogon180.csv", True)
sPath = oFilesys.GetAbsolutePathName("Ping-LastLogon180.csv")

Note also that you should use the ampersand character when concatenating
strings, not the + character:
Set PingResults = GetObject("winmgmts:{impersonationLevel=impersonate}//" _
& strComputer & "/root/cimv2").ExecQuery("SELECT * FROM Win32_PingStatus "
_
& "WHERE Address = '" + strComputer + "'")


My System SpecsSystem Spec
Old 08-19-2009   #5 (permalink)
Pegasus [MVP]


 
 

Re: split textfile to more textfiles


"info" <info@xxxxxx> wrote in message
news:59a14$4a8c42b8$5ed32112$9901@xxxxxx
Quote:

> the rule i want to apply is read 10000 lines from computers.txt and save
> it to computerinput.txt and that 2 more.
>
> 10000 clients
> 10000 clients
> 5000 clients
This was almost as hard as extracting a wisdom tooth!

What you need to do is very simple, much simpler than the code you posted:
1. Read the whole file into one long string, using the ReadAll method.
2. Put the string into an array, using the Split method and vbCRLF as a
delimiter.
3. Write the first 10,000 array elements into your first text file.
4. Write the next 10,000 array elements into your second text file.
5. Write the lines up to uBound(array) into your third text file.


My System SpecsSystem Spec
Old 08-19-2009   #6 (permalink)
ekkehard.horner


 
 

Re: split textfile to more textfiles

info schrieb:
Quote:

> Can anybody help me with a vbs script.
>
> I want to read a textfile. In the text file there are computernames.
> I want to split the textfile in three parts and save this to another text
> files.
>
>
> A example.
>
>
> name : computers.txt
>
> computer03
> computer04
> computer05
> computer06
>
>
>
> split to : computersplit01.txt
> computer01
> computer02
(where do these come from?)
Quote:

>
> split to : computersplit02.txt
> computer03
> computer04
>
> split to : computersplit03.txt
> computer05
> computer06
[...]

If you *don't* use .ReadAll and Split into array(s), the code
to solve your problem is simple that the output is all you need:

=== splitTextFile: split text file ============================================
----------------- 0
Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = ".\splittextfile_§N§.txt"
Dim nSize : nSize = 5
Dim nCnt : nCnt = 0
Dim tsIn : Set tsIn = oFS.OpenTextFile( Replace( sFSpec, "§N§", Right( "0" & nCnt,
2 ) ) )
nCnt = nCnt + 1
Dim tsOut : Set tsOut = oFS.CreateTextFile( Replace( sFSpec, "§N§", Right( "0" & nCnt,
2 ) ), True )
Do Until tsIn.AtEndOfStream
If 0 = tsIn.Line Mod nSize Then
tsOut.Close
nCnt = nCnt + 1
Set tsOut = oFS.CreateTextFile( Replace( sFSpec, "§N§", Right( "0" & nCnt, 2 ) ),
True )
End If
tsOut.Writeline tsIn.Readline
Loop
tsOut.Close
tsIn.Close

Dim nCnt2
For nCnt2 = 0 To nCnt
WScript.Echo "-----------------", nCnt2
WScript.Echo oFS.OpenTextFile( Replace( sFSpec, "§N§", Right( "0" & nCnt2 , 2 ) )
).ReadAll()
Next

----------------- 1
Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = ".\splittextfile_§N§.txt"
Dim nSize : nSize = 5
Dim nCnt : nCnt = 0

----------------- 2
Dim tsIn : Set tsIn = oFS.OpenTextFile( Replace( sFSpec, "§N§", Right( "0" & nCnt,
2 ) ) )
nCnt = nCnt + 1
Dim tsOut : Set tsOut = oFS.CreateTextFile( Replace( sFSpec, "§N§", Right( "0" & nCnt,
2 ) ), True )
Do Until tsIn.AtEndOfStream
If 0 = tsIn.Line Mod nSize Then

----------------- 3
tsOut.Close
nCnt = nCnt + 1
Set tsOut = oFS.CreateTextFile( Replace( sFSpec, "§N§", Right( "0" & nCnt, 2 ) ),
True )
End If
tsOut.Writeline tsIn.Readline

----------------- 4
Loop
tsOut.Close
tsIn.Close

Dim nCnt2

----------------- 5
For nCnt2 = 0 To nCnt
WScript.Echo "-----------------", nCnt2
WScript.Echo oFS.OpenTextFile( Replace( sFSpec, "§N§", Right( "0" & nCnt2 , 2 ) )
).ReadAll()
Next

=== splitTextFile: 0 done (00:00:00) ==========================================

My System SpecsSystem Spec
Old 08-19-2009   #7 (permalink)
Pegasus [MVP]


 
 

Re: split textfile to more textfiles


"info" <info@xxxxxx> wrote in message
news:a6a38$4a8c5b11$5ed32112$1246@xxxxxx
Quote:

> Ok thanks for the input.
> Do you have a example to read the first en next 10000 array elements into
> a textfile.
You probably mean "write the first 10,000 array elements into a text file",
not "read the first 10,000". Download the helpfile script56.chm from the
Microsoft site, then look at the "CreateTextFile" and "WriteLine" methods.
You will see detailed examples that do exactly this sort of thing.


My System SpecsSystem Spec
Old 08-19-2009   #8 (permalink)
James Watkins


 
 

Re: split textfile to more textfiles

This seems a rather convoluted implementation of a simple task that could be
solved in fewer than 20 lines of VB Script code. Are you perhaps trying to
confuse the poor OP, or impress him?


"ekkehard.horner" <ekkehard.horner@xxxxxx> wrote in message
news:4a8c6441$0$32663$9b4e6d93@xxxxxx-online.net...
info schrieb:
Quote:

> Can anybody help me with a vbs script.
>
> I want to read a textfile. In the text file there are computernames.
> I want to split the textfile in three parts and save this to another text
> files.
>
>
> A example.
>
>
> name : computers.txt
>
> computer03
> computer04
> computer05
> computer06
>
>
>
> split to : computersplit01.txt
> computer01
> computer02
(where do these come from?)
Quote:

>
> split to : computersplit02.txt
> computer03
> computer04
>
> split to : computersplit03.txt
> computer05
> computer06
[...]

If you *don't* use .ReadAll and Split into array(s), the code
to solve your problem is simple that the output is all you need:

=== splitTextFile: split text file
============================================
----------------- 0
Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = ".\splittextfile_§N§.txt"
Dim nSize : nSize = 5
Dim nCnt : nCnt = 0
Dim tsIn : Set tsIn = oFS.OpenTextFile( Replace( sFSpec, "§N§",
Right( "0" & nCnt,
2 ) ) )
nCnt = nCnt + 1
Dim tsOut : Set tsOut = oFS.CreateTextFile( Replace( sFSpec, "§N§",
Right( "0" & nCnt,
2 ) ), True )
Do Until tsIn.AtEndOfStream
If 0 = tsIn.Line Mod nSize Then
tsOut.Close
nCnt = nCnt + 1
Set tsOut = oFS.CreateTextFile( Replace( sFSpec, "§N§", Right( "0"
& nCnt, 2 ) ),
True )
End If
tsOut.Writeline tsIn.Readline
Loop
tsOut.Close
tsIn.Close

Dim nCnt2
For nCnt2 = 0 To nCnt
WScript.Echo "-----------------", nCnt2
WScript.Echo oFS.OpenTextFile( Replace( sFSpec, "§N§", Right( "0" &
nCnt2 , 2 ) )
).ReadAll()
Next

----------------- 1
Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = ".\splittextfile_§N§.txt"
Dim nSize : nSize = 5
Dim nCnt : nCnt = 0

----------------- 2
Dim tsIn : Set tsIn = oFS.OpenTextFile( Replace( sFSpec, "§N§",
Right( "0" & nCnt,
2 ) ) )
nCnt = nCnt + 1
Dim tsOut : Set tsOut = oFS.CreateTextFile( Replace( sFSpec, "§N§",
Right( "0" & nCnt,
2 ) ), True )
Do Until tsIn.AtEndOfStream
If 0 = tsIn.Line Mod nSize Then

----------------- 3
tsOut.Close
nCnt = nCnt + 1
Set tsOut = oFS.CreateTextFile( Replace( sFSpec, "§N§", Right( "0"
& nCnt, 2 ) ),
True )
End If
tsOut.Writeline tsIn.Readline

----------------- 4
Loop
tsOut.Close
tsIn.Close

Dim nCnt2

----------------- 5
For nCnt2 = 0 To nCnt
WScript.Echo "-----------------", nCnt2
WScript.Echo oFS.OpenTextFile( Replace( sFSpec, "§N§", Right( "0" &
nCnt2 , 2 ) )
).ReadAll()
Next

=== splitTextFile: 0 done (00:00:00)
==========================================


My System SpecsSystem Spec
Old 08-20-2009   #9 (permalink)
Todd Vargo


 
 

Re: split textfile to more textfiles


"info" <info@xxxxxx> wrote in message
news:59a14$4a8c42b8$5ed32112$9901@xxxxxx
Quote:

> the rule i want to apply is read 10000 lines from computers.txt and save
it
Quote:

> to computerinput.txt and that 2 more.
>
> 10000 clients
> 10000 clients
> 5000 clients
This is not what you asked for below. This is how I interpret above. Are you
sure this is what you want?

Const ForReading = 1
Dim filecounter, linecounter

Set oFSO = CreateObject("scripting.filesystemobject")
Set oShell = CreateObject("Wscript.Shell")

'Opens the input file
Set oFileIn = oFSO.OpenTextFile("computers.txt", ForReading)

filecounter = 0
linecounter = 10000

Do While Not oFileIn.AtEndOfStream
If linecounter = 10000 Then
filecounter = filecounter + 1
linecounter = 0
'Opens the next output file
Set oFileOut = oFSO.CreateTextFile("output" & filecounter & ".txt",
True)
End If
linecounter = linecounter + 1
oFileOut.Writeline oFileIn.ReadLine
Loop

Quote:
Quote:

> > "info" <info@xxxxxx> wrote in message
> > news:2bdda$4a8c24d4$5ed32112$27401@xxxxxx
Quote:

> >> here is the script.
> >> computers.txt must be split to 3 different files and save it to
diffent
Quote:
Quote:
Quote:

> >> files.
> >> I could make 3 same scripts,but then ik have no control over what
> >> computer is online or not.
> >> We have a customer with 25000 clients and i want data from this clients
> >> but FAST.
> >> Are the computers online Ok than save it to a 1 of the 3 files, if not
> >> then save it to different notonline.txt
> >> I want split the computers.txt to 3 files and in the future to 10 or
more
Quote:
Quote:
Quote:

> >> files
--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

My System SpecsSystem Spec
Old 08-20-2009   #10 (permalink)
ekkehard.horner


 
 

Re: split textfile to more textfiles

James Watkins schrieb:
Quote:

> This seems a rather convoluted implementation of a simple task that could be
> solved in fewer than 20 lines of VB Script code. Are you perhaps trying to
> confuse the poor OP, or impress him?
[...]
Once again I failed with an attempt to present an idea/code/its output
in a humerous/interesting way. Yes, I thought it funny to split the
script that splits a file and showing the code two times. My apologies
for any inconvenience caused by that.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Need to get tricky with textfiles - banging my head repeatedly PowerShell
Unable to retrieve value from drop down list (values read from textfile) VB Script
how to write into a big textfile ? PowerShell
writing Textfiles PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46