Jump to content
Colduction

[SOLVED] Create file with Unique filename If that file exists

Recommended Posts

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

I recommend to you to get COVID-19, but don't get Wife

Share this post


Link to post
Share on other sites
2 hours ago, Danp2 said:

previously discussed on the forum

Some threads was similar but they were not what i want


I recommend to you to get COVID-19, but don't get Wife

Share this post


Link to post
Share on other sites
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 

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites
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 😁


I recommend to you to get COVID-19, but don't get Wife

Share this post


Link to post
Share on other sites
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.


I recommend to you to get COVID-19, but don't get Wife

Share this post


Link to post
Share on other sites
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) :)❤


I recommend to you to get COVID-19, but don't get Wife

Share this post


Link to post
Share on other sites
3 hours ago, argumentum said:

<duplicated entry>

Which thread was same as this thread? show me (Please)


I recommend to you to get COVID-19, but don't get Wife

Share this post


Link to post
Share on other sites
10 hours ago, argumentum said:

My internet is messed up

I'm really sorry, i thought badly about you

Excuse me sir :)❤


I recommend to you to get COVID-19, but don't get Wife

Share this post


Link to post
Share on other sites

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Tippex
      I have a problem with FileOpenDialog using long default filenames ... they always get truncated. 
      For example: 
      "A Long FileName.mpg" as a default would prompt as just "FileName.mpg" (but scrolling left will show it named correctly).
      Does anyone know of a fix for this please (I didn't spot it in a Forum search), or is it one for the bug tracker?
      #include <FileConstants.au3> #include <MsgBoxConstants.au3> Local Const $sMessage = "Hold down Ctrl or Shift to choose multiple files." Local $sFileOpenDialog = FileOpenDialog($sMessage, @WindowsDir & "\", "Images (*.jpg;*.bmp)|Videos (*.avi;*.mpg)", BitOR($FD_FILEMUSTEXIST, $FD_MULTISELECT), "A Long FileName.mpg") Thanks,
    • By tezhihi
      Hi All,
      I have many files inside folder with ID Number. The problem here is I want to get ID Number,  file name inside folder and Get Size of its. However when I use loop "For" the result appeared with duplicate data. Please check the example in attached file and help me make the code below better for this case.
      #include <File.au3> #include <Array.au3> Global $a, $b $a = _FileListToArrayEx(@ScriptDir, '*.visf') For $i = 1 to $a[0] $b = StringSplit($a[$i], "\") ;~ _ArrayDisplay($b) For $j = 1 to $b[0] ;~ ConsoleWrite($b[$b[0] - 1] & '_' & $b[$b[0]] & ' ' & FileGetSize($a[$i]) & @CRLF) FileWrite(@ScriptDir & '\result.txt', $b[$b[0] - 1] & '_' & $b[$b[0]] & ' ' & FileGetSize($a[$i]) & @CRLF) Next Next Func _FileListToArrayEx($s_path, $s_mask = "*.*", $i_flag = 0, $s_exclude = -1, $f_recurse = True, $f_full_path = True) If FileExists($s_path) = 0 Then Return SetError(1, 1, 0) ; Strip trailing backslash, and add one after to make sure there's only one $s_path = StringRegExpReplace($s_path, "[\\/]+\z", "") & "\" ; Set all defaults If $s_mask = -1 Or $s_mask = Default Then $s_mask = "*.*" If $i_flag = -1 Or $i_flag = Default Then $i_flag = 0 If $s_exclude = -1 Or $s_exclude = Default Then $s_exclude = "" ; Look for bad chars If StringRegExp($s_mask, "[/:><\|]") Or StringRegExp($s_exclude, "[/:><\|]") Then Return SetError(2, 2, 0) EndIf ; Strip leading spaces between semi colon delimiter $s_mask = StringRegExpReplace($s_mask, "\s*;\s*", ";") If $s_exclude Then $s_exclude = StringRegExpReplace($s_exclude, "\s*;\s*", ";") ; Confirm mask has something in it If StringStripWS($s_mask, 8) = "" Then Return SetError(2, 2, 0) If $i_flag < 0 Or $i_flag > 2 Then Return SetError(3, 3, 0) ; Validate and create path + mask params Local $a_split = StringSplit($s_mask, ";"), $s_hold_split = "" For $i = 1 To $a_split[0] If StringStripWS($a_split[$i], 8) = "" Then ContinueLoop If StringRegExp($a_split[$i], "^\..*?\..*?\z") Then $a_split[$i] &= "*" & $a_split[$i] EndIf $s_hold_split &= '"' & $s_path & $a_split[$i] & '" ' Next $s_hold_split = StringTrimRight($s_hold_split, 1) If $s_hold_split = "" Then $s_hold_split = '"' & $s_path & '*.*"' Local $i_pid, $s_stdout, $s_hold_out, $s_dir_file_only = "", $s_recurse = "/s " If $i_flag = 1 Then $s_dir_file_only = ":-d" If $i_flag = 2 Then $s_dir_file_only = ":D" If Not $f_recurse Then $s_recurse = "" $i_pid = Run(@ComSpec & " /c dir /b " & $s_recurse & "/a" & $s_dir_file_only & " " & $s_hold_split, "", @SW_HIDE, 4 + 2) While 1 $s_stdout = StdoutRead($i_pid) If @error Then ExitLoop $s_hold_out &= $s_stdout WEnd $s_hold_out = StringRegExpReplace($s_hold_out, "\v+\z", "") If Not $s_hold_out Then Return SetError(4, 4, 0) ; Parse data and find matches based on flags Local $a_fsplit = StringSplit(StringStripCR($s_hold_out), @LF), $s_hold_ret $s_hold_out = "" If $s_exclude Then $s_exclude = StringReplace(StringReplace($s_exclude, "*", ".*?"), ";", "|") For $i = 1 To $a_fsplit[0] If $s_exclude And StringRegExp(StringRegExpReplace( _ $a_fsplit[$i], "(.*?[\\/]+)*(.*?\z)", "\2"), "(?i)\Q" & $s_exclude & "\E") Then ContinueLoop If StringRegExp($a_fsplit[$i], "^\w:[\\/]+") = 0 Then $a_fsplit[$i] = $s_path & $a_fsplit[$i] If $f_full_path Then $s_hold_ret &= $a_fsplit[$i] & Chr(1) Else $s_hold_ret &= StringRegExpReplace($a_fsplit[$i], "((?:.*?[\\/]+)*)(.*?\z)", "$2") & Chr(1) EndIf Next $s_hold_ret = StringTrimRight($s_hold_ret, 1) If $s_hold_ret = "" Then Return SetError(5, 5, 0) Return StringSplit($s_hold_ret, Chr(1)) EndFunc  
      Example.zip
    • By ur
      I have an ini file which has structure as below.

      And I want to use this value and change only the file name in below path of another ini file.
       
      Can you suggest any approach.


    • By 31290
      Hi everyone
      Hope you're doing great today.
      Well, I have a little question about extracting a certain string from a file name to make comparison after treatment.
      Let's get into the details:
      First of all, I gather and store a machine BIOS Version by running this:
      RunWait(@ComSpec & " /c " & "wmic bios get SMBIOSBIOSVERSION >> C:\Drivers\Tag.txt" & @CRLF, "", @SW_HIDE, "$STDOUT_CHILD") _FileWriteToLine("c:\Drivers\BIOS_Version.txt", 1, "", 1) $sContent = FileRead ("C:\Drivers\BIOS_Version.txt") $sContent = StringRegExpReplace($sContent, " ", "") Result is, for example, A10
      Then, I download the latest available BIOS version from the Dell related model website and the filename of the latest BIOS is stored in a $sBIOSName variable.
      Result is for example, E5440A14.exe
      What i can't figure out is how to extract the Bios name stored in the $sBIOSName knowing that each model this app would run won't have the same number of chars.
      Indeed, I have some OPL790AXX.exe / OPL7010AXX.exe / E5470AXX.exe etc...
      I imagine something like getting the whole string, left trimming it to the first A found count 2 chars on the right and store them to have the possibility to make the check between A10 and A14 (as the number of char of filenames is never the same)
      Hope this is clear  If not, don't hesitate to ask for more.
      Thanks in advance for the help
       
    • By Scottswan
      Basically I'm looking to make a snapshot of a whole HD's folders and files in one text file. Just the names. Preferably in alphabetical order.
      Folder 1
          File 1
          File 2
          File 3
      Folder 2
         Etc

      Is there a function that can copy just the names without opening the files? An alternative would be to put each into rename mode and then copy the text, but that is going to be painfully slow.
      Thanks.
×
×
  • Create New...