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

Posted on 2003-03-13
Medium Priority
Last Modified: 2010-05-01
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!
Question by:SilentKnight
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 38

Expert Comment

ID: 8128672
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.
LVL 18

Accepted Solution

deighton earned 200 total points
ID: 8128727
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

LVL 18

Expert Comment

ID: 8128756
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.

LVL 38

Expert Comment

ID: 8128771
I stand corrected.  :)

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


Author Comment

ID: 8134942
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!

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question