How to generate a large sparse file natively only with ahk?

Get help with using AutoHotkey and its commands and hotkeys
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

How to generate a large sparse file natively only with ahk?

08 Jan 2017, 05:41

I have a particularly mild form of ocd where by I've dealt with some of it by automatically generated palindrom buffer files on disk drives using a windows command:
fsutil file createnew filename length
,this basically creates a file that makes the available free space on any drive either an even number like 26 or 40 GB of free space or a palindrom like 1.81 or 35.3 GB of free space,in a nut shell I'm sort of nuts about numerical symmetry in numbers that stand out like drive free space or the volume level on a pc or TV for that matter. Anyway,I've always used scripts that actively modify free space on my drives by constantly modifying sparse files with size but no actuall data,I'd very much like a way to do that with no external tools,only with ahk,if not a complete solution ,I'd settle for someone pointing me in a general direction of how I might do that.

Thanks in advance.
Outsourcing Clicks & Presses Since 2004.
garry
Posts: 1978
Joined: 22 Dec 2013, 12:50

Re: How to generate a large sparse file natively only with ahk?

08 Jan 2017, 07:27

want you create a new big text file ?

Code: Select all

;- Benchmark test create big text file
;-------- https://autohotkey.com/boards/viewtopic.php?f=5&t=22666 ---
o := FileOpen("OutFile.txt", "w `n")
num := 70000
begin1:=a_tickcount
Loop, % num
    {
    ;ee := ee "`n" numToChar2(A_Index)          ;- 155 seconds
    ee .= numToChar2(A_Index) . "`n"          ;-  7-seconds
    }
delta1:=a_tickcount - begin1
o.WriteLine(ee)
ee=
msgbox, 262208,TimeDifference,Time1=%delta1%
run,outfile.txt
exitapp

numToChar2(num)
{
	String := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	while (num)
	{
		num--   ;- 0 base index
		OutVar := SubStr(String, Mod(num, 26) + 1, 1) OutVar
		num /= 26
	}
	return OutVar
}
;==========================================================================
EDIT : sorry you want something like 'fsutil' ( have no idea )

Code: Select all

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

behavior        Steuert das Dateisystemverhalten
dirty           Verwaltet fehlerhafte Bits auf dem Volume
file            Dateispezifische Befehle
fsinfo          Dateisysteminformationen
hardlink        Hardlinkverwaltung
objectid        Objektkennungverwaltung
quota           Kontingentverwaltung
reparsepoint    Analysepunktverwaltung
sparse          Steuert geringe Dateidatendichte
usn             USN-Verwaltung
volume          Volumeverwaltung

---- FILE - Unterstützte Befehle ----
findbysid               Sucht nach einer Datei mittels der Sicherheitskennung.
queryallocranges        Sucht die reservierten Bereiche nach einer Datei ab. setshortname            Legt den Kurznamen für eine Datei fest.
setvaliddata            Legt die gültige Datenlänge für eine Datei fest.
setzerodata             Setzt Daten einer Datei auf Null.
createnew               Erzeugt eine neue Datei mit der angegebenen Größe.

User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to generate a large sparse file natively only with ahk?

08 Jan 2017, 12:58

I've written a script that rounds the free disk space down to the nearest 100MB
by copying a large empty file and creating a small empty file.
You used the word 'sparse' which I take to mean mostly empty,
but with some non-zero bytes, did you mean a file of this description,
or would a file filled with null bytes be acceptable?
Btw be very careful with such a script that could
potentially try to fill the hard drive.
Perhaps add in a safety check to prevent too many files being created.

Btw '#MaxMem 200' for example, would allow you to create
a variable 200 MB in size.

How do you feel about speeling mistakes?
I saw on a TV program, a space, where there were 5 objects,
and you could only see 4 objects at the same time (something like that).
Free your mind of the need for all 5 objects. Om.

Code: Select all

^q:: ;round the free disk space down to the nearest 100MB
;by copying a large file and creating a small file
vDirBlank = %A_Desktop%\blanks
vPathBlankBrick = %vDirBlank%\blank brick.bin
vSizeBlankBrick := 10*1024*1024

IfNotExist, %vDirBlank%
FileCreateDir, %vDirBlank%

;create a large blank file that we copy and paste as required
IfNotExist, %vPathBlankBrick%
{
VarSetCapacity(vData, vSizeBlankBrick)
oFile := FileOpen(vPathBlankBrick, "w")
oFile.RawWrite(vData, vSizeBlankBrick)
oFile.Close()
}

;get size of files in blank dir, minus size of blank brick
;we add/remove files in this folder to get the free space number we want
;but we always maintain the blank brick so that we can copy and paste it
vFree2 := ""
Loop, %vDirBlank%\*.*, 0, 0 ;(0/1/2=files/both/folders, 0/1=recurse no/yes)
vFree2 += A_LoopFileSize
vFree2 -= vSizeBlankBrick

DllCall("GetDiskFreeSpaceEx" (A_IsUnicode ? "W":"A"), Str,"C:\", Int64P,vFree, UInt,0, UInt,0)
vFree := vFree + vFree2
vFreeMB := (vFree // 1024**2)

vNum := Mod(vFreeMB, 100)
vNum2 := vNum // 10 ;number of bricks required
vNum3 := Mod(vFreeMB, 10) ;amount of filler required

MsgBox % vNum " = " vNum2 "*10 + " vNum3

Loop
{
vPathBlankAdd = %vDirBlank%\blank %A_Index%.bin
if (A_Index <= vNum2)
IfNotExist, %vPathBlankAdd%
FileCopy, %vPathBlankBrick%, %vPathBlankAdd%
if (A_Index > vNum2)
IfExist, %vPathBlankAdd%
FileDelete, %vPathBlankAdd%
else
break
}

vSizeBlankFiller := vNum3*1024*1024
vPathBlankFiller = %vDirBlank%\blank filler.bin
vSizeBlankFiller2 := 0
IfExist, %vPathBlankFiller%
FileGetSize, vSizeBlankFiller2, %vPathBlankFiller%
if !(vSizeBlankFiller = vSizeBlankFiller2)
{
VarSetCapacity(vData, vSizeBlankFiller)
oFile := FileOpen(vPathBlankFiller, "w")
oFile.RawWrite(vData, vSizeBlankFiller)
oFile.Close()
}
MsgBox % "done"
Return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
just me
Posts: 6730
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to generate a large sparse file natively only with ahk?

09 Jan 2017, 02:59

Code: Select all

#NoEnv
FileDrive := "D:"
FileName := "Sparse.bin"
FileSize := 1024 * 1024 * 1024 ; 1 GB
FO := FileOpen(FileDrive . "\" . FileName, "w")
FO.Length := FileSize
FO.Close()
lexikos
Posts: 6676
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: How to generate a large sparse file natively only with ahk?

09 Jan 2017, 03:08

This is my take.

Code: Select all

FileOpen("C:\filler", "w").Length := 1024**3
jeeswg wrote:DllCall("GetDiskFreeSpaceEx" ...
Is there something wrong with the built-in DriveSpaceFree command?
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to generate a large sparse file natively only with ahk?

09 Jan 2017, 03:12

Wow! That FileOpen use is really nice. No '#MaxMem 1048576' problems there.
I was wondering if it could be done by DllCall.
Reminds me of an issue also if whether the data in 2 files can be compared,
without copying them into AutoHotkey's memory first.

Re. DriveSpaceFree
No, but DriveSpaceFree only handles MB.
And so if you wanted more exact control of free space,
bytes is the way to go!
Last edited by jeeswg on 09 Jan 2017, 03:37, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: How to generate a large sparse file natively only with ahk?

09 Jan 2017, 03:29

THANKS A SHIT TON GUYS, 'just me' provided a working solution,albeit with a few tweaks,this was basically how I did it before:

Code: Select all


; freeSpaceTailor("k", "218")
freeSpaceTailor(targetDriveLetter, targetFreeSpaceInMB)		;218 MB results in 606MB of actual free space on disk "FORt alpha"
{
DriveSpaceFree, dfspace, %targetDriveLetter%:\
; MsgBox,
; (
; disk free space: %dfspace%
; desired free space: %targetFreeSpaceInMB%
; )
sparseByteLength:= dfspace - targetFreeSpaceInMB
; MsgBox, Sparse length in MB: %sparseByteLength%
sparseByteLength *= 1000	;mb>kb
sparseByteLength *= 1000	;kb>bytes
RunWait, %comspec% /c fsutil file createnew "%targetDriveLetter%:\sparse.EFC" %sparseByteLength%
}

function basically gets called every time an "undesirable number" represented free space,a basic DriveSpaceFree check rounded to GB used to retrieve it, then a it's basic addition & subtraction to figure out a suitable size for the sparse file taking into account it's former size,I basically use the same logic to automatically adjust the volume on the pc.

only 'problem' with my previous approach was I had to use a static actively modified free space,instead of rounding to the nearest even number or palindrome,I couldn't do that as fsutil required a significant delay between creating sparse files to accurately generate a file of specified length.

And the reason why I wanted a sparse file was to avoid any write delay to disk as would be the case with actually writing physical data.


Thanks again folks. :mrgreen:
Outsourcing Clicks & Presses Since 2004.

Return to “Ask For Help”

Who is online

Users browsing this forum: f1ster, flyingDman, lakozyin and 238 guests