Jump to content

Recommended Posts

Posted (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 by Colduction
Posted
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

:P 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted

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

 

Posted
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 😁

Posted
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

:P 

Thanks for your code, but it wasn't what i want

Totally thanks for your care.

Posted
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 changed the title to [SOLVED] Create file with Unique filename If that file exists

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...