Page 1 of 1 1
Topic Options
#157691 - 2006-02-22 05:33 PM More ReadFile and String Issues
dataspike Offline
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 Offline
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)
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#157693 - 2006-02-22 06:14 PM Re: More ReadFile and String Issues
dataspike Offline
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 Offline
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 Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
$Write = WriteFile('C:\xfer2.txt',$file)
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#157696 - 2006-02-22 09:03 PM Re: More ReadFile and String Issues
dataspike Offline
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 Offline
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
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#157698 - 2006-02-22 09:20 PM Re: More ReadFile and String Issues
dataspike Offline
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 Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
$file[$line] = Join(Split($file[$line],$sOldString1),$newstring)
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#157700 - 2006-02-22 09:29 PM Re: More ReadFile and String Issues
dataspike Offline
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
#157701 - 2006-02-22 09:32 PM Re: More ReadFile and String Issues
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
This works.

Code:

Break on

$fileold = "D:\xfer.txt"
$filenew = "D:\xfer_new.txt"

$rc = Open (1, $fileold, 2)
$rc = Open (2, $filenew, 5)
$line = ReadLine (1)
$OldString= "%" + "windir" + "%"
$line = ReadLine (1)
While @ERROR = "0"
Select
Case %windir% = "c:\windows"
$newstring = "%" + "systemdrive" + "%\windows"
Case %windir% = "c:\winnt"
$newstring = "%" + "systemdrive" + "%\winnt"
EndSelect
$newline = Replace($line,$oldstring,$newstring)
$rc = WriteLine (2, $newline + @CRLF)
$line = ReadLine (1)
Loop
$rc = Close (1)
$rc = Close (2)
Del $fileold
Move $filenew $fileold



Used Jooel's replace UDF.
Replace() - string replace routine,the smaller one

[edit]
Just a little to late I see
[/edit]


Edited by Mart (2006-02-22 09:32 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#157702 - 2006-02-22 09:37 PM Re: More ReadFile and String Issues
dataspike Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 89
Loc: San Diego, CA
Thanks again, both of you for the help!!!!
Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 657 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.178 seconds in which 0.139 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org