Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How do I resize a file from the command prompt (or in a batch file)?

  • It needs to treat the file as binary. The file format is irrelevant and arbitrary.

  • Extending the file should pad it with zeros.

  • Shrinking the file should truncate the file.

In other words:

How do you duplicate the functionality of SetEndOfFile in the command prompt?

share|improve this question
add comment

4 Answers

up vote 7 down vote accepted

I don't believe the functionality exists natively in the DOS shell, I suspect you need to use another program to get the job done. Here are a few possibilities:

  1. There is a tool called Trunc which claims to do exactly what you are after:

    Trunc is a Windows command-line app to truncate files to a specified size. Also usable to enlarge a file.

    You'll have to try it out to find out exactly how the file is getting padded out (e.g. with zeros or garbage) but otherwise this seems to fit the bill of what you are asking for.

  2. fsutil is a Windows XP and above system tool that appears to have some functionality that might help. Specifically the fsutil file subcommand appears to be able to create, set length and also zero fill files.

    Specifically:

    fsutil file createnew "C:\temp\myfile" 1024

    Will create a zero filled 1024 byte file, but I cannot see a way to truncate the file. There is a command that sets valid data length, but it does not appear to modify the EOF marker as:

    Using setvaliddata

    In NTFS, there are two important concepts of file length: the End of File (EOF) marker and the Valid Data Length (VDL). The EOF indicates the actual length of the file. The VDL identifies the length of valid data on disk. Any reads between VDL and EOF automatically return 0 in order to preserve the C2 object reuse requirement.

  3. UVHD is a hex editor that appears to have the functions you are asking for. The only problem is that it only works under Windows/SFU (Services for Unix) and not DOS. What that actually means to an end user I don't actually know, but I have a vague suspicion that SFU was dropped in Vista and Win 7.

share|improve this answer
1  
Ah okay. Thanks, trunc does what I need. Oh, and a note: SFU (or rather, SUA -- "Subsystem for Unix-Based Applications", aka Interix) wasn't dropped in Windows 7; it's just now an optional component in the Ultimate version, unfortuntately. :\ –  Mehrdad Jun 20 '11 at 12:47
    
Ah, fair enough, I couldn't find much in the way of information on SUA/SFU though to be honest I didn't look too closely. I've also found another way to create arbitrary length files using fsutil and edited my answer, but as I noted I cannot see a valid way to truncate the files. –  Mokubai Jun 20 '11 at 13:03
    
By the way, I thought I'd point something out: I believe the valid data length is the virtual length of the file on the disk, not the physical length. If the file is compressed, for example, it could actually be smaller -- you'd need the compressed size of the file. –  Mehrdad Jun 22 '11 at 0:45
add comment

try my BSD truncate build: http://eadmaster.altervista.org/pub/prj/builds/truncate-1.12.zip

or chsize32 (closed source): http://www.ltr-data.se/files/chsize32.zip

Syntax:
CHSIZE32 [-s] file size[K|M|G|T]
share|improve this answer
add comment

copy /Y nul: file.txt

for setting to 0 bytes only, of course.

share|improve this answer
    
This really doesn't seem to answer the question. –  Manishearth Dec 19 '12 at 2:51
add comment

If you have GnuWin32 installed, then you can use dd to do it.

dd if=nul of=/the/file/to/truncate bs=1 count=0 seek=the_file_size oflag=append
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.