Login | |
|
 |
RE: Edit Ini via VBS - 8/4/2005 2:26:22 AM
|
|
 |
|
| |
Zifter
Posts: 318
Score: 0
Joined: 1/5/2005
From: Belgium
Status: offline
|
In VbScript there aren't any functions to edit ini files. Because a ini file is a text file, you could use the FileSystemObject to make your additions. Check the following example how to add text to the end of the file Option Explicit Const FOR_READING = 1 Const FOR_WRITING = 2 Const FOR_APPENDING = 8 Dim objFso Dim objFile Set objFso = CreateObject("Scripting.FileSystemObject") Set objFile = objFso.OpenTextFile("C:\example\zzz.ini",FOR_APPENDING) objFile.WriteLine "[front]" objFile.WriteLine "n1=bla" objFile.Close Set objFile = Nothing Set objFso = Nothing The code will be a little bit more complex when you want to change a ini value (you will have to read the file until you found the key you're looking for, the replace the following line with the new value) HTH EDIT: the Microsoft Scripting Guys can explain much better then I can
|
|
| |
|
|
|
 |
RE: Edit Ini via VBS - 8/5/2005 5:32:27 AM
|
|
 |
|
| |
mbouchard
Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
|
Take a look at the Files collection in the WSH documentation, this will have an example of how to get all files in a folder. Then all you would need is an if/then statement and you would be good to go. 'Code here from example in documentations. replace the following for each f1 in fc blah Next With For each f1 in fc If lcase(fso.GetExtensionName(f1.name)) = "ini" then 'Do your appending stuff here End If Next
_____________________________
Mike For useful Scripting links see the Read Me First stickey! Always remember Search is your friend.
|
|
| |
|
|
|
 |
RE: Edit Ini via VBS - 8/6/2005 10:27:58 AM
|
|
 |
|
| |
mbouchard
Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
|
Have you taken a look at the WSH help docs? These will help you. If you are just learning vbs your best bet is to try to put this script together in steps. Script 1 (enumerate files in a folder) Step 1 (all files) a) Take a look at the Files collection found in the WSH docs. copy and paste everything into your script editor (notepad will do). b) Delete the first and last line (Should be Function X and End Function. c) Change filespec/folderspec or what ever it is to your folder, include quotes i.e. ("c:\somefolder") d) Replace everything between the For x in x and the NEXT to MSGBOX f1.name. e) Save the script and run it. f) If you get an error post it and your script here. Show that you are making an attempt to write a script and not just asking people to do it for you. If it is working lets go on to Step 2 Step 2 a) edit the above script, inside the FOR NEXT add what I wrote below, also by this time you are probably getting sick of hitting enter everytime there is a file found. So we will make an additional change. For each f1 in fc If lcase(fso.GetExtensionName(f1.name)) = "ini" then 'instead of Msgbox use this msg = msg & f1.name & vbcr End If Next msgbox msg Now this script should only show a list of ini files. 1 to a line. Again, if you get an error message, post it and your code here. If it works on to the next script Script 2 (append to a file) Take what Zifter wrote below. It will work, just change the folder/file to a test folder/file on your PC. Here again, if you get an error, post it and your script here. Once you got that working, it is now time to combine the scripts. Script 3 There will be 2 things that will be different between the 2 scripts, Zifter uses objFSO while MS used fso. Pick one and use it. If you decide to use objFSO then open your first script and replace every instance of fso with objFSO, in notepad use crtl-H. If you want to use fso do the same thing with the second script and replace all objFSO with fso. it doesn't matter which one you use but it is good to be consistant between your scripts. Now that you have that part done, Take script 1 and copy everything into script 3 take script 2 and copy the top 5 lines (option explicit and the 3 CONST) and past it to the top of the script 3, you only need 1 Option Explicit at the top. This will help make sure you don't accidently misspell a variable. Example Dim strBlah strBlah = "Something" msgbox strBalh If you had Option explicit you would have gotten an error, without option explicit you would get nothing. Also, Option explicit will give you an error if you do not DIM your variables. Back to the script 3. Once you have the top part done, From Zifter's code, copy the bottom SET (open text file) the 2 writes and the close and replace the msg = msg xxxxxx with the 4 copied lines. Make 1 change to this line, to give it the ability to use whatever ini file is found with the GetExtension : This Set objFile = objFso.OpenTextFile("C:\example\zzz.ini",FOR_APPENDING) TO Set objFile = objFso.OpenTextFile(f1.Path,FOR_APPENDING) Note, I have removed the " from the second line and instead of using f1.NAME I am using f1.PATH Name will only display the name of the file xxx.ini while path will display the path to and the name of the file c:\folder\xxx.ini Now run this against a test folder, and give it multiple ini files to edit, they can be empty if you want but you will need to open each one to see if the lines were appended. if they were, you have a working script and you just will need to test it several times to verify it is in fact working. if you get any errors, post the error and your script here. Once you have a script you might want to check to see if the lines are already present, this will allow you to skip any files that are already done. But that part will come another day. Taking steps to put a "complex" script together helps make it much easier then trying to do everything at once. I know that this is how I do em. As you go, if you have any questions post em. The people here don't mind helping, as long as others make attempts to get something working.
_____________________________
Mike For useful Scripting links see the Read Me First stickey! Always remember Search is your friend.
|
|
| |
|
|
|
 |
RE: Edit Ini via VBS - 8/7/2005 10:27:08 AM
|
|
 |
|
| |
Fredledingue
Posts: 383
Score: 0
Joined: 5/9/2005
From:
Status: offline
|
ok, let's try this: Const ForReading = 1, ForWriting = 2, ForAppending = 3 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 Const OPEN_FILE_FOR_APPENDING = 8 Set fso = CreateObject("Scripting.fileSystemObject") IniKey = "[SomeIniKey]" Set f = fso.GetFile("xyz.ini") Set ts = f.OpenAsTextStream(ForReading, Tristatefalse) t = ts.ReadAll ts.Close t1 = Left(t, InStr(t, IniKey)-1) tx = Mid(t, InStr(t, IniKey)+ Len(IniKey)+1) t2 = Left(tx, InStr(tx, "[")-1) '---isolate your key t3 = Mid(tx, InStr(tx, "[")) '-------edit your key t2 = Replace(t2, "YourScriptingSkill=0", "YourScriptingSkill=100") t2 = Replace(t2, "GoVbs=No ", "GoVbs=Yes") 'etc '------rewrite ini file Set objOutputFile = fso.OpenTextFile("xyz.ini", ForWriting) objOutputFile.Write t1 & VbCrlf & t2 & VbCrlf & t3 objOutputFile.Close Msgbox "ini file updated.",,IniKey
_____________________________
Fred
|
|
| |
|
|
|
 |
RE: Edit Ini via VBS - 8/7/2005 10:40:11 AM
|
|
 |
|
| |
Fredledingue
Posts: 383
Score: 0
Joined: 5/9/2005
From:
Status: offline
|
Now if you want user input while re-editing your ini file... Const ForReading = 1, ForWriting = 2, ForAppending = 3 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 Const OPEN_FILE_FOR_APPENDING = 8 Title = "Updating ini file" Set fso = CreateObject("Scripting.fileSystemObject") IniKey = "[SomeIniKey]" Set f = fso.GetFile("xyz.ini") Set ts = f.OpenAsTextStream(ForReading, Tristatefalse) t = ts.ReadAll ts.Close t1 = Left(t, InStr(t, IniKey)-1) tx = Mid(t, InStr(t, IniKey)+ Len(IniKey)+1) t2 = Left(tx, InStr(tx, "[")-1) '---isolate your key t3 = Mid(tx, InStr(tx, "[")) '-------edit your key tvar = Split(t2, VbCrlf) i=1 Do Until tvar(i)="" tval = Mid(tvar(i),InStr(tvar(i),"=")) tname = Left(tvar(i),InStr(tvar(i),"=")-1) tvar(i) = tname & InputBox("new value for" & VbCrlf & tname, Title, tval, 120, 840) i=i+1 loop '------rewrite ini file Set objOutputFile = fso.OpenTextFile("xyz.ini", ForWriting) objOutputFile.WriteLine t1 objOutputFile.WriteLine IniKey For j=1 to i objOutputFile.WriteLine tvar(j) Next objOutputFile.WriteLine t3 objOutputFile.Close Msgbox IniKey & " updated.",,Title[/comor] Please note that I just wrote these code on the fly without testing them. There might be some error...
_____________________________
Fred
|
|
| |
|
|
|
 |
RE: Edit Ini via VBS - 8/8/2005 12:52:23 PM
|
|
 |
|
| |
jalejandro0211
Posts: 2
Score: 0
Joined: 8/8/2005
Status: offline
|
Try This funcs 'Work with INI files In VBS (ASP/WSH) 'v1.00 '2003 Antonin Foller, PSTRUH Software, http://www.motobit.com 'Function GetINIString(Section, KeyName, Default, FileName) 'Sub WriteINIString(Section, KeyName, Value, FileName) 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 'Separate section contents sContents = Mid(INIContents, PosSection, PosEndSection - PosSection) 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
|
|
| |
|
|
|
 |
RE: Edit Ini via VBS - 8/9/2005 12:11:35 AM
|
|
 |
|
| |
mbouchard
Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
|
You would need to DIM the objFile, just append this to your Dim statement at the top. Dim objFSO, f, f1, fc, s, objFile Also, you don't need your second set ObjFSO, once you set and DIM this at the top it is set for the script. You will need the set objFile though so leave that where it is. quote:
ORIGINAL: guzpapir Fredledinque thank you,for your work but that was not what I was looking for. I made a script from what mbouchard said but id shows an error the script goes like this: Option Explicit Const FOR_READING = 1 Const FOR_WRITING = 2 Const FOR_APPENDING = 8 Dim objFSO, f, f1, fc, s Set objFSO = CreateObject("Scripting.FileSystemObject") Set f = objFSO.GetFolder("C:\Example") Set fc = f.Files For Each f1 in fc If lcase(objFSO.GetExtensionName(f1.name)) = "ini" then Set objFso = CreateObject("Scripting.FileSystemObject") Set objFile = objFso.OpenTextFile(f1.Path,FOR_APPENDING) objFile.WriteLine "[front]" objFile.WriteLine "n1=bla" objFile.Close End If Next msgbox msg Maybe I dint understand you quite right mbouchard so If you or anyone else could help me I would be happy. The windows is complaining,pointing at the line 13(Set objFile = objFso.OpenTextFile(f1.Path,FOR_APPENDING) ) saying: variable undefined -objFile
_____________________________
Mike For useful Scripting links see the Read Me First stickey! Always remember Search is your friend.
|
|
| |
|
|
|
 |
RE: Edit Ini via VBS - 8/9/2005 12:14:43 AM
|
|
 |
|
| |
mbouchard
Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
|
Boom, Actually, imo your way would be a bit more troublesome. Basically, you could not use your computer while editing a file as any key press could mess up sendkeys. quote:
ORIGINAL: boom that is the hard way just do this to edit a freakin ini file i edit em all the time just open the ini and use the sendkeys function i always do it that way ----Snip----
_____________________________
Mike For useful Scripting links see the Read Me First stickey! Always remember Search is your friend.
|
|
| |
|
|
|
|
|