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!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.

I stand corrected.  :)

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

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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.