找回密码
 加入
搜索
查看: 8024|回复: 15

[效率算法] 如何创建大小固定的文件? [已解决]

  [复制链接]
发表于 2010-8-8 09:52:18 | 显示全部楼层 |阅读模式
本帖最后由 republican 于 2010-8-8 19:08 编辑

搞掂了HTTP断点续传,自然多点下载也不成问题了。
但问题是,如何创建固定大小的文件?

我现在用的是下列命令:
If Not FileExists($FileFullName) Then RunWait("cmd.exe /c fsutil file createnew "&$FileFullName&" "&$TotalFileLen,"",@SW_HIDE)        ;创建文件
不知道Au3应该如何做?


PS:
分享一个本地文件复制的过程,给有需要的人用:
Func _CopyFileBin($SourceFile,$DestinationFile,$FileLen=0,$FilePos=0)
        $sFile=FileOpen($SourceFile,16)
        If $sFile=-1 Then 
                SetError(1,0,0)
                Return
        EndIf
        
        If $FileLen=0 Then 
                $FileLen=FileGetSize($SourceFile)
        ElseIf $FileLen=-1 Then
                $FileLen=FileGetSize($SourceFile) - $FilePos
        EndIf
        
        If Not FileExists($DestinationFile) Then RunWait("cmd.exe /c fsutil file createnew "&$DestinationFile&" "&FileGetSize($SourceFile),"",@SW_HIDE)
        $dFile=FileOpen($DestinationFile,9)
        If $sFile=-1 Then 
                SetError(2,0,0)
                Return
        EndIf
        
        FileSetPos($sFile,$FilePos,0)
        FileSetPos($dFile,$FIlePos,0)
        FileWrite($dFile,FileRead($sFile,$FileLen))
        FileClose($dFile)
        FileClose($sFile)
EndFunc

评分

参与人数 1金钱 +10 收起 理由
afan + 10 感谢主动将修改帖子分类为[已解决],请继续 ...

查看全部评分

发表于 2010-8-8 17:18:06 | 显示全部楼层
每次写入1字节 效率很低
$Size = "1048576" ;1M
$file = "file.tmp"

; 每次写入1字节
If FileExists($file) Then FileWrite($file, " ")
$file = FileOpen ($file,1)
If FileGetSize($file) < $Size Then
        $Size = $Size - FileGetSize($file)
        For $i = 1 To $Size
                FileWrite($file, " ")
        Next
ElseIf FileGetSize($file) = $Size Then
        Exit
EndIf
 楼主| 发表于 2010-8-8 18:19:00 | 显示全部楼层
就是因为效率低我才需要用dos命令,用1k的字节填满200M需要2.2s,用fsutil仅需0.02s.效率啊.
 楼主| 发表于 2010-8-8 19:07:48 | 显示全部楼层
解决了,远在天边,近在眼前,就是用我给出的copyfilebin的思想。

30G的文件0.02s完结.果然有效率.....
发表于 2010-8-8 19:08:46 | 显示全部楼层
本帖最后由 xayle 于 2010-8-8 19:11 编辑
解决了,远在天边,近在眼前,就是用我给出的copyfilebin的思想。

30G的文件0.02s完结.果然有效率.....
republican 发表于 2010-8-8 19:07


官方网站上, 搜到的也是系统自带的命令最有效率

你发的copyfilebin 也是用的系统命令..
 楼主| 发表于 2010-8-8 19:11:58 | 显示全部楼层
本帖最后由 republican 于 2010-8-8 19:13 编辑

十分感谢!!

不过看看第六十六行,还是用fsutil.
 楼主| 发表于 2010-8-8 19:14:22 | 显示全部楼层
本帖最后由 republican 于 2010-8-8 19:21 编辑

回复 5# xayle

Au3内置的命令.

调用DOS,返回的信息还要截取,还有不稳定的情况。

只要能避免调用DOS就算OK了,我是这样想的.你觉得如何?
发表于 2010-8-8 19:22:44 | 显示全部楼层
本帖最后由 xayle 于 2010-8-8 19:24 编辑

回复 7# republican

官方的例子是调用_RunDOS() ,
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Process.au3>

;// options
#NoTrayIcon

;// vars
$path = "c:\dummy.bin"

;// gui

$main_gui = GUICreate("Dummy file maker", 353, 96, -1, -1)
$browse_input = GUICtrlCreateInput($path, 8, 24, 313, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
$browse_button = GUICtrlCreateButton("...", 320, 24, 27, 21, 0)
$size_input = GUICtrlCreateInput("0", 8, 64, 105, 21)
$Updown1 = GUICtrlCreateUpdown($size_input)
$store_label = GUICtrlCreateLabel("Store location. Avaliable free space on drive: " & Round(DriveSpaceFree(StringLeft(GUICtrlRead($browse_input), 3)), 1) & "MB", 8, 8, 353, 15)
$Label2 = GUICtrlCreateLabel("Size", 8, 48, 24, 15)
$type_input = GUICtrlCreateCombo("KB", 120, 64, 65, 25)
GUICtrlSetData(-1, "MB|GB")
$create_button = GUICtrlCreateButton("Create", 192, 64, 75, 25, 0)
$exit_button = GUICtrlCreateButton("Exit", 272, 64, 75, 25, 0)

GUISetState(@SW_SHOW)

;// main loop
Do
$msg = GUIGetMsg()
If $msg = $browse_button then
$path = filesaveDialog("Save dummy file to ...", "c:", "All (*.*)", 2, "dummy.bin")
If FileExists($path) then
MsgBox(0, "Dummy file maker", "File must NOT exist.")
Else
If $path = "" then
$path = "c:\dummy.bin"
GUICtrlSetData($browse_input, $path)
GUICtrlSetData($store_label, "Store location. Avaliable free space on drive: " & Round(DriveSpaceFree(StringLeft(GUICtrlRead($browse_input), 3)), 1) & "MB")
Else
GUICtrlSetData($browse_input, $path)
GUICtrlSetData($store_label, "Store location. Avaliable free space on drive: " & Round(DriveSpaceFree(StringLeft(GUICtrlRead($browse_input), 3)), 1) & "MB")
EndIf
EndIF
EndIf
If $msg = $create_button then
$size = GUICtrlRead($size_input)
$size_check = $size
If GUICtrlRead($type_input) = "KB" then
$size = $size * 1024
EndIf
If GUICtrlRead($type_input) = "MB" then
$size = $size * 1024 * 1024
EndIf
If GUICtrlRead($type_input) = "GB" then
$size = $size * 1024 * 1024 * 1024
EndIf
If $size <= 0 then
MsgBox(0, "Dummy file maker", "Desired size cannot be negative or 0 (zero).")
Else
$answer = MsgBox(4, "Dummy file maker", "Please confirm the folowing:" & @CRLF & "Dummy file size: " & $size & "KB" & @CRLF & "Destination: " & GUICtrlRead($browse_input))
If $answer = 6 then
_RunDOS("fsutil file createnew """ & GUICtrlRead($browse_input) & """z " & $size)
MsgBox(0, "Dummy file maker", "Creation completed.")
Else
MsgBox(0, "Dummy file maker", "Creation aborted.")
EndIf
EndIf
EndIf
Until $msg = $GUI_EVENT_CLOSE or $msg = $exit_button

评分

参与人数 1金钱 +20 收起 理由
afan + 20

查看全部评分

 楼主| 发表于 2010-8-8 19:25:35 | 显示全部楼层
本帖最后由 republican 于 2010-8-8 19:41 编辑

不卖关子了.
刚才已经PM你了..

附自己写的创建函数.
Func _CreateSizeFile($FileName,$FileSize,$cMode=0)
        If $cMode = 1 Then
                If FileExists($FileName) Then Return SetError(1,0,0)
        EndIf
        $hFile=FileOpen($FileName,18)
        If $hFile = -1 Then Return SetError(2,0,0)
        If $FileSize < 0 Then Return SetError(3,0,0)
        $cSize=FileSetPos($hFile,$FileSize - 4,0)
        If $cSize= False Then Return SetError(4,0,0)
        $cSize=FileWrite($hFile,Binary(0x00))
        If $cSize= False Then Return SetError(5,0,0)
        FileClose($hFile)
        Return 1
EndFunc
FileName;文件名
FileSize  ;文件大小(字节)
cMode    ;是否检查文件已存在


继续研究多线程资料...

评分

参与人数 1金钱 +20 收起 理由
afan + 20

查看全部评分

发表于 2010-8-8 19:28:31 | 显示全部楼层
我认为不需要获得命令的返回信息, 先获得目标磁盘的剩余空间, 在用_rundos() 执行
 楼主| 发表于 2010-8-8 19:30:56 | 显示全部楼层
回复 10# xayle

如果DOS挂掉呢?不是程序的作者,怎么能保证fsutil一定能成功创建?

所以能不调用外部程序就不调用外部程序。
发表于 2010-8-8 19:37:58 | 显示全部楼层
本帖最后由 xayle 于 2010-8-8 19:58 编辑

回复 9# republican


    这个效率搞了很多
100G 0.03左右
 楼主| 发表于 2010-8-8 19:45:44 | 显示全部楼层
问题解决就好了~~

谢谢你的帮助!

我估计1TB也是瞬间的事。

不知道指定偏移的资源占用是否与偏移量成比,如果不是(在我看来不成正比)的话就看硬盘的随机寻道时间了。
发表于 2010-8-8 19:49:01 | 显示全部楼层
本帖最后由 xayle 于 2010-8-8 19:53 编辑

回复 13# republican


    technet 上这个命令的解释offset
也是偏移
 楼主| 发表于 2010-8-8 19:56:27 | 显示全部楼层
回复 14# xayle

我也是在网上查了资料后忽然想起偏移的事。

因为在写CopyFileBin的过程中时,我发现偏移是可以任意指定的,即便大于文件大小也完全可以,不会有错误发生,遂进行试验,果真如此。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-10 11:30 , Processed in 0.083018 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表