Jump to content

Copy text string to ini file


Recommended Posts

I've been tasked to backup and migrate some 600+ user profiles and I'm 2/3 the way there. I've successfully scripted a recursive search that goes through all the user profile subfolders and renames their current profile from .ini to a .txt extension. Second, I've been able to push the new profile template from a single directory to all the userprofile\windows subdirectory.

Now I need a script that will go through each user profile folder and copy three text strings from User#\windows\profile.txt and copy them over to the User#\windows\profile.ini. It should read something like this:

Open file "D:\Profiles\User1\windows\profile.txt"

Search

------Setting1=Entry1

------Setting2=Entry2

------Setting3=Entry3

Copy (to temp or clipboard)

Close file

Open File "D:\Profiles\User1\windows\profile.ini"

Paste at end of file (there are some entries in the profile.ini already)

------Setting1=Entry1

------Setting2=Entry2

------Setting3=Entry3

Close file

Repeat for each profile under "D:\profiles\..\windows\

I have a baseline script which could be way off course, but it can replace specific entries with specifc entries (already definied in the script) when targeting a specific file.

Const ForReading = 1

Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("D:\Profile\User1\windows\Profile.ini", ForReading)

strText = objFile.ReadAll

objFile.Close

strNewText = Replace(strText, "POSDELL", "TextEntry1")

Set objFile = objFSO.OpenTextFile("D:\Profile\User1\windows\Profile.ini", ForWriting)

objFile.WriteLine strNewText

objFile.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("D:\Profile\User1\windows\Profile.ini", ForReading)

strText2 = objFile.ReadAll

objFile.Close

strNewText2 = Replace(strText2, "CHGDELL", "TextEntry2")

Set objFile = objFSO.OpenTextFile("D:\Profile\User1\windows\Profile.ini", ForWriting)

objFile.WriteLine strNewText2

objFile.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("D:\Profile\User1\windows\Profile.ini", ForReading)

strText3 = objFile.ReadAll

objFile.Close

strNewText3 = Replace(strText3, "COSADELL", "TextEntry3")

Set objFile = objFSO.OpenTextFile("D:\Profile\User1\windows\Profile.ini", ForWriting)

objFile.WriteLine strNewText3

objFile.Close

"

That is pretty much an edit I got from The Scripting Guy. To use that one, I made entries in the upgraded profile template for POSDELL, CHGDELL and COSADELL so that the search could target those standard entries and replace them with the entries from the profile.txt.

Edited by sentinel1705
Link to comment
Share on other sites


Im not eactly sure what you are looking for but here is a batch file that can find a string "setting#" and append it to the file.ini

@echo off

FOR /F "skip=2 tokens=*" %%i IN ('find /i "Setting1" D:\Profiles\User1\windows\profile.txt') DO echo %%i >>
D:\Profiles\User1\windows\profile.ini

FOR /F "skip=2 tokens=*" %%i IN ('find /i "Setting2" D:\Profiles\User1\windows\profile.txt') DO echo %%i >>
D:\Profiles\User1\windows\profile.ini

FOR /F "skip=2 tokens=*" %%i IN ('find /i "Setting3" D:\Profiles\User1\windows\profile.txt') DO echo %%i >>
D:\Profiles\User1\windows\profile.ini

Link to comment
Share on other sites

That might be close. I'll have to run it at work tomorr....later today. One thing though is the batch/script has to be recursive to search through all the subfolders for the .txt and .ini files (keeping them in pairs for each folder).

I could do a script to make text entries of all the folder names and make it a filelist.txt of sorts, but I was looking to go a little neater

Basically some sort of batch/script that targets a parent folder called Profiles\ and will scan through each Profiles\User1\windows directory and compare profile.txt to profile.ini, copy 3 entries from profile.txt and append them to profile.ini, then do it again for Profiles\User2\Windows, Profiles\User3\Windows, Profiles\User4\Windows, Profiles\User5\Windows, etc. My user names are not as simple as 1, 2, 3, 4, but you get the idea.

Link to comment
Share on other sites

FOR /F "skip=2 tokens=*" %%i IN ('find /i  "Setting1" D:\Profiles\User1\windows\profile.txt') DO echo %%i >> D:\Profiles\User1\windows\profile.ini

I'm not sure that the for loop is needed
>>D:\Profiles\User1\windows\profile.ini Find /i "data1=value1"<D:\Profiles\User1\windows\profile.txt

Link to comment
Share on other sites

I'm not sure that the for loop is needed
>>D:\Profiles\User1\windows\profile.ini Find /i "data1=value1"<D:\Profiles\User1\windows\profile.txt

I ran that code as this

@echo off
>>"C:\Documents and Settings\Administrator\My Documents\GPUpgrade\Test\OFASNRAB1\windows\Profile.ini" Find /i "POSCURRENTWS=NRAB1" <"C:\Documents and Settings\Administrator\My Documents\GPUpgrade\Test\OFASNRAB1\windows\Profile.txt"
pause

And all I got was "press any key to continue"

Link to comment
Share on other sites

Okay, I think I found one that works at eggheadcafe

Option Explicit  
Dim oFSO, sFile1, sFile2, oFile1, oFile2, sText
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile1 = "C:\Test\User_057245\windows\profile.txt"
sFile2 = "C:\Test\User_057245\windows\profile.ini"
Set oFile1 = oFSO.OpenTextFile(sFile1, 1)
Set oFile2 = oFSO.OpenTextFile(sFile2, 8)
Do While Not oFile1.AtEndOfStream
sText = Trim(ofile1.ReadLine)
If InStr(sText, "CHGCurrentWS=") <> 0 Then
oFile2.WriteLine sText
End If
If InStr(sText, "POSCurrentWS=") <> 0 Then
oFile2.WriteLine sText
End If
If InStr(sText, "CHGPATH=") <> 0 Then
oFile2.WriteLine sText
End If
Loop

That code scanned profile.txt for sText and copied them over to Profile.ini. Now I need to know how to edit that code so that instead of targetting just one user folder, I target something like:

C:\test\..\windows\profile.txt >> C:\test..\windows\profile.ini, so that it compares all the user profiles under C:\Test with the .txt and .ini sets in each windows subfolder.

I generated a folderlist.ini and a folderlist.txt of all my profiles if that can be called somehow.

Link to comment
Share on other sites

This should work for you. Just change string1-3. I have tested this and it worked. So Yzowl code works. If it dont work for you check the file locations or the string your trying to look for.

BTW you owe me a beer for the script. :thumbup LOL

@echo off

dir C:\test\*.* /b -o:d | find /v "." > c:\dirs.txt
for /f "tokens=*" %%a in (c:\dirs.txt) do call :search "%%a"
del C:\dirs.txt
goto EOF


:search
>>"C:\Test\%~1\windows\profile.ini" Find /i "string1"<"C:\Test\%~1\windows\profile.txt"
>>"C:\Test\%~1\windows\profile.ini" Find /i "string2"<"C:\Test\%~1\windows\profile.txt"
>>"C:\Test\%~1\windows\profile.ini" Find /i "string3"<"C:\Test\%~1\windows\profile.txt"

Link to comment
Share on other sites

This should work for you. Just change string1-3. I have tested this and it worked. So Yzowl code works. If it dont work for you check the file locations or the string your trying to look for.

BTW you owe me a beer for the script. :thumbup LOL

@echo off

dir C:\test\*.* /b -o:d | find /v "." > c:\dirs.txt
for /f "tokens=*" %%a in (c:\dirs.txt) do call :search "%%a"
del C:\dirs.txt
goto EOF


:search
>>"C:\Test\%~1\windows\profile.ini" Find /i "string1"<"C:\Test\%~1\windows\profile.txt"
>>"C:\Test\%~1\windows\profile.ini" Find /i "string2"<"C:\Test\%~1\windows\profile.txt"
>>"C:\Test\%~1\windows\profile.ini" Find /i "string3"<"C:\Test\%~1\windows\profile.txt"

I tried that and even moved my test folder to the root drive so there wouldn't be any issues with folder names with spaces.

Running this code

@echo off

pause
dir C:\GPUpgrade\Test\*.* /b -o:d | find /v "." > c:\dirs.txt
for /f "tokens=*" %%a in (c:\dirs.txt) do call :search "%%a"
del C:\dirs.txt
goto EOF

pause

:search
>>"C:\GPUpgrade\Test\%~1\windows\profile.txt" Find /i "CHGPATH="<"C:\GPUpgrade\Test\%~1\windows\profile.ini"
>>"C:\GPUpgrade\Test\%~1\windows\profile.txt" Find /i "CHGCurrentWS="<"C:\GPUpgrade\Test\%~1\windows\profile.ini"
>>"C:\GPUpgrade\Test\%~1\windows\profile.txt" Find /i "POSCurrentWS="<"C:\GPUpgrade\Test\%~1\windows\profile.ini"

pause

Produced this image when run from the command prompt:

post-191653-1211216893_thumb.jpg

Edit: Scratch that. It works, it just didn't give me any echo out so I never checked the actual files for comparison. When I did, there were about 50 entries of my search string in the other profile (because I kept running it over and over). One minor glitch though, is there any way to put a space/return line in there? When it copied over the first entry, it put it at the end of the preceeding entry - making them both on the same line.

Edit x2. I just added a return character to the template and had it use that as the "spacer". Now it all works.

This will be a real time saver. Thank you

Edited by sentinel1705
Link to comment
Share on other sites

You can put an echo at the end of the scrip to let you know it’s finished. I’m guessing you don’t want a pause after every profile so you will need to remove the pause from the function.

@echo off

dir C:\test\*.* /b -o:d | find /v "." > c:\dirs.txt
for /f "tokens=*" %%a in (c:\dirs.txt) do call :search "%%a"
del C:\dirs.txt
Echo script has finished
pause
goto EOF


:search
>>"C:\Test\%~1\windows\profile.ini" Find /i "string1"<"C:\Test\%~1\windows\profile.txt"
>>"C:\Test\%~1\windows\profile.ini" Find /i "string2"<"C:\Test\%~1\windows\profile.txt"
>>"C:\Test\%~1\windows\profile.ini" Find /i "string3"<"C:\Test\%~1\windows\profile.txt"

Another option is to have it echo "script has finished" and wait x amout of time and then finish. All you have to do is replace the pause with this:

Echo script has finished
ping 127.0.0.0 -n 10 >nul

This will wait for 10 seconds before it continues and in this case end.

Link to comment
Share on other sites

I dumped all the profiles to my thumbdrive for testing and edited the paths accordingly, now I am getting the error message "The system cannot find the path specified."

@echo off

dir E:\Notes\GPUpgrade\TSProfiles*.* /b -o:d | find /v "." > E:\dirs.txt
for /f "tokens=*" %%a in (E:\dirs.txt) do call :search "%%a"
del E:\dirs.txt
Echo The script has finished
ping 127.0.0.0 -n 10 >nul
goto EOF


:search
>>"E:\Notes\GPUpgrade\TSProfiles\%~1\windows\Profile.ini" Find /i "CHGPATH="<"E:\Notes\GPUpgrade\TSProfiles\%~1\windows\Profile.txt"
>>"E:\Notes\GPUpgrade\TSProfiles\%~1\windows\Profile.ini" Find /i "CHGCurrentWS="<"E:\Notes\GPUpgrade\TSProfiles\%~1\windows\Profile.txt"
>>"E:\Notes\GPUpgrade\TSProfiles\%~1\windows\Profile.ini" Find /i "POSCurrentWS="<"E:\Notes\GPUpgrade\TSProfiles\%~1\windows\Profile.txt"

post-191653-1211298418_thumb.jpg

Link to comment
Share on other sites

I dumped all the profiles to my thumbdrive for testing and edited the paths accordingly, now I am getting the error message "The system cannot find the path specified."

@echo off

dir E:\Notes\GPUpgrade\TSProfiles*.* /b -o:d | find /v "." > E:\dirs.txt
for /f "tokens=*" %%a in (E:\dirs.txt) do call :search "%%a"
del E:\dirs.txt
Echo The script has finished
ping 127.0.0.0 -n 10 >nul
goto EOF


:search
>>"E:\Notes\GPUpgrade\TSProfiles\%~1\windows\Profile.ini" Find /i "CHGPATH="<"E:\Notes\GPUpgrade\TSProfiles\%~1\windows\Profile.txt"
>>"E:\Notes\GPUpgrade\TSProfiles\%~1\windows\Profile.ini" Find /i "CHGCurrentWS="<"E:\Notes\GPUpgrade\TSProfiles\%~1\windows\Profile.txt"
>>"E:\Notes\GPUpgrade\TSProfiles\%~1\windows\Profile.ini" Find /i "POSCurrentWS="<"E:\Notes\GPUpgrade\TSProfiles\%~1\windows\Profile.txt"

post-191653-1211298418_thumb.jpg

Nevermind.... "dir E:\Notes\GPUpgrade\TSProfiles*.* " is not equal to "dir E:\Notes\GPUpgrade\TSProfiles\*.* :blushing:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...