Avatar billede kipolaks Nybegynder
13. maj 2003 - 18:49 Der er 6 kommentarer og
1 løsning

skrive til ini filer....

hejsa... jeg sidder og roder med redigering af ini filer i DOS.... jeg har et program der kan gøre det.. MEN.. HVIS JEG SKRIVER EN VÆRDI TIL EN INI FIL BLIVER DET MED STORE BOGSTAVER, OG DET BLIVER MAN IDIOT AF AT KIGGE PÅ!! :)

jeg har sourcecode til det.... er der nogen der lige gidder at tage et kig ?? :)

SETINI.BAS

DECLARE SUB Parse (CommandLine$, File$, Section$, Line$, Value$)
DECLARE FUNCTION GetToken$ (Working$, Switch$)
'----------------------------------------------------------------
'Initial setup, get needed strings from command line
CommandLine$ = COMMAND$
IF ((LEN(CommandLine$) = 0) OR (INSTR(CommandLine$, "/?"))) THEN GOTO HELP
CALL Parse(CommandLine$, File$, Section$, Line$, Value$)
ON ERROR GOTO SCREWED  'generally due to a bad file name
OPEN File$ FOR INPUT AS #1

'open separate temporary output file to write each modification to
'this will put it in the %TEMP% subdirectory, or in the root if no %TEMP%
Temp$ = ENVIRON$("TEMP") + "\" + "$setini$.tmp"
OPEN Temp$ FOR OUTPUT AS #2


'----------------------------------------------------------------
'Get to the desired section
DO
        IF EOF(1) THEN
                PRINT #2, "[" + Section$ + "]"
                EXIT DO
        ELSE
                LINE INPUT #1, Read$
                PRINT #2, Read$
                'look for section name surrounded by brackets, allow for spaces
                IF LEFT$(LTRIM$(UCASE$(Read$)), LEN(Section$) + 2) = "[" + Section$ + "]" THEN EXIT DO
                IF LEFT$(LTRIM$(UCASE$(Read$)), LEN(Section$) + 4) = "[ " + Section$ + " ]" THEN EXIT DO
        END IF
LOOP



'----------------------------------------------------------------
'Get to the desired line
DO
        IF EOF(1) THEN
                PRINT #2, Line$; "="; Value$
                EXIT DO
        ELSE
                'read
                LINE INPUT #1, Read$
             
                'test for end of section
                IF ((LEN(LTRIM$(Read$)) = 0) OR (LEFT$(LTRIM$(Read$), 1) = "[")) THEN
                        PRINT #2, Line$; "="; Value$
                        PRINT #2, Read$
                        EXIT DO
                END IF
     
                'look for line name. Allow for "illegal" leading spaces
                IF LEFT$(LTRIM$(UCASE$(Read$)), LEN(Line$) + 1) = Line$ + "=" THEN
                        PRINT #2, LEFT$(Read$, INSTR(Read$, "=")); Value$
                        EXIT DO
                ELSE
                        PRINT #2, Read$
                END IF
        END IF
LOOP



'----------------------------------------------------------------
'now do the rest of the file
DO WHILE NOT EOF(1)
        LINE INPUT #1, Read$
        PRINT #2, Read$
LOOP

CLOSE #1
CLOSE #2
SHELL "copy " + Temp$ + " " + File$ + " > nul"
KILL Temp$
GOTO done



'----------------------------------------------------------------
'--------------------    SUBROUTINES    -----------------------
'----------------------------------------------------------------
SCREWED:
PRINT
SELECT CASE ERR
CASE 52, 64
        PRINT "SETINI Error: Bad File Name"
CASE 53
        PRINT "SETINI Error #53: File Not Found"
CASE 61
        PRINT "SETINI Error #61: Disk Full"
CASE 70
        PRINT "SETINI Error #70: File Access Permission Denied"
CASE 71, 72
        PRINT "SETINI Error: Disk Error"
CASE 73
        PRINT "SETINI Error #73: Come on, go buy a decent version of DOS !"
CASE 75, 76
        PRINT "SETINI Error: Path Not Found"
CASE ELSE
        PRINT "SETINI: An unexpected error has occurred. Program terminated !"
        PRINT "Error is QB45 error number #"; ERR
        PRINT "How'd this happen ??!"
END SELECT
PRINT
GOTO done

'----------------------------------------------------------------
HELP:
PRINT
PRINT "SETINI  - DOS batchfile utility will add or change a value in an .INI - type"
PRINT "          file. Allows batchfiles to store & modify unlimited variables."
PRINT "USEAGE  - SETINI FileName Section Line [Value]"
PRINT "          FileName  Name of the .INI file to be modified."
PRINT "          Section    String indicating the .INI section."
PRINT "          Line      String on the left side of the equals sign."
PRINT "          Value      String to be put on the right side of the equals sign."
PRINT "NOTES  -  Spaces are allowed in Value. All items written to .INI file will"
PRINT "          be capitalized. SETINI is case-insensitive. SETINI will create Line"
PRINT "          and Section if they do not exist. Omit Value for nul case."
PRINT "          The file (FileName) must be organized like a WINDOWS "; CHR$(34); "INI"; CHR$(34); " file,"
PRINT "          but may use any actual name and extension (.DAT, .CFG, etc.)"
PRINT "EXAMPLE - This batch code would set the BorderWidth line in the"
PRINT "          [windows] section of the \windows\win.ini file to 10."
PRINT "          SETINI \WINDOWS\WIN.INI WINDOWS BORDERWIDTH 10"
PRINT "          The following does the same thing using environment variables:"
PRINT "          SET TACOS=10"
PRINT "          SETINI \WINDOWS\WIN.INI WINDOWS BORDERWIDTH %TACOS%"
PRINT "          SET TACOS="
PRINT
PRINT "          Eric Phelps Sacramento,CA 1994              DISTRIBUTE FREELY"
PRINT


'----------------------------------------------------------------
done:
END

FUNCTION GetToken$ (Working$, Switch$)
' returns the first string (quoted or not) as GetToken$
' trims that first string off of the input Working$
' uses a single character Switch$ to delimit the first "token" in Working$

'----------------------------------------------------------------
'What if Working$ has been trimmed too many times already?
IF Working$ = "" THEN
        GetToken$ = ""
        EXIT FUNCTION
END IF


'----------------------------------------------------------------
'get one "token"
Length% = INSTR(2, Working$, Switch$)
IF Length% = 0 THEN
        'we are down to the last token because there are no more switches!
        GetToken$ = Working$
        Working$ = ""
        EXIT FUNCTION
END IF
IF Switch$ = " " THEN
        'normal way of getting a space-delimited token
        GetToken$ = LEFT$(Working$, Length% - 1)
ELSE
        'Switch$ must be a quote (or something else)
        GetToken$ = MID$(Working$, 2, Length% - 1)
END IF


'----------------------------------------------------------------
'trim the Working$ so it no longer contains the token we just got
Working$ = RIGHT$(Working$, (LEN(Working$) - Length%))
Working$ = LTRIM$(Working$)
END FUNCTION

SUB Parse (CommandLine$, File$, Section$, Line$, Value$)
' real simple: GetToken$will identify a text string "token" which is
' delimited by the "switch" ( in this case it is always a " " space )
' GetToken$ will remove each token as it finds it, so CommandLine$ will
' always have the next token up front.
'----------------------------------------------------------------
'get the first token
File$ = GetToken$(CommandLine$, " ")

'----------------------------------------------------------------
'get the second token
Section$ = GetToken$(CommandLine$, " ")

'----------------------------------------------------------------
'get the third token
Line$ = GetToken$(CommandLine$, " ")

'----------------------------------------------------------------
'get the fourth token
'this is too easy: it's the leftovers! This way it can have embedded spaces!
Value$ = CommandLine$

END SUB
Avatar billede kipolaks Nybegynder
13. maj 2003 - 18:53 #1
hvis jeg kalder den således:
SETINI c:\MinFil.ini Config Navn orla

får jeg dette resultat:
[Config]
Navn=ORLA
Avatar billede joern Nybegynder
13. maj 2003 - 19:29 #2
Hej.

Jeg har ikke fordybet mig i din kode, men VB har en funktion der hedder lcase.  Kunne du ikke eksperimentere med den?

Evt. kan du efter at filen er dannet, indlæse den linie for linie, - sætte lcase foran den indlæste variabel og skrive linien til en 'skygge' som du derpå kopierer og lader overskrive den oprindelige ini-fil.

M.v.h.  Jørn
Avatar billede kipolaks Nybegynder
13. maj 2003 - 19:41 #3
problemet er at jeg ikke selv har vb... :( jeg søger en der kan fikse det og kopilere det til mig :) evt for nogle flere point
Avatar billede joern Nybegynder
13. maj 2003 - 20:47 #4
Hej.

Det bliver ikke mig, efter min vurdering mangler der en del, bl.a. en form.  Hvis ikke du kan få nogen til at gøre det for point, så prøv sharksforum.dk, dér gør 'eksperterne' det for cool cash.

M.v.h.  Jørn
Avatar billede kipolaks Nybegynder
13. maj 2003 - 21:34 #5
en form??? det er DOS!
Avatar billede joern Nybegynder
13. maj 2003 - 21:52 #6
Men poulseye.

Hvorfor stiller du så spørgsmålet i Visual Basic gruppen?  Der står en del jeg ikke genkender, bl.a. derfor vil jeg ikke forsøge mig.  Jeg står af spørgsmålet, håber andre kan hjælpe.

M.v.h.  Jørn
Avatar billede kipolaks Nybegynder
20. maj 2003 - 02:17 #7
lukker... jeg måtte gøre det på en anden måde (hvilket viste sig at være bedere)
Avatar billede Ny bruger Nybegynder

Din løsning...

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.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester