Colduction Posted February 26, 2020 Posted February 26, 2020 (edited) Hi guys! i want to write speedy and very fast code to check if chosen filename exists then add "-1" at end of it and if chosen filename exists again, add "-2" instead of "-1" and check filename until be unique. For Example: I have to write "Filename.txt" in C:\ directory with FileOpen then check if "Filename.txt" exists in that directory then rename chosen filename to "Filename-2.txt" and if exists again in directory, rename chosen filename to "Filename-3.txt" and do this work until to find unique number to add end of filename. ====================== SOLUTION by @Subz ====================== Edited May 20, 2020 by Colduction
Danp2 Posted February 26, 2020 Posted February 26, 2020 What have you tried? Seems like this has been previously discussed on the forum. Use the forum search function (or better yet just Google it) to find some examples. Latest Webdriver UDF Release Webdriver Wiki FAQs
Colduction Posted February 27, 2020 Author Posted February 27, 2020 2 hours ago, Danp2 said: previously discussed on the forum Some threads was similar but they were not what i want
Nine Posted February 27, 2020 Posted February 27, 2020 And in what way they were not waht you want ? Give a code example of what is not working and then we shall see if we can give you help... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
argumentum Posted February 27, 2020 Posted February 27, 2020 Local $sPathLeft, $sExt, $sPath = "C:\full path.ext" If FileExists($sPath) Then $sPathLeft = StringLeft($sPath, StringInStr($sPath, ".", 0, -1)) $sExt = StringTrimLeft($sPath, StringLen($sPathLeft)) $sPath = $sPathLeft & "_" & @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & @MSEC & "_." & $sExt EndIf Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
argumentum Posted February 27, 2020 Posted February 27, 2020 (edited) <duplicated entry> Edited February 27, 2020 by argumentum Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
Subz Posted February 27, 2020 Posted February 27, 2020 Or maybe something like: #include <File.au3> ConsoleWrite(_UniqueFileName(@ScriptDir & "\Filename.txt") & @CRLF) Func _UniqueFileName($_sFilePath) If FileExists($_sFilePath) = 0 Then Return $_sFilePath Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = "" Local $aPathSplit = _PathSplit($_sFilePath, $sDrive, $sDir, $sFileName, $sExtension) Local $i = 1 While 1 $_sFilePath = $sDrive & $sDir & $sFileName & "-" & $i & $sExtension If FileExists($_sFilePath) = 0 Then Return $_sFilePath $i += 1 WEnd EndFunc Colduction and Xandy 2
Colduction Posted February 27, 2020 Author Posted February 27, 2020 3 hours ago, Nine said: And in what way they were not waht you want ? Give a code example of what is not working and then we shall see if we can give you help... Hi @Nine, i didn't write useful code, all i know and i think about this, can be used Loop for check existing a file 😁
Colduction Posted February 27, 2020 Author Posted February 27, 2020 3 hours ago, argumentum said: Local $sPathLeft, $sExt, $sPath = "C:\full path.ext" If FileExists($sPath) Then $sPathLeft = StringLeft($sPath, StringInStr($sPath, ".", 0, -1)) $sExt = StringTrimLeft($sPath, StringLen($sPathLeft)) $sPath = $sPathLeft & "_" & @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & @MSEC & "_." & $sExt EndIf Thanks for your code, but it wasn't what i want Totally thanks for your care. argumentum 1
Colduction Posted February 27, 2020 Author Posted February 27, 2020 1 hour ago, Subz said: Or maybe something like: #include <File.au3> ConsoleWrite(_UniqueFileName(@ScriptDir & "\Filename.txt") & @CRLF) Func _UniqueFileName($_sFilePath) If FileExists($_sFilePath) = 0 Then Return $_sFilePath Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = "" Local $aPathSplit = _PathSplit($_sFilePath, $sDrive, $sDir, $sFileName, $sExtension) Local $i = 1 While 1 $_sFilePath = $sDrive & $sDir & $sFileName & "-" & $i & $sExtension If FileExists($_sFilePath) = 0 Then Return $_sFilePath $i += 1 WEnd EndFunc Thanks @Subz, it's what i want, it checks until get unique file name❤ I wrote similar to your code but i had a problem with determining address in Wile...WEnd loop. Thanks for your code (like a fish) :)❤
Colduction Posted February 27, 2020 Author Posted February 27, 2020 3 hours ago, argumentum said: <duplicated entry> Which thread was same as this thread? show me (Please)
argumentum Posted February 27, 2020 Posted February 27, 2020 55 minutes ago, Ho3ein said: Which thread was same none. My internet is messed up and created a double entry Colduction 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
Colduction Posted February 27, 2020 Author Posted February 27, 2020 10 hours ago, argumentum said: My internet is messed up I'm really sorry, i thought badly about you Excuse me sir :)❤
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