File.au3 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. #include-once
  2. #include "Array.au3"
  3. #include "FileConstants.au3"
  4. #include "StringConstants.au3"
  5. ; #INDEX# =======================================================================================================================
  6. ; Title .........: File
  7. ; AutoIt Version : 3.3.14.5
  8. ; Language ......: English
  9. ; Description ...: Functions that assist with files and directories.
  10. ; Author(s) .....: Brian Keene, Michael Michta, erifash, Jon, JdeB, Jeremy Landes, MrCreatoR, cdkid, Valik, Erik Pilsits, Kurt, Dale, guinness, DXRW4E, Melba23
  11. ; ===============================================================================================================================
  12. ; #CURRENT# =====================================================================================================================
  13. ; _FileCountLines
  14. ; _FileCreate
  15. ; _FileListToArray
  16. ; _FileListToArrayRec
  17. ; _FilePrint
  18. ; _FileReadToArray
  19. ; _FileWriteFromArray
  20. ; _FileWriteLog
  21. ; _FileWriteToLine
  22. ; _PathFull
  23. ; _PathGetRelative
  24. ; _PathMake
  25. ; _PathSplit
  26. ; _ReplaceStringInFile
  27. ; _TempFile
  28. ; ===============================================================================================================================
  29. ; #INTERNAL_USE_ONLY#============================================================================================================
  30. ; __FLTAR_ListToMask
  31. ; __FLTAR_AddToList
  32. ; __FLTAR_AddFileLists
  33. ; ===============================================================================================================================
  34. ; #FUNCTION# ====================================================================================================================
  35. ; Author ........: Tylo <tylo at start dot no>
  36. ; Modified.......: Xenobiologist, Gary, guinness, DXRW4E
  37. ; ===============================================================================================================================
  38. Func _FileCountLines($sFilePath)
  39. FileReadToArray($sFilePath)
  40. If @error Then Return SetError(@error, @extended, 0)
  41. Return @extended
  42. #cs
  43. Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
  44. If $hFileOpen = -1 Then Return SetError(1, 0, 0)
  45. Local $sFileRead = StringStripWS(FileRead($hFileOpen), $STR_STRIPTRAILING)
  46. FileClose($hFileOpen)
  47. Return UBound(StringRegExp($sFileRead, "\R", $STR_REGEXPARRAYGLOBALMATCH)) + 1 - Int($sFileRead = "")
  48. #ce
  49. EndFunc ;==>_FileCountLines
  50. ; #FUNCTION# ====================================================================================================================
  51. ; Author ........: Brian Keene <brian_keene at yahoo dot com>
  52. ; Modified.......:
  53. ; ===============================================================================================================================
  54. Func _FileCreate($sFilePath)
  55. Local $hFileOpen = FileOpen($sFilePath, BitOR($FO_OVERWRITE, $FO_CREATEPATH))
  56. If $hFileOpen = -1 Then Return SetError(1, 0, 0)
  57. Local $iFileWrite = FileWrite($hFileOpen, "")
  58. FileClose($hFileOpen)
  59. If Not $iFileWrite Then Return SetError(2, 0, 0)
  60. Return 1
  61. EndFunc ;==>_FileCreate
  62. ; #FUNCTION# ====================================================================================================================
  63. ; Author ........: Michael Michta
  64. ; Modified.......: guinness - Added optional parameter to return the full path.
  65. ; ===============================================================================================================================
  66. Func _FileListToArray($sFilePath, $sFilter = "*", $iFlag = $FLTA_FILESFOLDERS, $bReturnPath = False)
  67. Local $sDelimiter = "|", $sFileList = "", $sFileName = "", $sFullPath = ""
  68. ; Check parameters for the Default keyword or they meet a certain criteria
  69. $sFilePath = StringRegExpReplace($sFilePath, "[\\/]+$", "") & "\" ; Ensure a single trailing backslash
  70. If $iFlag = Default Then $iFlag = $FLTA_FILESFOLDERS
  71. If $bReturnPath Then $sFullPath = $sFilePath
  72. If $sFilter = Default Then $sFilter = "*"
  73. ; Check if the directory exists
  74. If Not FileExists($sFilePath) Then Return SetError(1, 0, 0)
  75. If StringRegExp($sFilter, "[\\/:><\|]|(?s)^\s*$") Then Return SetError(2, 0, 0)
  76. If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 0, 0)
  77. Local $hSearch = FileFindFirstFile($sFilePath & $sFilter)
  78. If @error Then Return SetError(4, 0, 0)
  79. While 1
  80. $sFileName = FileFindNextFile($hSearch)
  81. If @error Then ExitLoop
  82. If ($iFlag + @extended = 2) Then ContinueLoop
  83. $sFileList &= $sDelimiter & $sFullPath & $sFileName
  84. WEnd
  85. FileClose($hSearch)
  86. If $sFileList = "" Then Return SetError(4, 0, 0)
  87. Return StringSplit(StringTrimLeft($sFileList, 1), $sDelimiter)
  88. EndFunc ;==>_FileListToArray
  89. ; #FUNCTION# ====================================================================================================================
  90. ; Author ........: Melba23 - with credits for code snippets to Ultima, Partypooper, Spiff59, guinness, wraithdu
  91. ; Modified ......:
  92. ; ===============================================================================================================================
  93. Func _FileListToArrayRec($sFilePath, $sMask = "*", $iReturn = $FLTAR_FILESFOLDERS, $iRecur = $FLTAR_NORECUR, $iSort = $FLTAR_NOSORT, $iReturnPath = $FLTAR_RELPATH)
  94. If Not FileExists($sFilePath) Then Return SetError(1, 1, "")
  95. ; Check for Default keyword
  96. If $sMask = Default Then $sMask = "*"
  97. If $iReturn = Default Then $iReturn = $FLTAR_FILESFOLDERS
  98. If $iRecur = Default Then $iRecur = $FLTAR_NORECUR
  99. If $iSort = Default Then $iSort = $FLTAR_NOSORT
  100. If $iReturnPath = Default Then $iReturnPath = $FLTAR_RELPATH
  101. ; Check for valid recur value
  102. If $iRecur > 1 Or Not IsInt($iRecur) Then Return SetError(1, 6, "")
  103. Local $bLongPath = False
  104. ; Check for valid path
  105. If StringLeft($sFilePath, 4) == "\\?\" Then
  106. $bLongPath = True
  107. EndIf
  108. Local $sFolderSlash = ""
  109. ; Check if folders should have trailing \ and ensure that initial path does have one
  110. If StringRight($sFilePath, 1) = "\" Then
  111. $sFolderSlash = "\"
  112. Else
  113. $sFilePath = $sFilePath & "\"
  114. EndIf
  115. Local $asFolderSearchList[100] = [1]
  116. ; Add path to folder search list
  117. $asFolderSearchList[1] = $sFilePath
  118. Local $iHide_HS = 0, _
  119. $sHide_HS = ""
  120. ; Check for H or S omitted
  121. If BitAND($iReturn, $FLTAR_NOHIDDEN) Then
  122. $iHide_HS += 2
  123. $sHide_HS &= "H"
  124. $iReturn -= $FLTAR_NOHIDDEN
  125. EndIf
  126. If BitAND($iReturn, $FLTAR_NOSYSTEM) Then
  127. $iHide_HS += 4
  128. $sHide_HS &= "S"
  129. $iReturn -= $FLTAR_NOSYSTEM
  130. EndIf
  131. Local $iHide_Link = 0
  132. ; Check for link/junction omitted
  133. If BitAND($iReturn, $FLTAR_NOLINK) Then
  134. $iHide_Link = 0x400
  135. $iReturn -= $FLTAR_NOLINK
  136. EndIf
  137. Local $iMaxLevel = 0
  138. ; If required, determine \ count for max recursive level setting
  139. If $iRecur < 0 Then
  140. StringReplace($sFilePath, "\", "", 0, $STR_NOCASESENSEBASIC)
  141. $iMaxLevel = @extended - $iRecur
  142. EndIf
  143. Local $sExclude_List = "", $sExclude_List_Folder = "", $sInclude_List = "*"
  144. ; Check mask parameter
  145. Local $aMaskSplit = StringSplit($sMask, "|")
  146. ; Check for multiple sections and set values
  147. Switch $aMaskSplit[0]
  148. Case 3
  149. $sExclude_List_Folder = $aMaskSplit[3]
  150. ContinueCase
  151. Case 2
  152. $sExclude_List = $aMaskSplit[2]
  153. ContinueCase
  154. Case 1
  155. $sInclude_List = $aMaskSplit[1]
  156. EndSwitch
  157. Local $sInclude_File_Mask = ".+"
  158. ; Create Include mask for files
  159. If $sInclude_List <> "*" Then
  160. If Not __FLTAR_ListToMask($sInclude_File_Mask, $sInclude_List) Then Return SetError(1, 2, "")
  161. EndIf
  162. Local $sInclude_Folder_Mask = ".+"
  163. ; Set Include mask for folders
  164. Switch $iReturn
  165. Case 0
  166. ; Folders affected by mask if not recursive
  167. Switch $iRecur
  168. Case 0
  169. ; Folders match mask for compatibility
  170. $sInclude_Folder_Mask = $sInclude_File_Mask
  171. EndSwitch
  172. Case 2
  173. ; Folders affected by mask
  174. $sInclude_Folder_Mask = $sInclude_File_Mask
  175. EndSwitch
  176. Local $sExclude_File_Mask = ":"
  177. ; Create Exclude List mask for files
  178. If $sExclude_List <> "" Then
  179. If Not __FLTAR_ListToMask($sExclude_File_Mask, $sExclude_List) Then Return SetError(1, 3, "")
  180. EndIf
  181. Local $sExclude_Folder_Mask = ":"
  182. ; Create Exclude mask for folders
  183. If $iRecur Then
  184. If $sExclude_List_Folder Then
  185. If Not __FLTAR_ListToMask($sExclude_Folder_Mask, $sExclude_List_Folder) Then Return SetError(1, 4, "")
  186. EndIf
  187. ; If folders only
  188. If $iReturn = 2 Then
  189. ; Folders affected by normal mask
  190. $sExclude_Folder_Mask = $sExclude_File_Mask
  191. EndIf
  192. Else
  193. ; Folders affected by normal mask
  194. $sExclude_Folder_Mask = $sExclude_File_Mask
  195. EndIf
  196. ; Verify other parameters
  197. If Not ($iReturn = 0 Or $iReturn = 1 Or $iReturn = 2) Then Return SetError(1, 5, "")
  198. If Not ($iSort = 0 Or $iSort = 1 Or $iSort = 2) Then Return SetError(1, 7, "")
  199. If Not ($iReturnPath = 0 Or $iReturnPath = 1 Or $iReturnPath = 2) Then Return SetError(1, 8, "")
  200. ; Prepare for DllCall if required
  201. If $iHide_Link Then
  202. Local $tFile_Data = DllStructCreate("struct;align 4;dword FileAttributes;uint64 CreationTime;uint64 LastAccessTime;uint64 LastWriteTime;" & _
  203. "dword FileSizeHigh;dword FileSizeLow;dword Reserved0;dword Reserved1;wchar FileName[260];wchar AlternateFileName[14];endstruct")
  204. Local $hDLL = DllOpen('kernel32.dll'), $aDLL_Ret
  205. EndIf
  206. Local $asReturnList[100] = [0]
  207. Local $asFileMatchList = $asReturnList, $asRootFileMatchList = $asReturnList, $asFolderMatchList = $asReturnList
  208. Local $bFolder = False, _
  209. $hSearch = 0, _
  210. $sCurrentPath = "", $sName = "", $sRetPath = ""
  211. Local $iAttribs = 0, _
  212. $sAttribs = ''
  213. Local $asFolderFileSectionList[100][2] = [[0, 0]]
  214. ; Search within listed folders
  215. While $asFolderSearchList[0] > 0
  216. ; Set path to search
  217. $sCurrentPath = $asFolderSearchList[$asFolderSearchList[0]]
  218. ; Reduce folder search list count
  219. $asFolderSearchList[0] -= 1
  220. ; Determine return path to add to file/folder name
  221. Switch $iReturnPath
  222. ; Case 0 ; Name only
  223. ; Leave as ""
  224. Case 1 ;Relative to initial path
  225. $sRetPath = StringReplace($sCurrentPath, $sFilePath, "")
  226. Case 2 ; Full path
  227. If $bLongPath Then
  228. $sRetPath = StringTrimLeft($sCurrentPath, 4)
  229. Else
  230. $sRetPath = $sCurrentPath
  231. EndIf
  232. EndSwitch
  233. ; Get search handle - use code matched to required listing
  234. If $iHide_Link Then
  235. ; Use DLL code
  236. $aDLL_Ret = DllCall($hDLL, 'handle', 'FindFirstFileW', 'wstr', $sCurrentPath & "*", 'struct*', $tFile_Data)
  237. If @error Or Not $aDLL_Ret[0] Then
  238. ContinueLoop
  239. EndIf
  240. $hSearch = $aDLL_Ret[0]
  241. Else
  242. ; Use native code
  243. $hSearch = FileFindFirstFile($sCurrentPath & "*")
  244. ; If folder empty move to next in list
  245. If $hSearch = -1 Then
  246. ContinueLoop
  247. EndIf
  248. EndIf
  249. ; If sorting files and folders with paths then store folder name and position of associated files in list
  250. If $iReturn = 0 And $iSort And $iReturnPath Then
  251. __FLTAR_AddToList($asFolderFileSectionList, $sRetPath, $asFileMatchList[0] + 1)
  252. EndIf
  253. $sAttribs = ''
  254. ; Search folder - use code matched to required listing
  255. While 1
  256. ; Use DLL code
  257. If $iHide_Link Then
  258. ; Use DLL code
  259. $aDLL_Ret = DllCall($hDLL, 'int', 'FindNextFileW', 'handle', $hSearch, 'struct*', $tFile_Data)
  260. ; Check for end of folder
  261. If @error Or Not $aDLL_Ret[0] Then
  262. ExitLoop
  263. EndIf
  264. ; Extract data
  265. $sName = DllStructGetData($tFile_Data, "FileName")
  266. ; Check for .. return - only returned by the DllCall
  267. If $sName = ".." Then
  268. ContinueLoop
  269. EndIf
  270. $iAttribs = DllStructGetData($tFile_Data, "FileAttributes")
  271. ; Check for hidden/system attributes and skip if found
  272. If $iHide_HS And BitAND($iAttribs, $iHide_HS) Then
  273. ContinueLoop
  274. EndIf
  275. ; Check for link attribute and skip if found
  276. If BitAND($iAttribs, $iHide_Link) Then
  277. ContinueLoop
  278. EndIf
  279. ; Set subfolder flag
  280. $bFolder = False
  281. If BitAND($iAttribs, 16) Then
  282. $bFolder = True
  283. EndIf
  284. Else
  285. ; Reset folder flag
  286. $bFolder = False
  287. ; Use native code
  288. $sName = FileFindNextFile($hSearch, 1)
  289. ; Check for end of folder
  290. If @error Then
  291. ExitLoop
  292. EndIf
  293. $sAttribs = @extended
  294. ; Check for folder
  295. If StringInStr($sAttribs, "D") Then
  296. $bFolder = True
  297. EndIf
  298. ; Check for Hidden/System
  299. If StringRegExp($sAttribs, "[" & $sHide_HS & "]") Then
  300. ContinueLoop
  301. EndIf
  302. EndIf
  303. ; If folder then check whether to add to search list
  304. If $bFolder Then
  305. Select
  306. Case $iRecur < 0 ; Check recur depth
  307. StringReplace($sCurrentPath, "\", "", 0, $STR_NOCASESENSEBASIC)
  308. If @extended < $iMaxLevel Then
  309. ContinueCase ; Check if matched to masks
  310. EndIf
  311. Case $iRecur = 1 ; Full recur
  312. If Not StringRegExp($sName, $sExclude_Folder_Mask) Then ; Add folder unless excluded
  313. __FLTAR_AddToList($asFolderSearchList, $sCurrentPath & $sName & "\")
  314. EndIf
  315. ; Case $iRecur = 0 ; Never add
  316. ; Do nothing
  317. EndSelect
  318. EndIf
  319. If $iSort Then ; Save in relevant folders for later sorting
  320. If $bFolder Then
  321. If StringRegExp($sName, $sInclude_Folder_Mask) And Not StringRegExp($sName, $sExclude_Folder_Mask) Then
  322. __FLTAR_AddToList($asFolderMatchList, $sRetPath & $sName & $sFolderSlash)
  323. EndIf
  324. Else
  325. If StringRegExp($sName, $sInclude_File_Mask) And Not StringRegExp($sName, $sExclude_File_Mask) Then
  326. ; Select required list for files
  327. If $sCurrentPath = $sFilePath Then
  328. __FLTAR_AddToList($asRootFileMatchList, $sRetPath & $sName)
  329. Else
  330. __FLTAR_AddToList($asFileMatchList, $sRetPath & $sName)
  331. EndIf
  332. EndIf
  333. EndIf
  334. Else ; Save directly in return list
  335. If $bFolder Then
  336. If $iReturn <> 1 And StringRegExp($sName, $sInclude_Folder_Mask) And Not StringRegExp($sName, $sExclude_Folder_Mask) Then
  337. __FLTAR_AddToList($asReturnList, $sRetPath & $sName & $sFolderSlash)
  338. EndIf
  339. Else
  340. If $iReturn <> 2 And StringRegExp($sName, $sInclude_File_Mask) And Not StringRegExp($sName, $sExclude_File_Mask) Then
  341. __FLTAR_AddToList($asReturnList, $sRetPath & $sName)
  342. EndIf
  343. EndIf
  344. EndIf
  345. WEnd
  346. ; Close current search
  347. If $iHide_Link Then
  348. DllCall($hDLL, 'int', 'FindClose', 'ptr', $hSearch)
  349. Else
  350. FileClose($hSearch)
  351. EndIf
  352. WEnd
  353. ; Close the DLL if needed
  354. If $iHide_Link Then
  355. DllClose($hDLL)
  356. EndIf
  357. ; Sort results if required
  358. If $iSort Then
  359. Switch $iReturn
  360. Case 2 ; Folders only
  361. ; Check if any folders found
  362. If $asFolderMatchList[0] = 0 Then Return SetError(1, 9, "")
  363. ; Correctly size folder match list
  364. ReDim $asFolderMatchList[$asFolderMatchList[0] + 1]
  365. ; Copy size folder match array
  366. $asReturnList = $asFolderMatchList
  367. ; Simple sort list
  368. __ArrayDualPivotSort($asReturnList, 1, $asReturnList[0])
  369. Case 1 ; Files only
  370. ; Check if any files found
  371. If $asRootFileMatchList[0] = 0 And $asFileMatchList[0] = 0 Then Return SetError(1, 9, "")
  372. If $iReturnPath = 0 Then ; names only so simple sort suffices
  373. ; Combine file match lists
  374. __FLTAR_AddFileLists($asReturnList, $asRootFileMatchList, $asFileMatchList)
  375. ; Simple sort combined file list
  376. __ArrayDualPivotSort($asReturnList, 1, $asReturnList[0])
  377. Else
  378. ; Combine sorted file match lists
  379. __FLTAR_AddFileLists($asReturnList, $asRootFileMatchList, $asFileMatchList, 1)
  380. EndIf
  381. Case 0 ; Both files and folders
  382. ; Check if any root files or folders found
  383. If $asRootFileMatchList[0] = 0 And $asFolderMatchList[0] = 0 Then Return SetError(1, 9, "")
  384. If $iReturnPath = 0 Then ; names only so simple sort suffices
  385. ; Combine file match lists
  386. __FLTAR_AddFileLists($asReturnList, $asRootFileMatchList, $asFileMatchList)
  387. ; Set correct count for folder add
  388. $asReturnList[0] += $asFolderMatchList[0]
  389. ; Resize and add file match array
  390. ReDim $asFolderMatchList[$asFolderMatchList[0] + 1]
  391. _ArrayConcatenate($asReturnList, $asFolderMatchList, 1)
  392. ; Simple sort final list
  393. __ArrayDualPivotSort($asReturnList, 1, $asReturnList[0])
  394. Else
  395. ; Size return list
  396. Local $asReturnList[$asFileMatchList[0] + $asRootFileMatchList[0] + $asFolderMatchList[0] + 1]
  397. $asReturnList[0] = $asFileMatchList[0] + $asRootFileMatchList[0] + $asFolderMatchList[0]
  398. ; Sort root file list
  399. __ArrayDualPivotSort($asRootFileMatchList, 1, $asRootFileMatchList[0])
  400. ; Add the sorted root files at the top
  401. For $i = 1 To $asRootFileMatchList[0]
  402. $asReturnList[$i] = $asRootFileMatchList[$i]
  403. Next
  404. ; Set next insertion index
  405. Local $iNextInsertionIndex = $asRootFileMatchList[0] + 1
  406. ; Sort folder list
  407. __ArrayDualPivotSort($asFolderMatchList, 1, $asFolderMatchList[0])
  408. Local $sFolderToFind = ""
  409. ; Work through folder list
  410. For $i = 1 To $asFolderMatchList[0]
  411. ; Add folder to return list
  412. $asReturnList[$iNextInsertionIndex] = $asFolderMatchList[$i]
  413. $iNextInsertionIndex += 1
  414. ; Format folder name for search
  415. If $sFolderSlash Then
  416. $sFolderToFind = $asFolderMatchList[$i]
  417. Else
  418. $sFolderToFind = $asFolderMatchList[$i] & "\"
  419. EndIf
  420. Local $iFileSectionEndIndex = 0, $iFileSectionStartIndex = 0
  421. ; Find folder in FolderFileSectionList
  422. For $j = 1 To $asFolderFileSectionList[0][0]
  423. ; If found then deal with files
  424. If $sFolderToFind = $asFolderFileSectionList[$j][0] Then
  425. ; Set file list indexes
  426. $iFileSectionStartIndex = $asFolderFileSectionList[$j][1]
  427. If $j = $asFolderFileSectionList[0][0] Then
  428. $iFileSectionEndIndex = $asFileMatchList[0]
  429. Else
  430. $iFileSectionEndIndex = $asFolderFileSectionList[$j + 1][1] - 1
  431. EndIf
  432. ; Sort files if required
  433. If $iSort = 1 Then
  434. __ArrayDualPivotSort($asFileMatchList, $iFileSectionStartIndex, $iFileSectionEndIndex)
  435. EndIf
  436. ; Add files to return list
  437. For $k = $iFileSectionStartIndex To $iFileSectionEndIndex
  438. $asReturnList[$iNextInsertionIndex] = $asFileMatchList[$k]
  439. $iNextInsertionIndex += 1
  440. Next
  441. ExitLoop
  442. EndIf
  443. Next
  444. Next
  445. EndIf
  446. EndSwitch
  447. Else ; No sort
  448. ; Check if any file/folders have been added
  449. If $asReturnList[0] = 0 Then Return SetError(1, 9, "")
  450. ; Remove any unused return list elements from last ReDim
  451. ReDim $asReturnList[$asReturnList[0] + 1]
  452. EndIf
  453. Return $asReturnList
  454. EndFunc ;==>_FileListToArrayRec
  455. ; #INTERNAL_USE_ONLY#============================================================================================================
  456. ; Name...........: __FLTAR_AddFileLists
  457. ; Description ...: Add internal lists after resizing and optional sorting
  458. ; Syntax ........: __FLTAR_AddFileLists(ByRef $asTarget, $asSource_1, $asSource_2[, $iSort = 0])
  459. ; Parameters ....: $asReturnList - Base list
  460. ; $asRootFileMatchList - First list to add
  461. ; $asFileMatchList - Second list to add
  462. ; $iSort - (Optional) Whether to sort lists before adding
  463. ; |$iSort = 0 (Default) No sort
  464. ; |$iSort = 1 Sort in descending alphabetical order
  465. ; Return values .: None - array modified ByRef
  466. ; Author ........: Melba23
  467. ; Remarks .......: This function is used internally by _FileListToArrayRec
  468. ; ===============================================================================================================================
  469. Func __FLTAR_AddFileLists(ByRef $asTarget, $asSource_1, $asSource_2, $iSort = 0)
  470. ; Correctly size root file match array
  471. ReDim $asSource_1[$asSource_1[0] + 1]
  472. ; Simple sort root file match array if required
  473. If $iSort = 1 Then __ArrayDualPivotSort($asSource_1, 1, $asSource_1[0])
  474. ; Copy root file match array
  475. $asTarget = $asSource_1
  476. ; Add file match count
  477. $asTarget[0] += $asSource_2[0]
  478. ; Correctly size file match array
  479. ReDim $asSource_2[$asSource_2[0] + 1]
  480. ; Simple sort file match array if required
  481. If $iSort = 1 Then __ArrayDualPivotSort($asSource_2, 1, $asSource_2[0])
  482. ; Add file match array
  483. _ArrayConcatenate($asTarget, $asSource_2, 1)
  484. EndFunc ;==>__FLTAR_AddFileLists
  485. ; #INTERNAL_USE_ONLY#============================================================================================================
  486. ; Name...........: __FLTAR_AddToList
  487. ; Description ...: Add element to [?] or [?][2] list which is resized if necessary
  488. ; Syntax ........: __FLTAR_AddToList(ByRef $asList, $vValue_0, [$vValue_1])
  489. ; Parameters ....: $aList - List to be added to
  490. ; $vValue_0 - Value to add to array - if $vValue_1 exists value added to [?][0] element in [?][2] array
  491. ; $vValue_1 - Value to add to [?][1] element in [?][2] array (optional)
  492. ; Return values .: None - array modified ByRef
  493. ; Author ........: Melba23
  494. ; Remarks .......: This function is used internally by _FileListToArrayRec
  495. ; ===============================================================================================================================
  496. Func __FLTAR_AddToList(ByRef $aList, $vValue_0, $vValue_1 = -1)
  497. If $vValue_1 = -1 Then ; [?] array
  498. ; Increase list count
  499. $aList[0] += 1
  500. ; Double list size if too small (fewer ReDim needed)
  501. If UBound($aList) <= $aList[0] Then ReDim $aList[UBound($aList) * 2]
  502. ; Add value
  503. $aList[$aList[0]] = $vValue_0
  504. Else ; [?][2] array
  505. $aList[0][0] += 1
  506. If UBound($aList) <= $aList[0][0] Then ReDim $aList[UBound($aList) * 2][2]
  507. $aList[$aList[0][0]][0] = $vValue_0
  508. $aList[$aList[0][0]][1] = $vValue_1
  509. EndIf
  510. EndFunc ;==>__FLTAR_AddToList
  511. ; #INTERNAL_USE_ONLY#============================================================================================================
  512. ; Name...........: __FLTAR_ListToMask
  513. ; Description ...: Convert include/exclude lists to SRE format
  514. ; Syntax ........: __FLTAR_ListToMask(ByRef $sMask, $sList)
  515. ; Parameters ....: $asMask - Include/Exclude mask to create
  516. ; $asList - Include/Exclude list to convert
  517. ; Return values .: Success: 1
  518. ; Failure: 0
  519. ; Author ........: SRE patterns developed from those posted by various forum members and Spiff59 in particular
  520. ; Remarks .......: This function is used internally by _FileListToArrayRec
  521. ; ===============================================================================================================================
  522. Func __FLTAR_ListToMask(ByRef $sMask, $sList)
  523. ; Check for invalid characters within list
  524. If StringRegExp($sList, "\\|/|:|\<|\>|\|") Then Return 0
  525. ; Strip WS and insert | for ;
  526. $sList = StringReplace(StringStripWS(StringRegExpReplace($sList, "\s*;\s*", ";"), BitOR($STR_STRIPLEADING, $STR_STRIPTRAILING)), ";", "|")
  527. ; Convert to SRE pattern
  528. $sList = StringReplace(StringReplace(StringRegExpReplace($sList, "[][$^.{}()+\-]", "\\$0"), "?", "."), "*", ".*?")
  529. ; Add prefix and suffix
  530. $sMask = "(?i)^(" & $sList & ")\z"
  531. Return 1
  532. EndFunc ;==>__FLTAR_ListToMask
  533. ; #FUNCTION# ====================================================================================================================
  534. ; Author ........: erifash <erifash [at] gmail [dot] com>
  535. ; Modified.......: guinness - Use the native ShellExecute function.
  536. ; ===============================================================================================================================
  537. Func _FilePrint($sFilePath, $iShow = @SW_HIDE)
  538. Return ShellExecute($sFilePath, "", @WorkingDir, "print", $iShow = Default ? @SW_HIDE : $iShow)
  539. EndFunc ;==>_FilePrint
  540. ; #FUNCTION# ====================================================================================================================
  541. ; Author ........: Jonathan Bennett <jon at autoitscript dot com>, Valik - Support Windows Unix and Mac line separator
  542. ; Modified ......: Jpm - fixed empty line at the end, Gary Fixed file contains only 1 line, guinness - Optional flag to return the array count.
  543. ; : Melba23 - Read to 1D/2D arrays, guinness & jchd - Removed looping through 1D array with $FRTA_COUNT flag.
  544. ; ===============================================================================================================================
  545. Func _FileReadToArray($sFilePath, ByRef $vReturn, $iFlags = $FRTA_COUNT, $sDelimiter = "")
  546. ; Clear the previous contents
  547. $vReturn = 0
  548. If $iFlags = Default Then $iFlags = $FRTA_COUNT
  549. If $sDelimiter = Default Then $sDelimiter = ""
  550. ; Set "array of arrays" flag
  551. Local $bExpand = True
  552. If BitAND($iFlags, $FRTA_INTARRAYS) Then
  553. $bExpand = False
  554. $iFlags -= $FRTA_INTARRAYS
  555. EndIf
  556. ; Set delimiter flag
  557. Local $iEntire = $STR_CHRSPLIT
  558. If BitAND($iFlags, $FRTA_ENTIRESPLIT) Then
  559. $iEntire = $STR_ENTIRESPLIT
  560. $iFlags -= $FRTA_ENTIRESPLIT
  561. EndIf
  562. ; Set row count and split count flags
  563. Local $iNoCount = 0
  564. If $iFlags <> $FRTA_COUNT Then
  565. $iFlags = $FRTA_NOCOUNT
  566. $iNoCount = $STR_NOCOUNT
  567. EndIf
  568. ; Check delimiter
  569. If $sDelimiter Then
  570. ; Read file into an array
  571. Local $aLines = FileReadToArray($sFilePath)
  572. If @error Then Return SetError(@error, 0, 0)
  573. ; Get first dimension and add count if required
  574. Local $iDim_1 = UBound($aLines) + $iFlags
  575. ; Check type of return array
  576. If $bExpand Then ; All lines have same number of fields
  577. ; Count fields in first line
  578. Local $iDim_2 = UBound(StringSplit($aLines[0], $sDelimiter, $iEntire + $STR_NOCOUNT))
  579. ; Size array
  580. Local $aTemp_Array[$iDim_1][$iDim_2]
  581. ; Declare the variables
  582. Local $iFields, _
  583. $aSplit
  584. ; Loop through the lines
  585. For $i = 0 To $iDim_1 - $iFlags - 1
  586. ; Split each line as required
  587. $aSplit = StringSplit($aLines[$i], $sDelimiter, $iEntire + $STR_NOCOUNT)
  588. ; Count the items
  589. $iFields = UBound($aSplit)
  590. If $iFields <> $iDim_2 Then
  591. ; Return error
  592. Return SetError(3, 0, 0)
  593. EndIf
  594. ; Fill this line of the array
  595. For $j = 0 To $iFields - 1
  596. $aTemp_Array[$i + $iFlags][$j] = $aSplit[$j]
  597. Next
  598. Next
  599. ; Check at least 2 columns
  600. If $iDim_2 < 2 Then Return SetError(4, 0, 0)
  601. ; Set dimension count
  602. If $iFlags Then
  603. $aTemp_Array[0][0] = $iDim_1 - $iFlags
  604. $aTemp_Array[0][1] = $iDim_2
  605. EndIf
  606. Else ; Create "array of arrays"
  607. ; Size array
  608. Local $aTemp_Array[$iDim_1]
  609. ; Loop through the lines
  610. For $i = 0 To $iDim_1 - $iFlags - 1
  611. ; Split each line as required
  612. $aTemp_Array[$i + $iFlags] = StringSplit($aLines[$i], $sDelimiter, $iEntire + $iNoCount)
  613. Next
  614. ; Set dimension count
  615. If $iFlags Then
  616. $aTemp_Array[0] = $iDim_1 - $iFlags
  617. EndIf
  618. EndIf
  619. ; Return the array
  620. $vReturn = $aTemp_Array
  621. Else ; 1D
  622. If $iFlags Then
  623. Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
  624. If $hFileOpen = -1 Then Return SetError(1, 0, 0)
  625. Local $sFileRead = FileRead($hFileOpen)
  626. FileClose($hFileOpen)
  627. If StringLen($sFileRead) Then
  628. $vReturn = StringRegExp(@LF & $sFileRead, "(?|(\N+)\z|(\N*)(?:\R))", $STR_REGEXPARRAYGLOBALMATCH)
  629. $vReturn[0] = UBound($vReturn) - 1
  630. Else
  631. Return SetError(2, 0, 0)
  632. EndIf
  633. Else
  634. $vReturn = FileReadToArray($sFilePath)
  635. If @error Then
  636. $vReturn = 0
  637. Return SetError(@error, 0, 0)
  638. EndIf
  639. EndIf
  640. EndIf
  641. Return 1
  642. EndFunc ;==>_FileReadToArray
  643. ; #FUNCTION# ====================================================================================================================
  644. ; Author ........: Jos van der Zande <jdeb at autoitscript dot com>
  645. ; Modified.......: Updated for file handles by PsaltyDS, @error = 4 msg and 2-dimension capability added by Spiff59 and fixed by guinness.
  646. ; ===============================================================================================================================
  647. Func _FileWriteFromArray($sFilePath, Const ByRef $aArray, $iBase = Default, $iUBound = Default, $sDelimiter = "|")
  648. Local $iReturn = 0
  649. ; Check if we have a valid array as an input.
  650. If Not IsArray($aArray) Then Return SetError(2, 0, $iReturn)
  651. ; Check the number of dimensions is no greater than a 2d array.
  652. Local $iDims = UBound($aArray, $UBOUND_DIMENSIONS)
  653. If $iDims > 2 Then Return SetError(4, 0, 0)
  654. ; Determine last entry of the array.
  655. Local $iLast = UBound($aArray) - 1
  656. If $iUBound = Default Or $iUBound > $iLast Then $iUBound = $iLast
  657. If $iBase < 0 Or $iBase = Default Then $iBase = 0
  658. If $iBase > $iUBound Then Return SetError(5, 0, $iReturn)
  659. If $sDelimiter = Default Then $sDelimiter = "|"
  660. ; Open output file for overwrite by default, or use input file handle if passed.
  661. Local $hFileOpen = $sFilePath
  662. If IsString($sFilePath) Then
  663. $hFileOpen = FileOpen($sFilePath, $FO_OVERWRITE)
  664. If $hFileOpen = -1 Then Return SetError(1, 0, $iReturn)
  665. EndIf
  666. ; Write array data to file.
  667. Local $iError = 0
  668. $iReturn = 1 ; Set the return value to true.
  669. Switch $iDims
  670. Case 1
  671. For $i = $iBase To $iUBound
  672. If Not FileWrite($hFileOpen, $aArray[$i] & @CRLF) Then
  673. $iError = 3
  674. $iReturn = 0
  675. ExitLoop
  676. EndIf
  677. Next
  678. Case 2
  679. Local $sTemp = ""
  680. For $i = $iBase To $iUBound
  681. $sTemp = $aArray[$i][0]
  682. For $j = 1 To UBound($aArray, $UBOUND_COLUMNS) - 1
  683. $sTemp &= $sDelimiter & $aArray[$i][$j]
  684. Next
  685. If Not FileWrite($hFileOpen, $sTemp & @CRLF) Then
  686. $iError = 3
  687. $iReturn = 0
  688. ExitLoop
  689. EndIf
  690. Next
  691. EndSwitch
  692. ; Close file only if specified by a string path.
  693. If IsString($sFilePath) Then FileClose($hFileOpen)
  694. ; Return the results.
  695. Return SetError($iError, 0, $iReturn)
  696. EndFunc ;==>_FileWriteFromArray
  697. ; #FUNCTION# ====================================================================================================================
  698. ; Author ........: Jeremy Landes <jlandes at landeserve dot com>
  699. ; Modified.......: MrCreatoR - added $iFlag parameter
  700. ; ===============================================================================================================================
  701. Func _FileWriteLog($sLogPath, $sLogMsg, $iFlag = -1)
  702. Local $iOpenMode = $FO_APPEND
  703. Local $sMsg = @YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " : " & $sLogMsg
  704. If $iFlag = Default Then $iFlag = -1
  705. If $iFlag <> -1 Then
  706. $iOpenMode = $FO_OVERWRITE
  707. $sMsg &= @CRLF & FileRead($sLogPath)
  708. EndIf
  709. ; Open output file for appending to the end/overwriting, or use input file handle if passed
  710. Local $hFileOpen = $sLogPath
  711. If IsString($sLogPath) Then $hFileOpen = FileOpen($sLogPath, $iOpenMode)
  712. If $hFileOpen = -1 Then Return SetError(1, 0, 0)
  713. Local $iReturn = FileWriteLine($hFileOpen, $sMsg)
  714. ; Close file only if specified by a string path
  715. If IsString($sLogPath) Then $iReturn = FileClose($hFileOpen)
  716. If $iReturn <= 0 Then Return SetError(2, $iReturn, 0)
  717. Return $iReturn
  718. EndFunc ;==>_FileWriteLog
  719. ; #FUNCTION# ====================================================================================================================
  720. ; Author ........: cdkid
  721. ; Modified.......: partypooper, MrCreatoR, Melba23
  722. ; ===============================================================================================================================
  723. Func _FileWriteToLine($sFilePath, $iLine, $sText, $bOverWrite = False, $bFill = False)
  724. If $bOverWrite = Default Then $bOverWrite = False
  725. If $bFill = Default Then $bFill = False
  726. If Not FileExists($sFilePath) Then Return SetError(2, 0, 0)
  727. If $iLine <= 0 Then Return SetError(4, 0, 0)
  728. If Not (IsBool($bOverWrite) Or $bOverWrite = 0 Or $bOverWrite = 1) Then Return SetError(5, 0, 0)
  729. If Not IsString($sText) Then
  730. $sText = String($sText)
  731. If $sText = "" Then Return SetError(6, 0, 0)
  732. EndIf
  733. If Not IsBool($bFill) Then Return SetError(7, 0, 0)
  734. ; Read current file into array
  735. Local $aArray = FileReadToArray($sFilePath)
  736. ; Create empty array if empty file
  737. If @error Then Local $aArray[0]
  738. Local $iUBound = UBound($aArray) - 1
  739. ; If Fill option set
  740. If $bFill Then
  741. ; If required resize array to allow line to be written
  742. If $iUBound < $iLine Then
  743. ReDim $aArray[$iLine]
  744. $iUBound = $iLine - 1
  745. EndIf
  746. Else
  747. If ($iUBound + 1) < $iLine Then Return SetError(1, 0, 0)
  748. EndIf
  749. ; Write specific line - array is 0-based so reduce by 1 - and either replace or insert
  750. $aArray[$iLine - 1] = ($bOverWrite ? $sText : $sText & @CRLF & $aArray[$iLine - 1])
  751. ; Concatenate array elements
  752. Local $sData = ""
  753. For $i = 0 To $iUBound
  754. $sData &= $aArray[$i] & @CRLF
  755. Next
  756. $sData = StringTrimRight($sData, StringLen(@CRLF)) ; Required to strip trailing EOL
  757. ; Write data to file
  758. Local $hFileOpen = FileOpen($sFilePath, FileGetEncoding($sFilePath) + $FO_OVERWRITE)
  759. If $hFileOpen = -1 Then Return SetError(3, 0, 0)
  760. FileWrite($hFileOpen, $sData)
  761. FileClose($hFileOpen)
  762. Return 1
  763. EndFunc ;==>_FileWriteToLine
  764. ; #FUNCTION# ====================================================================================================================
  765. ; Author ........: Valik (Original function and modification to rewrite), tittoproject (Rewrite)
  766. ; Modified.......:
  767. ; ===============================================================================================================================
  768. Func _PathFull($sRelativePath, $sBasePath = @WorkingDir)
  769. If Not $sRelativePath Or $sRelativePath = "." Then Return $sBasePath
  770. ; Normalize slash direction.
  771. Local $sFullPath = StringReplace($sRelativePath, "/", "\") ; Holds the full path (later, minus the root)
  772. Local Const $sFullPathConst = $sFullPath ; Holds a constant version of the full path.
  773. Local $sPath ; Holds the root drive/server
  774. Local $bRootOnly = StringLeft($sFullPath, 1) = "\" And StringMid($sFullPath, 2, 1) <> "\"
  775. If $sBasePath = Default Then $sBasePath = @WorkingDir
  776. ; Check for UNC paths or local drives. We run this twice at most. The
  777. ; first time, we check if the relative path is absolute. If it's not, then
  778. ; we use the base path which should be absolute.
  779. For $i = 1 To 2
  780. $sPath = StringLeft($sFullPath, 2)
  781. If $sPath = "\\" Then
  782. $sFullPath = StringTrimLeft($sFullPath, 2)
  783. Local $nServerLen = StringInStr($sFullPath, "\") - 1
  784. $sPath = "\\" & StringLeft($sFullPath, $nServerLen)
  785. $sFullPath = StringTrimLeft($sFullPath, $nServerLen)
  786. ExitLoop
  787. ElseIf StringRight($sPath, 1) = ":" Then
  788. $sFullPath = StringTrimLeft($sFullPath, 2)
  789. ExitLoop
  790. Else
  791. $sFullPath = $sBasePath & "\" & $sFullPath
  792. EndIf
  793. Next
  794. ; If this happens, we've found a funky path and don't know what to do
  795. ; except for get out as fast as possible. We've also screwed up our
  796. ; variables so we definitely need to quit.
  797. ; If $i = 3 Then Return ""
  798. ; A path with a drive but no slash (e.g. C:Path\To\File) has the following
  799. ; behavior. If the relative drive is the same as the $BasePath drive then
  800. ; insert the base path. If the drives differ then just insert a leading
  801. ; slash to make the path valid.
  802. If StringLeft($sFullPath, 1) <> "\" Then
  803. If StringLeft($sFullPathConst, 2) = StringLeft($sBasePath, 2) Then
  804. $sFullPath = $sBasePath & "\" & $sFullPath
  805. Else
  806. $sFullPath = "\" & $sFullPath
  807. EndIf
  808. EndIf
  809. ; Build an array of the path parts we want to use.
  810. Local $aTemp = StringSplit($sFullPath, "\")
  811. Local $aPathParts[$aTemp[0]], $j = 0
  812. For $i = 2 To $aTemp[0]
  813. If $aTemp[$i] = ".." Then
  814. If $j Then $j -= 1
  815. ElseIf Not ($aTemp[$i] = "" And $i <> $aTemp[0]) And $aTemp[$i] <> "." Then
  816. $aPathParts[$j] = $aTemp[$i]
  817. $j += 1
  818. EndIf
  819. Next
  820. ; Here we re-build the path from the parts above. We skip the
  821. ; loop if we are only returning the root.
  822. $sFullPath = $sPath
  823. If Not $bRootOnly Then
  824. For $i = 0 To $j - 1
  825. $sFullPath &= "\" & $aPathParts[$i]
  826. Next
  827. Else
  828. $sFullPath &= $sFullPathConst
  829. ; If we detect more relative parts, remove them by calling ourself recursively.
  830. If StringInStr($sFullPath, "..") Then $sFullPath = _PathFull($sFullPath)
  831. EndIf
  832. ; Clean up the path.
  833. Do
  834. $sFullPath = StringReplace($sFullPath, ".\", "\")
  835. Until @extended = 0
  836. Return $sFullPath
  837. EndFunc ;==>_PathFull
  838. ; #FUNCTION# ====================================================================================================================
  839. ; Author ........: Erik Pilsits
  840. ; Modified.......:
  841. ; ===============================================================================================================================
  842. Func _PathGetRelative($sFrom, $sTo)
  843. If StringRight($sFrom, 1) <> "\" Then $sFrom &= "\" ; add missing trailing \ to $sFrom path
  844. If StringRight($sTo, 1) <> "\" Then $sTo &= "\" ; add trailing \ to $sTo
  845. If $sFrom = $sTo Then Return SetError(1, 0, StringTrimRight($sTo, 1)) ; $sFrom equals $sTo
  846. Local $asFrom = StringSplit($sFrom, "\")
  847. Local $asTo = StringSplit($sTo, "\")
  848. If $asFrom[1] <> $asTo[1] Then Return SetError(2, 0, StringTrimRight($sTo, 1)) ; drives are different, rel path not possible
  849. ; create rel path
  850. Local $i = 2
  851. Local $iDiff = 1
  852. While 1
  853. If $asFrom[$i] <> $asTo[$i] Then
  854. $iDiff = $i
  855. ExitLoop
  856. EndIf
  857. $i += 1
  858. WEnd
  859. $i = 1
  860. Local $sRelPath = ""
  861. For $j = 1 To $asTo[0]
  862. If $i >= $iDiff Then
  863. $sRelPath &= "\" & $asTo[$i]
  864. EndIf
  865. $i += 1
  866. Next
  867. $sRelPath = StringTrimLeft($sRelPath, 1)
  868. $i = 1
  869. For $j = 1 To $asFrom[0]
  870. If $i > $iDiff Then
  871. $sRelPath = "..\" & $sRelPath
  872. EndIf
  873. $i += 1
  874. Next
  875. If StringRight($sRelPath, 1) == "\" Then $sRelPath = StringTrimRight($sRelPath, 1) ; remove trailing \
  876. Return $sRelPath
  877. EndFunc ;==>_PathGetRelative
  878. ; #FUNCTION# ====================================================================================================================
  879. ; Author ........: Valik
  880. ; Modified.......: guinness
  881. ; ===============================================================================================================================
  882. Func _PathMake($sDrive, $sDir, $sFileName, $sExtension)
  883. ; Format $sDrive, if it's not a UNC server name, then just get the drive letter and add a colon
  884. If StringLen($sDrive) Then
  885. If Not (StringLeft($sDrive, 2) = "\\") Then $sDrive = StringLeft($sDrive, 1) & ":"
  886. EndIf
  887. ; Format the directory by adding any necessary slashes
  888. If StringLen($sDir) Then
  889. If Not (StringRight($sDir, 1) = "\") And Not (StringRight($sDir, 1) = "/") Then $sDir = $sDir & "\"
  890. Else
  891. $sDir = "\"
  892. EndIf
  893. If StringLen($sDir) Then
  894. ; Append a backslash to the start of the directory if required
  895. If Not (StringLeft($sDir, 1) = "\") And Not (StringLeft($sDir, 1) = "/") Then $sDir = "\" & $sDir
  896. EndIf
  897. ; Nothing to be done for the filename
  898. ; Add the period to the extension if necessary
  899. If StringLen($sExtension) Then
  900. If Not (StringLeft($sExtension, 1) = ".") Then $sExtension = "." & $sExtension
  901. EndIf
  902. Return $sDrive & $sDir & $sFileName & $sExtension
  903. EndFunc ;==>_PathMake
  904. ; #FUNCTION# ====================================================================================================================
  905. ; Author ........: Valik
  906. ; Modified.......: DXRW4E - Re-wrote to use a regular expression; guinness - Update syntax and structure.
  907. ; ===============================================================================================================================
  908. Func _PathSplit($sFilePath, ByRef $sDrive, ByRef $sDir, ByRef $sFileName, ByRef $sExtension)
  909. Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", $STR_REGEXPARRAYMATCH)
  910. If @error Then ; This error should never happen.
  911. ReDim $aArray[5]
  912. $aArray[$PATH_ORIGINAL] = $sFilePath
  913. EndIf
  914. $sDrive = $aArray[$PATH_DRIVE]
  915. If StringLeft($aArray[$PATH_DIRECTORY], 1) == "/" Then
  916. $sDir = StringRegExpReplace($aArray[$PATH_DIRECTORY], "\h*[\/\\]+\h*", "\/")
  917. Else
  918. $sDir = StringRegExpReplace($aArray[$PATH_DIRECTORY], "\h*[\/\\]+\h*", "\\")
  919. EndIf
  920. $aArray[$PATH_DIRECTORY] = $sDir
  921. $sFileName = $aArray[$PATH_FILENAME]
  922. $sExtension = $aArray[$PATH_EXTENSION]
  923. Return $aArray
  924. EndFunc ;==>_PathSplit
  925. ; #FUNCTION# ====================================================================================================================
  926. ; Author ........: Kurt (aka /dev/null) and JdeB
  927. ; Modified ......: guinness - Re-wrote the function entirely for improvements in readability.
  928. ; ===============================================================================================================================
  929. Func _ReplaceStringInFile($sFilePath, $sSearchString, $sReplaceString, $iCaseSensitive = 0, $iOccurance = 1)
  930. If StringInStr(FileGetAttrib($sFilePath), "R") Then Return SetError(1, 0, -1)
  931. ; Open the file for reading.
  932. Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
  933. If $hFileOpen = -1 Then Return SetError(2, 0, -1)
  934. ; Read the contents of the file and stores in a variable
  935. Local $sFileRead = FileRead($hFileOpen)
  936. FileClose($hFileOpen) ; Close the open file after reading
  937. ; Set the default parameters
  938. If $iCaseSensitive = Default Then $iCaseSensitive = 0
  939. If $iOccurance = Default Then $iOccurance = 1
  940. ; Replace strings
  941. $sFileRead = StringReplace($sFileRead, $sSearchString, $sReplaceString, 1 - $iOccurance, $iCaseSensitive)
  942. Local $iReturn = @extended
  943. ; If there are replacements then overwrite the file
  944. If $iReturn Then
  945. ; Retrieve the file encoding
  946. Local $iFileEncoding = FileGetEncoding($sFilePath)
  947. ; Open the file for writing and set the overwrite flag
  948. $hFileOpen = FileOpen($sFilePath, $iFileEncoding + $FO_OVERWRITE)
  949. If $hFileOpen = -1 Then Return SetError(3, 0, -1)
  950. ; Write to the open file
  951. FileWrite($hFileOpen, $sFileRead)
  952. FileClose($hFileOpen) ; Close the open file after writing
  953. EndIf
  954. Return $iReturn
  955. EndFunc ;==>_ReplaceStringInFile
  956. ; #FUNCTION# ====================================================================================================================
  957. ; Author ........: Dale (Klaatu) Thompson
  958. ; Modified.......: Hans Harder - Added Optional parameters, guinness - Fixed using non-supported characters in the file prefix.
  959. ; ===============================================================================================================================
  960. Func _TempFile($sDirectoryName = @TempDir, $sFilePrefix = "~", $sFileExtension = ".tmp", $iRandomLength = 7)
  961. ; Check parameters for the Default keyword or they meet a certain criteria
  962. If $iRandomLength = Default Or $iRandomLength <= 0 Then $iRandomLength = 7
  963. If $sDirectoryName = Default Or (Not FileExists($sDirectoryName)) Then $sDirectoryName = @TempDir
  964. If $sFileExtension = Default Then $sFileExtension = ".tmp"
  965. If $sFilePrefix = Default Then $sFilePrefix = "~"
  966. ; Check if the directory exists or use the current script directory
  967. If Not FileExists($sDirectoryName) Then $sDirectoryName = @ScriptDir
  968. ; Remove the appending backslash
  969. $sDirectoryName = StringRegExpReplace($sDirectoryName, "[\\/]+$", "")
  970. ; Remove the initial dot (.) from the file extension
  971. $sFileExtension = StringRegExpReplace($sFileExtension, "^\.+", "")
  972. ; Remove any non-supported characters in the file prefix
  973. $sFilePrefix = StringRegExpReplace($sFilePrefix, '[\\/:*?"<>|]', "")
  974. ; Create the temporary file path without writing to the selected directory
  975. Local $sTempName = ""
  976. Do
  977. ; Create a random filename
  978. $sTempName = ""
  979. While StringLen($sTempName) < $iRandomLength
  980. $sTempName &= Chr(Random(97, 122, 1))
  981. WEnd
  982. ; Temporary filepath
  983. $sTempName = $sDirectoryName & "\" & $sFilePrefix & $sTempName & "." & $sFileExtension
  984. Until Not FileExists($sTempName) ; Exit the loop if no file with the same name is present
  985. Return $sTempName
  986. EndFunc ;==>_TempFile