Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Anyone know how to reduce the length of an existing file?

Hi all,

I need to be able to write data to a file, and will not know in advance whether the final filesize will be smaller or larger than when the file was first opened.

No problem increasing the file length: The Put statement can save data past the end of a file open for binary access.

But I can't figure out how to shorten the file length.  It's OK if I need to close the first and then use some function (API?), similar to UNIX's truncate() function.

I don't want to copy part of the file, then delete the original, because other applications will be accessing the file at the same time.  (These other apps are well enough behaved to cope with changing filesizes, but of course, won't allow the file they're accessing to be deleted)

Thanks in advance!
SilentKnight
0
SilentKnight
Asked:
SilentKnight
  • 2
  • 2
1 Solution
 
PaulHewsCommented:
There is no windows api to shorten a file.  You can write over it with a new file of shorter length, but not while other applications have it open.  Best bet would be to do a compression during hours when no-one has it open.
0
 
deightonCommented:
Option Explicit

Private Sub SetFileSize(sPath As String, lSize As Long)

Dim lFILE As Long



lFILE = lopen(sPath, READ_WRITE)

If llseek(lFILE, lSize, FILE_BEGIN) <> -1 Then

    SetEndOfFile lFILE

End If

lclose lFILE


End Sub

Private Sub Command1_Click()
SetFileSize "c:\download\behemoth.txt", 145
End Sub



'your api declares in a .bas or whereever

Option Explicit

Declare Function lopen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long

Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long

Public Const READ_WRITE = 2

Declare Function llseek Lib "kernel32" Alias "_llseek" (ByVal hFile As Long, ByVal loffset As Long, ByVal iOrigin As Long) As Long

Public Const FILE_END = 2
Public Const FILE_BEGIN = 0


Declare Function SetEndOfFile Lib "kernel32" (ByVal hFile As Long) As Long


0
 
deightonCommented:
I think that can work if the file is open, I opened it in notepad and found I could increase and decrease the size ok.

0
 
PaulHewsCommented:
I stand corrected.  :)

Notepad doesn't keep the file open.  Try an excel file instead.

0
 
SilentKnightAuthor Commented:
Thanks deighton!!!

Works a treat.  I'm happy to do this after closing the file (because once I've finished modifying contents, I know how long it needs to be).  I just can't use the usual copy/delete approach in this case.

But others might be interested to keep this thread going if they want to find out how to shorten the length of an open file to other applications sharing the file.

Thanks again all - you guys are GREAT!
SilentKnight
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now