Jump to content

[Request] Create file with specific size


Recommended Posts

Hi, Jaclaz.

I'm so sorry when ask you again. I try to know how each line in your code work.

i'm very interested with your combine math in .batch.

Let say about limited number in .bat

2 months ago, I pratice using fsutil to create a blank file with specific size

Fsutil file createnew abc def

 (abc is file name and def is kb in size)

 

My problem is create a file, that say about 2Gb then convert it to ext2 for running some LinuxOS persistence.

 

My .bat

 

set /p size=Enter size (in Gb):

set /a size=%size% * 1073741824

Fsutil file createnew C:\persist %size%

 

But .bat limited number and then if file size more than about 1.3Gb (not remember exactly number), .bat will error

How can I solve this problem?? Tks

 

Edit: I want to create exactly file size. ( 1024 Mb for 1Gb).

Edited by congnt92
Link to comment
Share on other sites


Just a quick post to clear up a few things:

I have created this as a New Topic, please don't start new unrelated questions within another subject thread.

I'm not aware that an empty file has a size, a specific size above zero will have content.

EXT2 is a file system format not a file type. You cannot convert it, while it resides in the NTFS or FAT system it remains that.

  • Upvote 1
Link to comment
Share on other sites

There is a limit (actually more than one) in batch math.

Besides the fact that only integers are accepted, there is a limit which is 0x7FFFFFFF, i.e. 2,147,483,647.

Numbers bigger than that are NEGATIVE numbers, i.e.:

SET /A max=0x80000000

will output:

-2147483648

and

SET /A max=0xFFFFFFFF

will output:

-1

 

But if you want a file exactly 1 Gb (a "real" one, what to be "politically correct" we should call a Gib), i.e. 1024x1024x1024=1,073,741,824

There should be no problem.

 

Otherwise you will need a trick or two (or more).

Check my multiply.cmd:

http://reboot.pro/topic/2986-yacbfc-dec2hexcmd-and-hex2deccmd/

 

The file you create with fsutil is not actually "blank", it is filled of 00's:

https://technet.microsoft.com/en-us/library/cc788058.aspx

 

Then, in order to have an EXT2 filesystem applied to it you need a tool capable of formatting a file with that filesystem or capable to format a volume once you have mounted it in a virtual disk driver.

The Ext2dfsd project:

https://sourceforge.net/projects/ext2fsd/

does contain a mkefs2.exe, a Windows port of the mke2fs Linux utility (through Cygwin), which may (or may not) do what you need.

mke2fs is not exactly a tool with a "friendly" syntax, but usually it auto-calculates all parameters, so that something *like*:

set CYGWIN=nodosfilewarningmke2fs.exe <full path to the image file>set CYGWIN=

should work :unsure:

 

jaclaz

  • Upvote 1
Link to comment
Share on other sites

Just a quick post to clear up a few things:

I have created this as a New Topic, please don't start new unrelated questions within another subject thread.

I'm not aware that an empty file has a size, a specific size above zero will have content.

EXT2 is a file system format not a file type. You cannot convert it, while it resides in the NTFS or FAT system it remains that.

 

Oh i'm sorry.

I think that this is a small quetion, and I created 2 topics on one day (one about For /f command and one about EFI partition). I don't know if it is kind of spam or not when i create the third topic.

 

---

 

I just use fsutil to create a empty file, then use mke2fs as Jaclaz say. I know that fsutil just create a file, not ext2 file. But tks for your explain.

Link to comment
Share on other sites

There is a limit (actually more than one) in batch math.

Besides the fact that only integers are accepted, there is a limit which is 0x7FFFFFFF, i.e. 2,147,483,647.

Numbers bigger than that are NEGATIVE numbers, i.e.:

SET /A max=0x80000000

will output:

-2147483648

and

SET /A max=0xFFFFFFFF

will output:

-1

 

But if you want a file exactly 1 Gb (a "real" one, what to be "politically correct" we should call a Gib), i.e. 1024x1024x1024=1,073,741,824

There should be no problem.

 

Otherwise you will need a trick or two (or more).

Check my multiply.cmd:

http://reboot.pro/topic/2986-yacbfc-dec2hexcmd-and-hex2deccmd/

 

The file you create with fsutil is not actually "blank", it is filled of 00's:

https://technet.microsoft.com/en-us/library/cc788058.aspx

 

Then, in order to have an EXT2 filesystem applied to it you need a tool capable of formatting a file with that filesystem or capable to format a volume once you have mounted it in a virtual disk driver.

The Ext2dfsd project:

https://sourceforge.net/projects/ext2fsd/

does contain a mkefs2.exe, a Windows port of the mke2fs Linux utility (through Cygwin), which may (or may not) do what you need.

mke2fs is not exactly a tool with a "friendly" syntax, but usually it auto-calculates all parameters, so that something *like*:

set CYGWIN=nodosfilewarningmke2fs.exe <full path to the image file>set CYGWIN=

should work :unsure:

 

jaclaz

 

Tks for your explain, Jaclaz.

About command to make a ext2 file, i found it from RMPrepUSB (steve use fsutil vs mke2fs, too, but not extactly size). Also i don't want use RMPrepUSB just create a ext2 file. Since we have fsutil and light weight tool mke2fs.exe we only need one thing, a .bat file to do that.

 

I found that, we can create a "empty" file (not sure) with dd for windows. But still it is not exactly size as we want.

 

I will try your .bat from reboot then see if i can use some code from it for my case.

Tks

Link to comment
Share on other sites

The tool I normally use to create a 00's file is fsz (part of the dsfok toolkit), but still it uses bytes as argument.

 

Technically you can even use a loop in plain batch, using the powers of 2 (as long as you want sizes that are multiples), but since you need anyway a third party utility (mke2fs), you could use a file creating one that accepts suffixes like K, M or G.

Here:

http://www.henrynestler.com/colinux/tools/

http://www.henrynestler.com/colinux/tools/file-utils-mingw-bin-stripped.zip

meet mkfile.exe :

C:\appoggio\Filetests>mkfileUsage: mkfile [-s] [-r] [-k] [-m] [-g] filename filesizewhere filename is the name of the file to createfilesize if the size of the file.-s means create a sparse file ( only supported on ntfs 5 or greater )-r means resize an existing file-k -m -g means the size is in KB, MB, or GB

jaclaz

  • Upvote 1
Link to comment
Share on other sites

The tool I normally use to create a 00's file is fsz (part of the dsfok toolkit), but still it uses bytes as argument.

 

Technically you can even use a loop in plain batch, using the powers of 2 (as long as you want sizes that are multiples), but since you need anyway a third party utility (mke2fs), you could use a file creating one that accepts suffixes like K, M or G.

Here:

http://www.henrynestler.com/colinux/tools/

http://www.henrynestler.com/colinux/tools/file-utils-mingw-bin-stripped.zip

meet mkfile.exe :

C:\appoggio\Filetests>mkfileUsage: mkfile [-s] [-r] [-k] [-m] [-g] filename filesizewhere filename is the name of the file to createfilesize if the size of the file.-s means create a sparse file ( only supported on ntfs 5 or greater )-r means resize an existing file-k -m -g means the size is in KB, MB, or GB

jaclaz

Greate. I'll try it now. Tks for your useful tool.

And how can you know these tool. It seem quite "hidden" on internet. I try googled many times but not found some tools like this.

Link to comment
Share on other sites

And how can you know these tool. It seem quite "hidden" on internet. I try googled many times but not found some tools like this.

 

Would you think that the title of The Finder was arbitrary? :dubbio:

(please note the capital T and F :yes:)

 

And of course, it's a state secret ...:

http://www.literaturepage.com/read.php?titleid=man-in-the-iron-mask&abspage=294

:w00t::ph34r:

 

 

 

jaclaz

  • Upvote 1
Link to comment
Share on other sites

 

And how can you know these tool. It seem quite "hidden" on internet. I try googled many times but not found some tools like this.

 

Would you think that the title of The Finder was arbitrary? :dubbio:

(please note the capital T and F :yes:)

 

And of course, it's a state secret ...:

http://www.literaturepage.com/read.php?titleid=man-in-the-iron-mask&abspage=294

:w00t::ph34r:

 

 

 

jaclaz

 

Oh, I see. You're "google" guy.

So if i can not found something on internet, may i will ask for your help :thumbup. Thanks again for your help in almost my topic.

God bless  you :)

Link to comment
Share on other sites

Now that the general conversation has finished and because you hadn't specified your OS, I thought I'd mention using a (probably built-in) PowerShell script to create your file.

 

New-SizedFile.ps1

param( [string]$FilePath,[double]$Size )$file = [System.IO.File]::Create($FilePath)$file.SetLength($Size)$file.Close()

Example Uses:

Create Dummy.txt with size 698 bytes
.\New-SizedFile.ps1 C:\Users\Yzöwl\Dummy.txt 698
 
Create Dummy.txt with size 86 KB
.\New-SizedFile.ps1 C:\Users\Yzöwl\Dummy.txt 86KB
 
Create Dummy.txt with size 20 MB
.\New-SizedFile.ps1 C:\Users\Yzöwl\Dummy.txt 20MB
 
Create Dummy.txt with size 3 GB
.\New-SizedFile.ps1 C:\Users\Yzöwl\Dummy.txt 3GB
  • Upvote 1
Link to comment
Share on other sites

 

Now that the general conversation has finished and because you hadn't specified your OS, I thought I'd mention using a (probably built-in) PowerShell script to create your file.

 

New-SizedFile.ps1

param( [string]$FilePath,[double]$Size )$file = [System.IO.File]::Create($FilePath)$file.SetLength($Size)$file.Close()

Example Uses:

Create Dummy.txt with size 698 bytes
.\New-SizedFile.ps1 C:\Users\Yzöwl\Dummy.txt 698
 
Create Dummy.txt with size 86 KB
.\New-SizedFile.ps1 C:\Users\Yzöwl\Dummy.txt 86KB
 
Create Dummy.txt with size 20 MB
.\New-SizedFile.ps1 C:\Users\Yzöwl\Dummy.txt 20MB
 
Create Dummy.txt with size 3 GB
.\New-SizedFile.ps1 C:\Users\Yzöwl\Dummy.txt 3GB

 

 

Tks for your help. Let me say about my goal.

I'm creating a multiboot which contain: winpe, ubuntu (live, not install to harddisk). It will work on UEFI and LEGACY.

-WinPE is thin ver, and I create ToolsforPE.ISO then after boot to WinPE, I use .bat to mount it. (this is reason why I create a topic about For /f command and solved with Jaclaz's help)

-with uefi computer, i need .efi file to boot then i need a .bat which mount EFI partition then copy some .efi to it, then chainload them by grub2. this is reason why i create a topic about order partition

-ubuntu live and persistence need a file call: casper-rw with ext2 format. And i must create a "empty" file by fsutil then use mke2fs convert it. since fsutil create a file base on kb in size bytes in size, then i get problems with .bat because it limit number. Jaclaz give a link to download useful tool which create exactly size in Gib.

 

Because I use .bat for all of 3 task above then I do not know if i can use PowerShell in .bat or not.

Edited by congnt92
Link to comment
Share on other sites

If powershell is available you can access it directly from your .bat .cmd file.

@Powershell -C ".\New-SizedFile.ps1 %UserProfile%\Dummy.txt 1GB"

The above command assumes the .ps1 is along side the .cmd.

  • Upvote 1
Link to comment
Share on other sites

But first you have to change execution policy in Powershell, otherwise it will return an information that the script cannot be run.

Not necessary, just bypassing it would suffice should the Execution Policy be a problem.

 

Example:

@PowerShell -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -Command ".\New-SizedFile.ps1 %UserProfile%\Dummy.txt 1GB"
  • Upvote 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.


×
×
  • Create New...