17. juni 2004 - 17:48Der er
9 kommentarer og 3 løsninger
Hente variabler fra et .txt dokument
Hvordan kan man hente variabler ud af et tekst dokument, vha. f.eks. asp eller lignende ?
I tekst dokumentet står værdierne af de variabler jeg skal bruge, roddet og kun adskilt af et ; Ved hver tiende værdi skal de to værdier indsættes i en database.
Tekst dokumentet kan se sådan her ud :
256543;Træbord;2402-10-12-20;Colli 2/2 model 5036;19890;C15161-10-50-20;wr23789;
'gemme gemmme Response.Write "test" & var1 & " " & var2 end sub
Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", 1) ReadAllTextFile = f.ReadAll dim AllArray dim smallA(10) AAr = Split(ReadAllTextFile,";") b=0 a=0 While a<>UBound(AAr)
if b=10 then savedb smallA(2),smallA(4) ' her angives hvilke af de ti værdier du er interesseret i
Eventuel en ini-fil som kan læses/skrives med følgende:
Funktionerne
Sub WriteINIString(Section, KeyName, Value, FileName) Dim INIContents, PosSection, PosEndSection
'Get contents of the INI file As a string INIContents = GetFile(FileName)
'Find section PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare) If PosSection>0 Then 'Section exists. Find end of section PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[") '?Is this last section? If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1
'Separate section contents Dim OldsContents, NewsContents, Line Dim sKeyName, Found OldsContents = Mid(INIContents, PosSection, PosEndSection - PosSection) OldsContents = split(OldsContents, vbCrLf)
'Temp variable To find a Key sKeyName = LCase(KeyName & "=")
'Enumerate section lines For Each Line In OldsContents If LCase(Left(Line, Len(sKeyName))) = sKeyName Then Line = KeyName & "=" & Value Found = True End If NewsContents = NewsContents & Line & vbCrLf Next
If isempty(Found) Then 'key Not found - add it at the end of section NewsContents = NewsContents & KeyName & "=" & Value Else 'remove last vbCrLf - the vbCrLf is at PosEndSection NewsContents = Left(NewsContents, Len(NewsContents) - 2) End If
'Combine pre-section, new section And post-section data. INIContents = Left(INIContents, PosSection-1) & _ NewsContents & Mid(INIContents, PosEndSection) else'if PosSection>0 Then 'Section Not found. Add section data at the end of file contents. If Right(INIContents, 2) <> vbCrLf And Len(INIContents)>0 Then INIContents = INIContents & vbCrLf End If INIContents = INIContents & "[" & Section & "]" & vbCrLf & _ KeyName & "=" & Value end if'if PosSection>0 Then WriteFile FileName, INIContents End Sub
Function GetINIString(Section, KeyName, Default, FileName) Dim INIContents, PosSection, PosEndSection, sContents, Value, Found
'Get contents of the INI file As a string INIContents = GetFile(FileName)
'Find section PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare) If PosSection>0 Then 'Section exists. Find end of section PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[") '?Is this last section? If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1
If InStr(1, sContents, vbCrLf & KeyName & "=", vbTextCompare)>0 Then Found = True 'Separate value of a key. Value = SeparateField(sContents, vbCrLf & KeyName & "=", vbCrLf) End If End If If isempty(Found) Then Value = Default GetINIString = Value End Function
'Separates one field between sStart And sEnd Function SeparateField(ByVal sFrom, ByVal sStart, ByVal sEnd) Dim PosB: PosB = InStr(1, sFrom, sStart, 1) If PosB > 0 Then PosB = PosB + Len(sStart) Dim PosE: PosE = InStr(PosB, sFrom, sEnd, 1) If PosE = 0 Then PosE = InStr(PosB, sFrom, vbCrLf, 1) If PosE = 0 Then PosE = Len(sFrom) + 1 SeparateField = Mid(sFrom, PosB, PosE - PosB) End If End Function
'File functions Function GetFile(ByVal FileName) Dim FS: Set FS = CreateObject("Scripting.FileSystemObject") 'Go To windows folder If full path Not specified. If InStr(FileName, ":\") = 0 And Left (FileName,2)<>"\\" Then FileName = FS.GetSpecialFolder(0) & "\" & FileName End If On Error Resume Next
GetFile = FS.OpenTextFile(FileName).ReadAll End Function
Function WriteFile(ByVal FileName, ByVal Contents)
Dim FS: Set FS = CreateObject("Scripting.FileSystemObject") 'On Error Resume Next
'Go To windows folder If full path Not specified. If InStr(FileName, ":\") = 0 And Left (FileName,2)<>"\\" Then FileName = FS.GetSpecialFolder(0) & "\" & FileName End If
Dim OutStream: Set OutStream = FS.OpenTextFile(FileName, 2, True) OutStream.Write Contents End Function
Jeg har problemer med at åbne tekstfilen, når jeg prøver at åbne siden med denne streng : set f=fs.OpenTextFile(Server.MapPath("Db2.txt"),1) går serveren helt i stå.
Siden kommer ikke med en fejlmelding eller noget, den står bare og leder efter siden, lige som når man laver et endeløs loop. Her afbryder den bare ikke, den bliver ved med at læse siden. Jeg har lavet en set f=fs.OpenTextFile på serveren før.
Hele siden ser således ud nu: (Jeg har leget lidt med det og fejl fundet en del på siden, så det meste af koden er Deaktiveret.)
Set conn=Server.CreateObject("ADODB.Connection") Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ="&Server.MapPath(DBPathRealative)&";Uid=********;Pwd=*******;" set Rs=Server.CreateObject("ADODB.recordset")
Function DelDB() Rs.open "DELETE FROM AstUK WHERE CatNO <> '' ", conn End Function
'Sorteringen set fs=Server.CreateObject("Scripting.FileSystemObject") set f=fs.OpenTextFile(Server.MapPath(TXTPathRealative),1) DelDB() Do while f.AtEndOfStream<>true
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.