找回密码
 加入
搜索
查看: 3076|回复: 9

[AU3基础] (已解决)批量创建文件,再次谢谢3MILE哥,chenronting,xiehuahere,user3000

  [复制链接]
发表于 2011-11-10 21:11:53 | 显示全部楼层 |阅读模式
本帖最后由 xyhqqaa 于 2011-11-11 02:34 编辑

新手求助.最近遇到个问题.我想批量创建XX类型的文件.但是不知道使用_filecreat跟_fileanddirctreat如何实现批量生成文件.而且文件名长度要一致..囧..比如我要生成100个TXT文件.第一个为新建文件001.txt或者新建文件(001).txt  ,第二个为新建文件002...................假如我要生成1000个TXT文件..那么名称生成又为新建文件0001.txt..........依次类推....其实最纠结的是这个文件名的生成..使用批处理是可以生成文件.但是文件名俺就不知道该如何生成刚才的样式...跪求批处理生成方式也行..
  1. #include <ButtonConstants.au3>
  2. #include <ComboConstants.au3>
  3. #include <EditConstants.au3>
  4. #include <GUIConstantsEx.au3>
  5. #include <StaticConstants.au3>
  6. #include <WindowsConstants.au3>
  7. #include<file.au3>
  8. #include <array.au3>

  9. #Region ### START Koda GUI section ### Form=
  10. $Form1 = GUICreate("Form1", 369, 251, 192, 114, -1, BitOR($WS_EX_ACCEPTFILES,$WS_EX_WINDOWEDGE))
  11. $Form1context = GUICtrlCreateContextMenu()
  12. $MenuItem1 = GUICtrlCreateMenuItem("清空", $Form1context)
  13. $Group1 = GUICtrlCreateGroup("Group1", 0, 8, 361, 153)
  14. $Label1 = GUICtrlCreateLabel("路径:", 8, 40, 48, 17)
  15. $Label2 = GUICtrlCreateLabel("文件名:", 8, 80, 52, 17)
  16. $Input1 = GUICtrlCreateInput("", 64, 32, 209, 28)
  17. GUICtrlSetState(-1,$GUI_ACCEPTFILES)
  18. GUICtrlSetFont(-1, 13, 400, 0, "MS Sans Serif")
  19. $Input2 = GUICtrlCreateInput("", 64, 72, 209, 28)
  20. GUICtrlSetFont(-1, 13, 400, 0, "MS Sans Serif")
  21. $Button1 = GUICtrlCreateButton("浏览", 288, 32, 65, 25)
  22. $Button1context = GUICtrlCreateContextMenu($Button1)

  23. $Label3 = GUICtrlCreateLabel("数量:", 8, 120, 40, 17)
  24. $Input3 = GUICtrlCreateInput("", 64, 112, 73, 28)
  25. GUICtrlSetFont(-1, 13, 400, 0, "MS Sans Serif")
  26. $Combo1 = GUICtrlCreateCombo("", 280, 72, 73, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL, $CBS_SIMPLE))
  27. GUICtrlSetData(-1, ".txt|.jpg|.doc|.xls|.exe")
  28. GUICtrlSetFont(-1, 13, 400, 0, "MS Sans Serif")
  29. GUICtrlCreateGroup("", -99, -99, 1, 1)
  30. $Button2 = GUICtrlCreateButton("批量创建", 56, 176, 257, 65)




  31. GUISetState(@SW_SHOW)
  32. #EndRegion ### END Koda GUI section ###



  33. While 1
  34.         $nMsg = GUIGetMsg()
  35.         Switch $nMsg
  36.                 Case $GUI_EVENT_CLOSE
  37.                         Exit
  38.                 Case $Button1
  39.                         $files = FileSelectFolder("选择相应文件夹",@WorkingDir)
  40.                         GUICtrlSetData($input1,$files)
  41. Case $Button2
  42. $way = GUICtrlRead($input1)
  43. $name = GUICtrlRead($input2)
  44. $style = GUICtrlRead($Combo1)
  45. _FileAndDirCreate($way&""&$name&$style)
  46. case $MenuItem1
  47. GUICtrlSetData($input1,"")
  48.         EndSwitch
  49. WEnd




  50. Func _FileAndDirCreate($FilePach)
  51.         Dim $szDrive, $szDir, $szFName, $szExt
  52.         $arrPath = _PathSplit($FilePach, $szDrive, $szDir, $szFName, $szExt)
  53.         If Not FileExists($FilePach) Then
  54.                 If $szExt = '' Then
  55.                         DirCreate($FilePach)
  56.                 Else
  57.                         _FileCreate($FilePach)
  58.                 EndIf
  59.         Else
  60.                 For $i = 0001 To 1000
  61.                         If Not FileExists($szDrive & $szDir & $szFName & '(' & $i & ')' & $szExt) Then ExitLoop
  62.                 Next
  63.                 If $szExt = '' Then
  64.                         DirCreate($szDrive & $szDir & $szFName & '(' & $i & ')')
  65.                 Else
  66.                         _FileCreate($szDrive & $szDir & $szFName & '(' & $i & ')' & $szExt)
  67.                 EndIf
  68.                         EndIf
  69.                          MsgBox("","",$szDrive)
  70. EndFunc   ;==>_FileAndDirCreate
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2011-11-10 21:32:53 | 显示全部楼层
那我给出关键的部分吧,希望你能看的懂
#Include <File.au3>

$wenjianleixing='txt'
Global $kaishi=1
Global $jieshu = 11
For $i = $kaishi To $jieshu
        _FileCreate(@ScriptDir & '\' & $i & '.' & $wenjianleixing)
        ;While Not FileExists(@ScriptDir & '\' & $i & '.' & $wenjianleixing)
                ;Sleep(100)
        ;WEnd
        Sleep(100)
Next


中间的是注释掉的,你如果不需要就去掉吧,一个思路罢了。

评分

参与人数 1金钱 +10 收起 理由
xyhqqaa + 10 大哥...谢谢你帮我解决生成文件的部分..试了 ...

查看全部评分

发表于 2011-11-10 21:48:29 | 显示全部楼层
不要意思,又看了一下你的说明,感觉我些的不对头,不过实在没有真正明白楼主的意思。请说明白点,谢谢
 楼主| 发表于 2011-11-10 22:23:48 | 显示全部楼层
回复 3# chenronting


我的想法是,我想批量生成文件.自动生成的文件假如为少99个..则命名为新建文档01.txt ,新建文档02.txt,新建文档03.txt  ..........新建文档99.txt ,文件字符长度要一样...假如我新建100-999个.则文件名新建文档001.txt,新建文档002.txt,新建文档003.txt........新建文档999.txt,...刚好生成的字符为三位.这样命名长度一样...依次类推
    囧..不知道这样说明白了没..不过谢谢你的帮助,我稍微有点头绪...剩下就是循环判断生成文件个数命来名文件.......
 楼主| 发表于 2011-11-10 22:55:25 | 显示全部楼层
现在有点头大...当文件个数1-9时候.文件名不加0 ,当文件数9-99的时候 1-9的前面个数需要加0.但排除10-99.
.当文件个数100-999的时候 1-9前面需要加00 ,10-99前面需要加0 但排除100-999....这判断该怎么同时判断..生成..大神.大哥,大爷们...新手迷茫死了
发表于 2011-11-10 23:24:22 | 显示全部楼层
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include<file.au3>
#include <array.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 369, 251, 192, 114, -1, BitOR($WS_EX_ACCEPTFILES, $WS_EX_WINDOWEDGE))
$Form1context = GUICtrlCreateContextMenu()
$MenuItem1 = GUICtrlCreateMenuItem("清空", $Form1context)
$Group1 = GUICtrlCreateGroup("Group1", 0, 8, 361, 153)
$Label1 = GUICtrlCreateLabel("路径:", 8, 40, 48, 17)
$Label2 = GUICtrlCreateLabel("文件名:", 8, 80, 52, 17)
$Input1 = GUICtrlCreateInput("", 64, 32, 209, 28)
GUICtrlSetState(-1, $GUI_ACCEPTFILES)
GUICtrlSetFont(-1, 13, 400, 0, "MS Sans Serif")
$Input2 = GUICtrlCreateInput("", 64, 72, 209, 28)
GUICtrlSetFont(-1, 13, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("浏览", 288, 32, 65, 25)
$Button1context = GUICtrlCreateContextMenu($Button1)

$Label3 = GUICtrlCreateLabel("数量:", 8, 120, 40, 17)
$Input3 = GUICtrlCreateInput("", 64, 112, 73, 28)
GUICtrlSetFont(-1, 13, 400, 0, "MS Sans Serif")
$Combo1 = GUICtrlCreateCombo("", 280, 72, 73, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL, $CBS_SIMPLE))
GUICtrlSetData(-1, ".txt|.jpg|.doc|.xls|.exe")
GUICtrlSetFont(-1, 13, 400, 0, "MS Sans Serif")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button2 = GUICtrlCreateButton("批量创建", 56, 176, 257, 65)




GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###



While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        $files = FileSelectFolder("选择相应文件夹", @WorkingDir)
                        GUICtrlSetData($Input1, $files)
                Case $Button2
                        $way = GUICtrlRead($Input1)
                        $name = GUICtrlRead($Input2)
                        $style = GUICtrlRead($Combo1)
                        _FileAndDirCreate($way & "\" & $name & $style)
                Case $MenuItem1
                        GUICtrlSetData($Input1, "")
        EndSwitch
WEnd




Func _FileAndDirCreate($FilePach)
        Dim $szDrive, $szDir, $szFName, $szExt
        $arrPath = _PathSplit($FilePach, $szDrive, $szDir, $szFName, $szExt)
        If Not FileExists($FilePach) Then
                If $szExt = '' Then
;~                         DirCreate($FilePach)
                Else
;~                         _FileCreate($FilePach)
                EndIf
        Else
                Local $str
                Local $len = StringLen(GUICtrlRead($Input3))
                For $i = 1 To GUICtrlRead($Input3)
                        $str &= $FilePach & StringFormat('%0' & $len & 'd', $i) & @CRLF
                Next
                MsgBox(0, 0, $str)
        EndIf
EndFunc   ;==>_FileAndDirCreate

评分

参与人数 1金钱 +40 收起 理由
xyhqqaa + 40 谢谢...俺会好好学习..做伸手党不是好事..囧 ...

查看全部评分

发表于 2011-11-10 23:31:43 | 显示全部楼层
同样给一个思路。假如是少于99个:
  1. #Include <File.au3>

  2. For $i = 0 To 99
  3.      _FileCreate(@ScriptDir & '\' & StringFormat("%02u", $i) & '.txt')
  4.      Sleep(100)
  5. Next
复制代码

评分

参与人数 1金钱 +10 收起 理由
xyhqqaa + 10 谢谢.....又学到了新知识..谢谢

查看全部评分

发表于 2011-11-10 23:32:04 | 显示全部楼层
真佩服你了, 都混到中级会员了, 连AU3基础都还不会!
判断不是有判断的语句吗?
if ... then...  
select... endselect

但用函数 stringformat 更能简洁达到你的要求.

评分

参与人数 1金钱 +10 收起 理由
xyhqqaa + 10 谢谢批评- - 囧..会好好学习

查看全部评分

 楼主| 发表于 2011-11-10 23:42:47 | 显示全部楼层
囧....水人一个.只会发水帖.....楼上.您批评的是...正在慢慢学习..  囧...俺错了....伸手影响独立思考....以后努力自己动手...还有谢谢大家.谢谢3mile哥.谢谢xiehuahere..谢谢chenronting.....俺新手会努力的
 楼主| 发表于 2011-11-11 00:31:07 | 显示全部楼层
本帖最后由 xyhqqaa 于 2011-11-11 00:55 编辑

代码整理如下..  不知道会不会乱七八糟的- -///
看懂StringFormat这参数估计要我蛋疼几天...继续努力..谢谢大家
  1. #NoTrayIcon
  2. #include <ButtonConstants.au3>
  3. #include <ComboConstants.au3>
  4. #include <EditConstants.au3>
  5. #include <GUIConstantsEx.au3>
  6. #include <StaticConstants.au3>
  7. #include <WindowsConstants.au3>
  8. #include<file.au3>
  9. #include <array.au3>
  10. #region ### START Koda GUI section ### Form=
  11. $Form1 = GUICreate("Form1", 369, 251, 192, 114, -1, BitOR($WS_EX_ACCEPTFILES, $WS_EX_WINDOWEDGE))
  12. $Form1context = GUICtrlCreateContextMenu()
  13. $MenuItem1 = GUICtrlCreateMenuItem("清空", $Form1context)
  14. $MenuItem2 = GUICtrlCreateMenuItem("", $Form1context)
  15. $MenuItem3 = GUICtrlCreateMenuItem("退出", $Form1context)
  16. $Group1 = GUICtrlCreateGroup("Group1", 0, 8, 361, 153)
  17. $Label1 = GUICtrlCreateLabel("路径:", 8, 40, 48, 17)
  18. $Label2 = GUICtrlCreateLabel("文件名:", 8, 80, 52, 17)
  19. $Input1 = GUICtrlCreateInput("", 64, 32, 209, 28)
  20. GUICtrlSetState(-1, $GUI_ACCEPTFILES)
  21. GUICtrlSetFont(-1, 13, 400, 0, "MS Sans Serif")
  22. $Input2 = GUICtrlCreateInput("", 64, 72, 209, 28)
  23. GUICtrlSetFont(-1, 13, 400, 0, "MS Sans Serif")
  24. $Button1 = GUICtrlCreateButton("浏览", 288, 32, 65, 25)
  25. $Button1context = GUICtrlCreateContextMenu($Button1)
  26. $Label3 = GUICtrlCreateLabel("数量:", 8, 120, 40, 17)
  27. $Input3 = GUICtrlCreateInput("", 64, 112, 73, 28)
  28. GUICtrlSetFont(-1, 13, 400, 0, "MS Sans Serif")
  29. $Combo1 = GUICtrlCreateCombo("", 280, 72, 73, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL, $CBS_SIMPLE))
  30. GUICtrlSetData(-1, ".txt|.jpg|.doc|.xls|.exe||")
  31. GUICtrlSetFont(-1, 13, 400, 0, "MS Sans Serif")
  32. GUICtrlCreateGroup("", -99, -99, 1, 1)
  33. $Button2 = GUICtrlCreateButton("批量创建", 56, 176, 257, 65)
  34. GUISetState(@SW_SHOW)
  35. #endregion ### END Koda GUI section ###

  36. While 1
  37.         $nMsg = GUIGetMsg()
  38.         Switch $nMsg
  39.                 Case $GUI_EVENT_CLOSE
  40.                         Exit
  41.                 Case $Button1
  42.                         $files = FileSelectFolder("选择相应文件夹", @WorkingDir)
  43.                         GUICtrlSetData($Input1, $files)
  44.                 Case $Button2
  45.                         If GUICtrlRead($Input1) = "" And GUICtrlRead($Input2) = "" Then
  46.                                 MsgBox("", "", "不允许为空啦")
  47.                        
  48.                 ElseIf GUICtrlRead($Input3) = "" Then
  49.                                 MsgBox("", "", "请填写数量哦")
  50.                         Else
  51.                                 _hui()
  52.                                MsgBox("", "", "生成完毕")
  53.                         EndIf
  54.                 Case $MenuItem1
  55.                         GUICtrlSetData($Input1, "")
  56.                 Case $MenuItem3
  57.                         Exit
  58.         EndSwitch
  59. WEnd


  60. Func _hui()
  61.         $style = GUICtrlRead($Combo1)
  62.         $way = GUICtrlRead($Input1)
  63.         $name = GUICtrlRead($Input2)
  64.         $number = GUICtrlRead($Input3)
  65.         $len = StringLen($number)
  66.         For $i = 1 To $number
  67.                 _FileCreate($way & "" & $name & "-" & StringFormat('%0' & $len & 'd', $i) & $style)

  68.         Next
  69. EndFunc   ;==>_hui
复制代码
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2026-9-21 00:37 , Processed in 0.067396 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2026 Discuz! Team.

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