numdig Posted July 22, 2011 Share Posted July 22, 2011 (edited) Could also be named something like_FileExGetShortName()or something like thatI've been using this for ages and it makes my life so much easier.So sharing it now.DESCRIPTION:1/ FileGetShortNameV2 is squizzing the path but leaves the filename intact.=> eg. 'C:\DOCUME~1\PSZCZE~1\LOCALS~1\Temp\AUTOIT~1\FILEGE~1\CRASHT~1\the_filaneme_is_the_original_one.txt'So, for instance, if you writeConst $SOME_LONG_PATH_DIR = @DesktopDir $filePath = $SOME_LONG_PATH_DIR & "\the_filaneme_is_the_original_one.txt" FileWrite($filePath, "blabla") $fileShortName = FileGetShortName($filePath) ;; (...) ;; ;; somewhere further in the code ;; FileCopy($fileShortName, $fileShortName & ".back")Your backup file will be saved as THE_FI~1.TXT.back instead of the_filaneme_is_the_original_one.txt.back...Another example, would be UnZip(FileGetShortName($zipFile)) ... the unzipped folder will not look anything like the original one.All this will not happen if you use FileGetShortNameV2 because the filenames are preserved (only the rest of the path is shrinked down).2/ It doesn't crash when the path is too long.(cf. and http://www.autoitscript.com/trac/autoit/ticket/1982)3/ The cherry on the cake is that this function can help to write an entire UDFand solve the issue http://www.autoitscript.com/trac/autoit/ticket/1795FileGetShortNameV2.au3FileGetShortNameV2_Demo.au3 Edited July 27, 2011 by Chess Link to comment Share on other sites More sharing options...
numdig Posted July 27, 2011 Author Share Posted July 27, 2011 (edited) In the same way, DirRemove could be replaced by DirRemoveV2 it would only take a couple loops using _FileGetListToArray going down the hierarchy and calling FileGetShortName all the way to never get screwed by a long path and finally deleting from the leaves, up to the root directory it s fairly simple but i can't be bothered for the moment XP -- edit Tuesday, 4 December 2012, 7:11 PM, +0100 UTC expandcollapse popup;------------------------------------------------------------------------------- ; Name .........: DirRemoveV3 ; Description ..: Recursively, use short names ; Return .......: 1 if succeeded ;------------------------------------------------------------------------------- Func DirRemoveV3($pathToDelete, $isShortName = 0) $res = 1 $shortPathToDelete = $pathToDelete If Not $isShortName Then $shortPathToDelete = FileGetShortNameV2($pathToDelete) ; Is it really a directory? If Not StringInStr(FileGetAttrib($shortPathToDelete), "D") Then EasyTraceWarningLine("'" & $shortPathToDelete & "' IS NOT A DIRECTORY. SKIP.") Return 0 EndIf EasyTraceWriteLine("LISTING '" & $shortPathToDelete & "'...") EasyTraceIndent() $files = _FileListToArray($shortPathToDelete) If Not @error Then For $i = 1 To $files[0] $fileName = $files[$i] $filePath = $shortPathToDelete & "" & $fileName $fileShortPath = FileGetShortNameV2($shortPathToDelete & "" & $fileName) If StringInStr(FileGetAttrib($fileShortPath), "D") Then $res = DirRemoveV3($fileShortPath, 1) ; Only recursion If Not $res Then ExitLoop Else $res = FileDeleteV3($fileShortPath, 1) ; The actual delete If Not $res Then ExitLoop EndIf Next EndIf EasyTraceUnindent() If $res Then ; Wait for directory to be really empty _FileListToArray($shortPathToDelete) $totalWait = 0 While Not @error Sleep(10) $totalWait += 10 If $totalWait > 2000 Then ExitLoop _FileListToArray($shortPathToDelete) WEnd ; Try to delete the directoy (that should be empty by now) EasyTraceWriteLine("DEL DIR '" & $shortPathToDelete & "'...") If Not IsSet("DRY_RUN") Then $res = DirRemove($shortPathToDelete) ; The actual delete If Not $res Then EasyTraceErrorLine("DEL DIR '" & $fileShortPath & "' FAILED.") EndIf Return $res EndFunc + Using a couple helpers such as: Func EasyTraceWrite($text) If IsSet("TRACE_WRITE") Then ConsoleWrite(Eval("TRACE_WRITE_INDENT") & $text) EndFunc Func EasyTraceWriteLine($text) Return EasyTraceWrite($text & @CRLF) EndFunc Func EasyTraceIndent() Set_("TRACE_WRITE_INDENT", Eval("TRACE_WRITE_INDENT") & @TAB) EndFunc Func EasyTraceUnindent() Set_("TRACE_WRITE_INDENT", StringTrimRight(Eval("TRACE_WRITE_INDENT"), 1)) EndFunc Func EasyTraceInfoLine($text) ConsoleWrite("+ INFO: " & $text & " +" & @CRLF) EndFunc Func EasyTraceWarningLine($text) ConsoleWrite("- WARNING: " & $text & " -" & @CRLF) EndFunc Func EasyTraceErrorLine($text) ConsoleWrite("! ERROR: " & $text & " !" & @CRLF) EndFunc and: ;------------------------------------------------------------------------------- ; Name .........: FileDeleteV2 ; Description ..: ; Return .......: ;------------------------------------------------------------------------------- Func FileDeleteV2($szPath, $isShortName = 0) $szPath_short = $szPath If Not $isShortName Then $szPath_short = FileGetShortNameV2($szPath) Return FileDelete($szPath_short) EndFunc ;==>FileDeleteV2 ;------------------------------------------------------------------------------- ; Name .........: FileDeleteV3 ; Description ..: ; Return .......: ;------------------------------------------------------------------------------- Func FileDeleteV3($szPath, $isShortName = 0) $res = 1 EasyTraceWriteLine("DEL FILE '" & $szPath & "'...") $szPathShort = $szPath If Not $isShortName Then $szPathShort = FileGetShortNameV2($szPath) If StringInStr(FileGetAttrib($szPathShort), "R") Then $res = FileSetAttrib($szPathShort, "-R") If Not $res Then EasyTraceWarningLine("FAILED REMOVE READ ATTRIBUTE FROM '" & $szPathShort & "'!") If Not IsSet("DRY_RUN") Then $res = FileDeleteV2($szPath, $isShortName) If Not $res Then EasyTraceInfoLine("RETRY DEL FILE '" & $szPath & "'.") ; Eventually retry Assert(Not IsSet("DRY_RUN"), "DRY_RUN global variable should not be set at this stage.") Sleep(10) If FileExists($szPathShort) Then $res = FileDeleteV2($szPath, $isShortName) ; Failed again? If Not $res Then EasyTraceErrorLine("DEL FILE '" & $szPath & "' FAILED.") EndIf Return $res EndFunc and: ;------------------------------------------------------------------------------- ; Name .........: IsSet ;------------------------------------------------------------------------------- Func IsSet($varname) $ret = 0 If IsDeclared($varname) Then $varval = Eval($varname) $ret = (IsString($varval) And ($varval <> "")) Or ($varval <> 0) EndIf Return $ret EndFunc ;==>IsSet Edited December 4, 2012 by Pinault Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now