Page 1 of 1 1
Topic Options
#204197 - 2012-02-03 05:11 PM Clear the contents of a file
Bonji Offline
Starting to like KiXtart

Registered: 2001-09-28
Posts: 169
Loc: Virginia
I feel like I must be missing something simple, but for the life of me I cannot figure out how to clear a text file to just be blank.

Deleting the file and re-creating it is my usual solution, but in this case it will not work because the users that need a cleared out file only have access to this file specifically. If they delete the file, they do not have rights to create a new one. Modifying rights on this folder is not an option, either.

Any ideas?

Top
#204198 - 2012-02-03 05:27 PM Re: Clear the contents of a file [Re: Bonji]
Bonji Offline
Starting to like KiXtart

Registered: 2001-09-28
Posts: 169
Loc: Virginia
Well, I finally came up with a way to clear out a file:

 Code:
Break On

$oFSO = CreateObject("Scripting.FileSystemObject")
$oFile = $oFSO.OpenTextFile(@ScriptDir + "\Test.txt", 2)

$ = $oFile.Write("")
$ = $oFile.Close()

$oFile = "Nothing"
$oFSO = "Nothing"


I would prefer not to use a FSO object for this, but if nothing else works then this will have to do.

Top
#204199 - 2012-02-03 05:36 PM Re: Clear the contents of a file [Re: Bonji]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 687
Loc: Maryland, USA
in a batch file you could use "cat /dev/null > test.txt" It will give you an error, but will clear it out. I seem to remember there is a way to do this with standard DOS commands, but I can't remember. \:\)
Top
#204200 - 2012-02-03 06:01 PM Re: Clear the contents of a file [Re: BradV]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
How about...

 Code:
$nul = RedirectOutput("\\path\to\file\test.txt",1)
? ""
$nul = RedirectOutput("")

Top
#204204 - 2012-02-04 04:40 PM Re: Clear the contents of a file [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Or better yet...

 Code:
$nul = RedirectOutput("\\path\to\file\test.txt",1)
$nul = RedirectOutput("")

Top
#204205 - 2012-02-04 04:45 PM Re: Clear the contents of a file [Re: ShaneEP]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmm... wonder if that actually re-creates the file instead of just emptying it...
_________________________
!

download KiXnet

Top
#204206 - 2012-02-04 05:10 PM Re: Clear the contents of a file [Re: Lonkero]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
I've wondered the same thing, but I don't have the account to test it. Windows XP Home edition is much use when it comes to testing security. haha
Top
#204207 - 2012-02-05 11:29 AM Re: Clear the contents of a file [Re: Lonkero]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
 Originally Posted By: Lonkero
hmm... wonder if that actually re-creates the file instead of just emptying it...



Hmmmm....interesting.

I did some testing and it looks like it actually empties the file. Regular users do not have write permissions in the folder but do have write permissions on the file. Creation date and time stays the same. I do not see the file being deleted when I place some sleeps in the code. It just results in an empty file.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#204208 - 2012-02-05 03:06 PM Re: Clear the contents of a file [Re: Mart]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
that's good.


Edited by Lonkero (2012-02-05 03:06 PM)
_________________________
!

download KiXnet

Top
Page 1 of 1 1


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

Who's Online
0 registered and 530 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.057 seconds in which 0.023 seconds were spent on a total of 13 queries. Zlib compression enabled.

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