#69687 - 2002-09-08 04:31 AM
WriteLine is appending not replacing
|
Tom Hagen
Fresh Scripter
Registered: 2001-11-02
Posts: 7
Loc: Milwaukee, WI
|
I'm currently trying to write a script for my remote users to run a backup that will compare the files on their local machine. The problem is the file I want to use for logging. It does not replace the line, instead it appends the info. Can any help me with replacing the text instead of appending?
Thanks,
Tom
code:
;***** Find where the users My Documents folder is and backup if not on server.
; $Location is the location of where My Documents is located in the registry ; @HomeDrive is the drive location that User Manager has the Home drive as ; $Source is the Path that robocopy uses for Source ; $Dest is the Path that robocopy uses for Destination ; $File is the Filename that robocopy uses ; $Opti is the Options that robocopy uses ; $ShellCMD is the command line that is passed out to run
;***** Edit or Create a log of the backup. ; $FileName the path and name location of the log file ; $X line read from UserBackup.CSV ; $Found if the string is found or not found ; $LineToWrite the information of when the user did a backup
;DEBUG "ON"
$Location = ReadValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "Personal") If @ERROR = 0 If Left($Location, 2) = @HomeDrive ? "The location of My Documents is on the server already no backup needed." Else $Source = $Location $Dest = Chr(34) + "F:\My Documents" + Chr(34) $Opti = "/R:3 /Z /Mir" $ShellCMD = "RoboCopy " + $Source + " " + $Dest + " " + $Opti
Shell "%comspec% /c $ShellCMD" EndIf EndIf
$FileName = "C:\Data\UserBackup.csv" ;eventully to be placed on server. $X = ReadLine(1) $Found = 0 $LineToWrite = %username% + "," + @Date + "," + @Time
OPEN(1, $FileName, 5) ;UserBackup file created to track when users backup.
While @Error=0 If INSTR ($X, %UserName%) <> 0 $X = WriteLine (1, $LineToWrite) $Found = 1 EndIf
$X = ReadLine(1) Loop
If $Found = 0 WriteLine (1, $LineToWrite) EndIF
Close(1) ;Close UserBackup.csv file
Exit
Current log file. Tom.Hagen,2002/09/07,21:16:49Tom.Hagen,2002/09/07,21:17:13Tom.Hagen,2002/09/07,21:17:17
_________________________
Tom Hagen Jr
Power comes from the Mind.
|
|
Top
|
|
|
|
#69688 - 2002-09-08 04:59 AM
Re: WriteLine is appending not replacing
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
You might be better off using an INI file format. That way you can leverage ReadProfileString and WriteProfileString. The ReadProfileString can enumerate the section headings or the keys of a particular section.
If you want to use WriteLine you will be required to manage the whole file in memory and write the whole file after modifying the line.
{edit} Also when using WriteLine, it only writes what you tell it. You have to include @CRLF to get a new line. [ 08. September 2002, 05:02: Message edited by: Howard Bullock ]
|
|
Top
|
|
|
|
#69690 - 2002-09-08 04:21 PM
Re: WriteLine is appending not replacing
|
Tom Hagen
Fresh Scripter
Registered: 2001-11-02
Posts: 7
Loc: Milwaukee, WI
|
I tried the WriteProfileString and it did not work. Can you tell me what I'm doing wrong? The way I need it to work is that if the person's name is not there it must be created or added to the file, its now doing it.
Thanks,
Tom
code:
$FileName = "C:\Data\UserBackup.ini" $Found = 0 $LineToWrite = "," + %username% + "," + @Date + "," + @Time
OPEN(1, $FileName, 5) ;UserBackup file created to track when users backup.
ReadProfileString ($FileName, "", %username%) If @ERROR = 0 WriteProfileString ($FileName, "", %username%, $LineToWrite) $Found = 1 Endif
If $Found = 0 WriteLine (1, "%username%=" + $LineToWrite + @CRLF) EndIF
Close(1) ;Close UserBackup.csv file
_________________________
Tom Hagen Jr
Power comes from the Mind.
|
|
Top
|
|
|
|
#69691 - 2002-09-08 05:25 PM
Re: WriteLine is appending not replacing
|
Sealeopard
KiX Master
   
Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
|
Please read the KiXtart manual for the correct parameters to use OPEN, READPROFILESTRING, and WRITYEPROFILESTRING. OPEN is not required to read and write to an .INi file. Additionally, you need to use @USERID instead of %USERNAME%. OPEN, CLOSe, rEADLINE, WRITELINE are to be used for text files, not .INI files which have separate READ/WRITE commands due to the special .INI format.
If you are using wRITEPROFILESTRING, missing sections and added automatically.
code:
$FileName = 'C:\Data\UserBackup.ini' $LineToWrite = ''+@Date + ' ' + @Time $rc = WriteProfileString ($FileName, @USERID, 'Logging', $LineToWrite)
[ 08. September 2002, 17:32: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.
|
|
Top
|
|
|
|
#69693 - 2002-09-09 02:04 PM
Re: WriteLine is appending not replacing
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Tom,
Is you question answered here?
I noticed and referred back here from Cramsession too.
Ref - WriteLine is appending not replacing
Thanks!
Kent
|
|
Top
|
|
|
|
#69694 - 2002-09-10 07:36 AM
Re: WriteLine is appending not replacing
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Tom,
We have been able to cobble together what you are looking for..
code:
;***** Find where the users My Documents folder is and backup if not on server.
; $Location is the location of where My Documents is located in the registry ; @HomeDrive is the drive location that User Manager has the Home drive as ; $Source is the Path that robocopy uses for Source ; $Dest is the Path that robocopy uses for Destination ; $File is the Filename that robocopy uses ; $Opti is the Options that robocopy uses ; $ShellCMD is the command line that is passed out to run
;***** Edit or Create a log of the backup. ; $FileName the path and name location of the log file ; $X line read from UserBackup.CSV ; $Found if the string is found or not found ; $LineToWrite the information of when the user did a backup
;DEBUG "ON"
$location = ReadValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "Personal") IF @error = 0 IF Left($location, 2) = @homedrive ? "The location of My Documents is on the server already no backup needed." ELSE $source = $location $dest = Chr(34) + "F:\My Documents" + Chr(34) $opti = "/R:3 /Z /Mir" $shellcmd = "RoboCopy " + $source + " " + $dest + " " + $opti
SHELL "%comspec% /c $ShellCMD" ENDIF ENDIF
; -- to do the replacement, you should refer to the following post (final reply): ; -- http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=2;t=001951#000001 $filename = "C:\Data\UserBackup.csv" ;eventully yo be placed on server. $filename1 = "C:\Data\UserBackup.cs1" IF Open(1,$filename) ENDIF DEL $filename1 IF Open(2,$filename1,5) ENDIF
$line=ReadLine(1) WHILE (@error = 0) $found=Instr($line, @userid) IF ($found = 0) ? $line $linetowrite = @userid + "," + @date + "," + @time ? $linetowrite ELSE $result=$linetowrite ENDIF IF (WriteLine(2,$linetowrite+chr(13)+chr(10)) <> 0) ENDIF $line=ReadLine(1) LOOP ? "complete" $rc = Close(1) ;Close UserBackup.csv file $rc = Close(2) ;Close UserBackup.cs1 file COPY $filename1 $filename /h DEL $filename1
EXIT
HTH,
Kent [ 10. September 2002, 14:12: Message edited by: kdyer ]
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 756 anonymous users online.
|
|
|