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:
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
GOTO done
'----------------------------------------------------------------
HELP:
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 " Eric Phelps Sacramento,CA 1994 DISTRIBUTE FREELY"
'----------------------------------------------------------------
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
