2009-09-10 22 views
7

Chcę otworzyć nieprawidłowy plik, skopiuj wszystkie dane i napisz do pliku tekstowego.Jak odczytać plik i zapisać w pliku tekstowym?

Mój nieprawidłowy plik.

File name - 1.mis

M3;3395;44;0;1;;20090404;094144;8193;3;0;;;; 
M3;3397;155;0;2;;20090404;105941;8193;3;0;;;; 
M3;3396;160;0;1;;20090404;100825;8193;3;0;;;; 
M3;3398;168;0;2;;20090404;110106;8193;3;0;;;; 

tak dalej ...

Powyższe dane powinny pojawić się w pliku tekstowym o tej samej nazwie pliku (1.txt).

Próbowałem tego kodu.

Dim sFileText As String 
Dim iFileNo As Integer 
iFileNo = FreeFile 
Open "C:\Clients\Converter\Clockings.mis" For Input As #iFileNo 
Do While Not EOF(iFileNo) 
Input #iFileNo, sFileText 
Loop 
Close #iFileNo 

Open "C:\Clients\Converter\2.txt" For Output As #iFileNo 
Do While Not EOF(iFileNo) 
Write #iFileNo, sFileText 
Loop 
Close #iFileNo 

Nic nie jest zapisywane w pliku 1.txt.

+1

Dobrze, jeśli plik mis sklepów jest to dane w postaci tekstu, można po prostu skopiować plik do 1.txt ... :-) –

+2

nie ma nic w 1.txt bo piszesz do 2.txt ... –

Odpowiedz

13

Jest o wiele łatwiej wykorzystać czas pracy skryptowy, który jest instalowany domyślnie w systemie Windows

Po prostu przejdź do projektu Odnośnik i sprawdź środowisko wykonawcze Microsoft Scripting i kliknij OK.

Następnie można użyć tego kodu, który jest lepiej niż domyślnego pliku poleceń

Dim FSO As FileSystemObject 
Dim TS As TextStream 
Dim TempS As String 
Dim Final As String 
Set FSO = New FileSystemObject 
Set TS = FSO.OpenTextFile("C:\Clients\Converter\Clockings.mis", ForReading) 
'Use this for reading everything in one shot 
Final = TS.ReadAll 
'OR use this if you need to process each line 
Do Until TS.AtEndOfStream 
    TempS = TS.ReadLine 
    Final = Final & TempS & vbCrLf 
Loop 
TS.Close 

Set TS = FSO.OpenTextFile("C:\Clients\Converter\2.txt", ForWriting, True) 
    TS.Write Final 
TS.Close 
Set TS = Nothing 
Set FSO = Nothing 

chodzi o to, co jest nie tak z oryginalnego kodu tutaj czytasz każdy wiersz pliku tekstowego.

Input #iFileNo, sFileText 

to tutaj piszesz go

Write #iFileNo, sFileText 

sFileText jest zmienną ciąg tak, co się dzieje jest to, że za każdym razem można przeczytać, po prostu zastąpić zawartość sFileText z treścią wiersza ty po prostu czytaj.

Kiedy więc piszesz, wszystko, co piszesz, to ostatnia linia, którą czytasz, która jest prawdopodobnie pustą linią.

Dim sFileText As String 
Dim sFinal as String 
Dim iFileNo As Integer 
iFileNo = FreeFile 
Open "C:\Clients\Converter\Clockings.mis" For Input As #iFileNo 
Do While Not EOF(iFileNo) 
    Input #iFileNo, sFileText 
sFinal = sFinal & sFileText & vbCRLF 
Loop 
Close #iFileNo 

iFileNo = FreeFile 'Don't assume the last file number is free to use 
Open "C:\Clients\Converter\2.txt" For Output As #iFileNo 
Write #iFileNo, sFinal 
Close #iFileNo 

Uwaga: nie trzeba wykonywać pętli do zapisu. sFinal zawiera pełny tekst pliku gotowy do napisania za jednym razem. Zauważ, że dane wejściowe odczytują LINIĘ na raz, więc każda linia dołączona do sFinal musi mieć dołączone CR i LF na końcu, aby mogły być poprawnie zapisane w systemie MS Windows. Inny system operacyjny może po prostu potrzebować LF (Chr $ (10)).

Jeśli potrzebujesz przetworzyć przychodzące dane, musisz zrobić coś takiego.

Dim sFileText As String 
Dim sFinal as String 
Dim vTemp as Variant 
Dim iFileNo As Integer 
Dim C as Collection 
Dim R as Collection 
Dim I as Long 
Set C = New Collection 
Set R = New Collection 

iFileNo = FreeFile 
Open "C:\Clients\Converter\Clockings.mis" For Input As #iFileNo 
Do While Not EOF(iFileNo) 
    Input #iFileNo, sFileText 
    C.Add sFileText 
Loop 
Close #iFileNo 

For Each vTemp in C 
    Process vTemp 
Next sTemp 

iFileNo = FreeFile 
Open "C:\Clients\Converter\2.txt" For Output As #iFileNo 
For Each vTemp in R 
    Write #iFileNo, vTemp & vbCRLF 
Next sTemp 
Close #iFileNo 
+0

Niektóre komputery użytkowników nie mają obiektu FileSystemObject (doświadczyłem tego). Myślę, że nadgorliwe działy IT czasami depczą skryptowe środowisko uruchomieniowe ze strachu przed wirusami. – MarkJ

+0

Tak, dlatego należy dołączyć jako część instalacji. Sysop może wtedy zdecydować, czy zrobić wyjątek dla aplikacji. Lub utwórz własną wersję, owijając natywną funkcję lub zawijaj .NET. Oba są przesadą IMO. –

2
FileCopy "1.mis", "1.txt" 
+0

Filecopy działa, Załóżmy, że chcę dodać coś w docelowym pliku tekstowym, Jak zrobić kod. – Gopal

2
An example of reading a file: 
Dim sFileText as String 
Dim iFileNo as Integer 
iFileNo = FreeFile 
'open the file for reading 
Open "C:\Test.txt" For Input As #iFileNo 
'change this filename to an existing file! (or run the example below first) 

'read the file until we reach the end 
Do While Not EOF(iFileNo) 
Input #iFileNo, sFileText 
'show the text (you will probably want to replace this line as appropriate to your program!) 
MsgBox sFileText 
Loop 

'close the file (if you dont do this, you wont be able to open it again!) 
Close #iFileNo 
(note: an alternative to Input # is Line Input # , which reads whole lines). 


An example of writing a file: 
Dim sFileText as String 
Dim iFileNo as Integer 
iFileNo = FreeFile 
'open the file for writing 
Open "C:\Test.txt" For Output As #iFileNo 
'please note, if this file already exists it will be overwritten! 

'write some example text to the file 
Print #iFileNo, "first line of text" 
Print #iFileNo, " second line of text" 
Print #iFileNo, "" 'blank line 
Print #iFileNo, "some more text!" 

'close the file (if you dont do this, you wont be able to open it again!) 
Close #iFileNo 

Od Here

+0

Zobacz, @Gopal: Above Same ** dane ** powinny pojawić się w pliku tekstowym z tym samym. On/Ona potrzebuje FileCopy. – adatapost

+0

@Khenu - W pliku źródłowym mam n liczby linii, Jak zrobić kod? – Gopal

3

Jeśli chcesz zrobić to linia po linii:

Dim sFileText As String 
Dim iInputFile As Integer, iOutputFile as integer 

iInputFile = FreeFile 
Open "C:\Clients\Converter\Clockings.mis" For Input As #iInputFile 
iOutputFile = FreeFile 
Open "C:\Clients\Converter\2.txt" For Output As #iOutputFile 
Do While Not EOF(iInputFile) 
    Line Input #iInputFile , sFileText 
    ' sFileTextis a single line of the original file 
    ' you can append anything to it before writing to the other file 
    Print #iOutputFile, sFileText 
Loop 
Close #iInputFile 
Close #iOutputFile 
Powiązane problemy