Hi!
Suppose I want to save a file with AHK - with the name test (AHK know that the file is a txt file - I don't need to give the file extension .txt)
AHK also adds a suffix to the file name _01 - The final file name was .: test_01.txt
Next time I want to save a new file (another file) with the same name - test
This time AHK adds another suffix to the file name _02 the final file name was .: test_02.txt
Should I build an array to see if the filename has already been used? (and how many times)
Or is it better to use a loop file ... every time? (Maybe there will be 150/200 files in the directory )
The first time the file name is created (eg Test_01.txt) - no problem
Next time - maybe I want to know that the name has been duplicated again?
___________________________________________________________________________________________________________________________
Improved function
Maybe it's complicated to create a table that shows the names that are similar to the file name I enter?
(for information only
When the file is to be handled, I intend to use Switch / Case - All files with the name Test should be handled in the same way.)
Or that there are almost similar names (eg. Test / Tast or Testet) In the Switch/Case I only have defined Test
It becomes obvious when the case / switch is used, but better to know the problem before.
Manage many same filenames in some way
Re: Manage many same filenames in some way
There is no right answer-- do what works-- but FileExist, perhaps in conjunction with a counter, gives you a "fail safe" method of seeing whether the file already exists, regardless of how that file was created.
If you are subsequently processing files in a similar way according to file names or other parameters, then using a function might be convenient and efficient, depending on what you are trying to achieve. Again, there is not necessarily a right answer.
I use the following sort of loop in one of my routines.
If you are subsequently processing files in a similar way according to file names or other parameters, then using a function might be convenient and efficient, depending on what you are trying to achieve. Again, there is not necessarily a right answer.
I use the following sort of loop in one of my routines.
Code: Select all
target := dir ".xyz" , n := 0
While FileExist(target)
++n, target := dir n ".xyz"
Re: Manage many same filenames in some way
Thank you!
I think the naming of the files is most easy - maybe.
As I said, I try to simplify / improve the choice of file names based on the existing file names.
My idea - right now - is that in a GUI window there is an Edit box and a Table with all file names. (maybe without the suffix _01.jpg, _02.jpg etc.)
When a letter is entered in the edit field (eg. B) only filenames as begin with B is shown in the Table.
When the next letter is entered (eg. E) - only filenames as begin with BE is shown in the Table.
When the next letter is entered (eg. S) - only filenames as begin with BES is shown in the Table.
and so on...
Maybe when BESON is in the edit field, it stands BESON, BESON, BESON in the table (because the suffix of the names is removed).
Is it difficult to make the file search? (as described above) How? - any idea?
I think the naming of the files is most easy - maybe.
As I said, I try to simplify / improve the choice of file names based on the existing file names.
My idea - right now - is that in a GUI window there is an Edit box and a Table with all file names. (maybe without the suffix _01.jpg, _02.jpg etc.)
When a letter is entered in the edit field (eg. B) only filenames as begin with B is shown in the Table.
When the next letter is entered (eg. E) - only filenames as begin with BE is shown in the Table.
When the next letter is entered (eg. S) - only filenames as begin with BES is shown in the Table.
and so on...
Maybe when BESON is in the edit field, it stands BESON, BESON, BESON in the table (because the suffix of the names is removed).
Is it difficult to make the file search? (as described above) How? - any idea?
Re: Manage many same filenames in some way
A ListView would handle that well. You can use the native type-ahead feature to jump to a listing, or use an edit form as you have described, as a filter for the listview. I have one where I just use the ListView's "Select" option to select matching rows. There are other ways to do it, of course.
Re: Manage many same filenames in some way
I have encountered several problems.
The first difficulty is creating arrays of file names.
Starting here .:
Maybe aFileSh.13 is pointing at the same file as aFileLn.13 (but, I'm not 100% sure)
Would have preferred to handle just one array
I have tried this way (but it doesn't work for me)
The first difficulty is creating arrays of file names.
Starting here .:
Code: Select all
dirSelect := "..\LogoLib\*.*" ; Select directory
aFileSh := []
aFileLn := []
Loop % dirSelect ; Read the files in the directory
{ aFileSh.push(subStr(A_LoopFileName, 1, inStr(A_LoopFileName, ".")-1))
aFileLn.push(A_LoopFileLongPath)
}
For k, v in aFileLn
res .= "k.: " k "`t`tv.: " v "`n"
MsgBox ,, %A_ScriptName% - Rad %A_LineNumber%, % res
Would have preferred to handle just one array
I have tried this way (but it doesn't work for me)
Code: Select all
aFile := []
Loop % dirSelect ; Read the files in the directory
{ SplitPath A_LoopFileLongPath, OutFileName, OutDir, OutExtension, OutNameNoExt
aFile.sh.push(OutNameNoExt)
aFile.ln.push(A_LoopFileLongPath)
}
MsgBox ,, %A_ScriptName% - Rad %A_LineNumber%, % aFile.sh.12 "`n" aFile.ln.12
Re: Manage many same filenames in some way
"Doesn't work" is difficult to debug. What is the actual output? What is the desired output?
Re: Manage many same filenames in some way
Now is the desired output
1) an modified filename - maybe remove the prefix (ö&... or ä&...) on some files
and some suffix (_01, _02 and so on) In this test case I only save file name without extension.
2) In the other "output" I want to have the full filename with path.
3?)Maybe I want to / must save some other information from the file
(right now I don't know)
with another dirSelect it's easy to test the code .:
1) an modified filename - maybe remove the prefix (ö&... or ä&...) on some files
and some suffix (_01, _02 and so on) In this test case I only save file name without extension.
2) In the other "output" I want to have the full filename with path.
3?)Maybe I want to / must save some other information from the file
(right now I don't know)
with another dirSelect it's easy to test the code .:
Code: Select all
#SingleInstance force
dirSelect := "..\LogoLib\*.*" ; Select directory
aFile := []
Loop % dirSelect ; Read the files in the directory
{ SplitPath A_LoopFileLongPath, OutFileName, OutDir, OutExtension, OutNameNoExt
aFile.sh.push(OutNameNoExt)
aFile.ln.push(A_LoopFileLongPath)
}
MsgBox ,, %A_ScriptName% - Rad %A_LineNumber%, % aFile.sh.12 "`n" aFile.ln.12
Re: Manage many same filenames in some way
Please provide an example of the desired output that the MsgBox should show.
Re: Manage many same filenames in some way
Thank you!
I must define even the under arrays like this .:
aFile := [], aFile.sh := [], aFile.ln := []
Now this part works!
The MsgBox only show information about one file like this.
I must define even the under arrays like this .:
aFile := [], aFile.sh := [], aFile.ln := []
Now this part works!
Code: Select all
dirSelect := "..\LogoLib\*.*" ; Select directory
aFile := [], aFile.sh := [], aFile.ln := []
Loop % dirSelect ; Read the files in the directory
{ SplitPath A_LoopFileLongPath, OutFileName, OutDir, OutExtension, OutNameNoExt
aFile.sh.push(OutNameNoExt)
aFile.ln.push(A_LoopFileLongPath)
}
MsgBox ,, %A_ScriptName% - Rad %A_LineNumber%, % aFile.sh.12 "`n" aFile.ln.12
For k, v in aFile.ln
res .= "k.: " k "`t`tv.: " v "`n"
MsgBox ,, %A_ScriptName% - Rad %A_LineNumber%, % res
- - TestFile
- - c:\.....\TestFile.jpg
Re: Manage many same filenames in some way
Glad you have it working!
Re: Manage many same filenames in some way
Next problem was...
1)
Assume that a file name is missing in a directory.
The first time the file (eg name TB) is created it will be named TB_01.jpg (or ö&TB_01.jpg - depending on other choices)
No problem - Now the file TB_01.jpg exist.
2) Now the user give the name TB (again)
But now the file TB_01.jpg exist.
The file Name should be ow the file TB_02.jpg.
3) And next time TB_03.jpg (and so on)
One problem I can see is to add 01 + 1 = 02
Check if that filename exist. If yes - try TB_03.jpg
I think all files need to be compared again (with the new file name)?
I'm not sure how to handle this...
1)
Assume that a file name is missing in a directory.
The first time the file (eg name TB) is created it will be named TB_01.jpg (or ö&TB_01.jpg - depending on other choices)
No problem - Now the file TB_01.jpg exist.
2) Now the user give the name TB (again)
But now the file TB_01.jpg exist.
The file Name should be ow the file TB_02.jpg.
3) And next time TB_03.jpg (and so on)
One problem I can see is to add 01 + 1 = 02
Check if that filename exist. If yes - try TB_03.jpg
I think all files need to be compared again (with the new file name)?
I'm not sure how to handle this...
Re: Manage many same filenames in some way
I provided an example in my first post.
Re: Manage many same filenames in some way
I do not understand what's happens (how to use it)mikeyww wrote: ↑14 May 2021, 10:23....I use the following sort of loop in one of my routines.Code: Select all
target := dir ".xyz" , n := 0 While FileExist(target) ++n, target := dir n ".xyz"
Re: Manage many same filenames in some way
This routine increments your file suffix until it identifies a file that does not already exist.
Code: Select all
dir = %A_ScriptDir% ; The directory of the image files
suffix := 1 ; The starting file suffix
Loop
target := Format(dir "\TB_{:02}.jpg", suffix++) ; File path
Until !FileExist(target)
MsgBox, 64, Target, %target%
Re: Manage many same filenames in some way
Thank you!
I'm still not quite sure what's happen. (I will test this solution)
(But I have to be sure that nothing can go wrong)
Can I always be sure that the files will be compared in ascending order?
TB_01.jpg
TB_02.jpg
Then TB_03.jpg is the new name (if the filename not exist)
(all the filenames is already in an array)
It will never be compared in descending order.
TB_02.jpg
TB_01.jpg
Edit
TB_01.jpg must exist before I use the loop above?
I'm still not quite sure what's happen. (I will test this solution)
(But I have to be sure that nothing can go wrong)
Can I always be sure that the files will be compared in ascending order?
TB_01.jpg
TB_02.jpg
Then TB_03.jpg is the new name (if the filename not exist)
(all the filenames is already in an array)
It will never be compared in descending order.
TB_02.jpg
TB_01.jpg
Edit
TB_01.jpg must exist before I use the loop above?
Last edited by Albireo on 18 May 2021, 14:53, edited 1 time in total.
Re: Manage many same filenames in some way
It sure looks that way, but you need not take my word for it. Try the script; see if it works! suffix++ increments the value by one.
The script requires no pre-existing files. The final %target% contains the new file path.
The script requires no pre-existing files. The final %target% contains the new file path.
Re: Manage many same filenames in some way
Thanks!
(But maybe you don't understand my problem/wish?)
In the script (below), there is three differnet test values in the InputName. - ( TB, PT and CBA )
- - - - - - -
These files below are found in a directory, and saved in an array aFile.sh.
(I do not know if this is a possible sequence) but ...
The expected result from the different InputName should be .:
(But maybe you don't understand my problem/wish?)
In the script (below), there is three differnet test values in the InputName. - ( TB, PT and CBA )
- - - - - - -
These files below are found in a directory, and saved in an array aFile.sh.
(I do not know if this is a possible sequence) but ...
- ABC.01.jpg
- ABC.02.jpg
- TB_01.jpg
- CBA_02.jpg
- CBA_01.jpg
Code: Select all
#SingleInstance force
InputName := "TB"
; InputName := "PT"
; InputName := "CBA"
; Files In the direcxtory (in this case not sorted)
aFile := [], aFile.sh := []
aFile.sh := ["ABC.01.jpg","ABC.02.jpg","TB_01.jpg","CBA_02.jpg","CBA_01.jpg"]
/*
For k, v in aFile.sh
res .= "k.: " k "`t`tv.: " v "`n"
MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%, % res
*/
; - - - - - -
; dir = %A_ScriptDir% ; The directory of the image files
suffix := 1 ; The starting file suffix
Loop
target := Format("\" InputName "_{:02}.jpg", suffix++) ; File path
Until !FileExist(target)
MsgBox, 64, Target, %target%
- TB - to name -> TB_02.jpg
- PT - to name -> PT_01.jpg
- CBA - to name -> CBA_03.jpg
Re: Manage many same filenames in some way
Code: Select all
aFile := [], aFile.sh := []
aFile.sh := ["ABC.01.jpg","ABC.02.jpg","TB_01.jpg","CBA_02.jpg","CBA_01.jpg"]
MsgBox, 64, TB, % next(aFile.sh, "TB_")
MsgBox, 64, TB, % next(aFile.sh, "PT_")
MsgBox, 64, TB, % next(aFile.sh, "CBA_")
next(array, prefix) {
suffix := 1
Loop
For k, v in array
found := v = fn := Format("{}{:02}.jpg", prefix, found ? suffix += found : suffix)
Until found
Until !found
Return fn
}
Who is online
Users browsing this forum: Bing [Bot] and 142 guests