#157691 - 2006-02-22 05:33 PM
More ReadFile and String Issues
|
dataspike
Getting the hang of it
Registered: 2005-03-09
Posts: 89
Loc: San Diego, CA
|
Here's what I am trying to do. I have a text file that has a URL path and a destination path. Based on what OS, I'd like it to automatically change the destination path in the file. Here is the code I am trying to use.
Code:
Function Change_Text
$sMyFile = @SCRIPTDIR + '\xfer.txt' If Exist($sMyFile) $sOldString1="%" + "windir" + "%" Select Case InStr(%windir%,"\Windows") $sNewString= %systemdrive% + "\Windows" Case InStr(%windir%,"\WinNT") $sNewString= %systemdrive% + "\WinNT" Case 1 $sNewString = %systemdrive% + "\Temp" EndSelect $msg = MessageBox("Old string = " + $sOldString1 + @CRLF + "New string = " + $sNewString,"Notice",64) Exit
For Each $sSection in Split(ReadProfileString($sMyFile,"",""),Chr(10)) If $sSection ; "Checking section "+$sSection+@CRLF For Each $sEntry in Split(ReadProfileString($sMyFile,$sSection,""),Chr(10)) If $sEntry $sValue=ReadProfileString($sMyFile,$sSection,$sEntry) ; "Entry "+$sEntry+" has value "+$sValue+" status:" If $sValue=$sOldString1; Or $sValue=$sOldString2 Or $sValue=$sOldString3 ; " **UPDATED**"+@CRLF $=WriteProfileString($sMyFile,$sSection,$sEntry,$sNewString) Else ; " No change"+@CRLF EndIf EndIf Next EndIf Next EndIf
EndFunction
Here is the text file http://upload.dataspike.org/files/1/xfer.txt (right click, save-as)
However, when I run the function it doesn't do anything. I know the commands are based off of using an INI type format with sections and what not, but with this file being read by an external application, I can't have any of those sections, it has to be formatted exactly as it is.
Any help, and or suggestions would be awesome!
Thanks, Chris
|
|
Top
|
|
|
|
#157692 - 2006-02-22 05:47 PM
Re: More ReadFile and String Issues
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
See readfile() and writefile() UDFs $sOldString1="%" + "windir" + "%" $newstring = '%systemdrive%'+split('%windir%',':')[1]
$file=readfile($filename) for each $line in $file if instr($line,$sOldString1) $line = join(split($line,$sOldString1),$newstring) endif next $nul=writefile($filename)
|
|
Top
|
|
|
|
#157693 - 2006-02-22 06:14 PM
Re: More ReadFile and String Issues
|
dataspike
Getting the hang of it
Registered: 2005-03-09
Posts: 89
Loc: San Diego, CA
|
When I use the above mentioned code, with the fucntions
ReadFile WriteFile
I get no changes made to the file. It does read the text file, but it doesn't make any changes. It actually gives me an error, because no array is referenced. I tried moving the command and adding the $line array, but no luck.
I'm kind of lost if you can't tell.
Edited by dataspike (2006-02-22 06:26 PM)
|
|
Top
|
|
|
|
#157694 - 2006-02-22 08:47 PM
Re: More ReadFile and String Issues
|
dataspike
Getting the hang of it
Registered: 2005-03-09
Posts: 89
Loc: San Diego, CA
|
Code:
Function Test $filename = 'C:\xfer.txt' $sOldString1= "%" + "windir" + "%" $newstring = '%systemdrive%'+ Split('%windir%',':')[1] $file = ReadFile($filename) For Each $line in $file If InStr($line,$sOldString1) $line = Join(Split($line,$sOldString1),$newstring) EndIf Next $Write = WriteFile('C:\xfer2.txt',$line) EndFunction
When using the above test function, it only creates a blank file. I'm at a loss as to why it's not working.
Edited by dataspike (2006-02-22 08:50 PM)
|
|
Top
|
|
|
|
#157695 - 2006-02-22 08:54 PM
Re: More ReadFile and String Issues
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
$Write = WriteFile('C:\xfer2.txt',$file)
|
|
Top
|
|
|
|
#157696 - 2006-02-22 09:03 PM
Re: More ReadFile and String Issues
|
dataspike
Getting the hang of it
Registered: 2005-03-09
Posts: 89
Loc: San Diego, CA
|
Quote:
$Write = WriteFile('C:\xfer2.txt',$file)
All that does is duplicates the original file, doesn't make any changes.
|
|
Top
|
|
|
|
#157697 - 2006-02-22 09:18 PM
Re: More ReadFile and String Issues
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
ok... I thought that might happen.
Change the for each loop into a standard for next loop
For $line=0 to ubound($file)-1 If InStr($file[$line],$sOldString1) $line = Join(Split($file[$line],$sOldString1),$newstring) EndIf Next
|
|
Top
|
|
|
|
#157698 - 2006-02-22 09:20 PM
Re: More ReadFile and String Issues
|
dataspike
Getting the hang of it
Registered: 2005-03-09
Posts: 89
Loc: San Diego, CA
|
Quote:
ok... I thought that might happen.
Change the for each loop into a standard for next loop
For $line=0 to ubound($file)-1 If InStr($file[$line],$sOldString1) $line = Join(Split($file[$line],$sOldString1),$newstring) EndIf Next
Same exact file again.
|
|
Top
|
|
|
|
#157699 - 2006-02-22 09:27 PM
Re: More ReadFile and String Issues
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
$file[$line] = Join(Split($file[$line],$sOldString1),$newstring)
|
|
Top
|
|
|
|
#157700 - 2006-02-22 09:29 PM
Re: More ReadFile and String Issues
|
dataspike
Getting the hang of it
Registered: 2005-03-09
Posts: 89
Loc: San Diego, CA
|
Quote:
$file[$line] = Join(Split($file[$line],$sOldString1),$newstring)
Finally... Thank you for your help.
Code:
Function Test $filename = 'C:\xfer.txt' $sOldString1= "%" + "windir" + "%" $newstring = '%systemdrive%'+ Split('%windir%',':')[1] $file = ReadFile($filename) For $line=0 to Ubound($file)-1 If InStr($file[$line],$sOldString1) $file[$line] = Join(Split($file[$line],$sOldString1),$newstring) EndIf Next $Write = WriteFile('C:\xfer2.txt',$file) EndFunction
Edited by dataspike (2006-02-22 09:30 PM)
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 657 anonymous users online.
|
|
|