Posts: 111
Threads: 31
Joined: Sep 2014
Good Morning,
Could someone provide me with a short ex. of converting a folder's: a) filename w. extension and b) the path to variables
Let's say we have a folder:
C:\folder
with 3 files:
file1.txt
file2.txt
file3.txt
How can I have QM do this for any folder?
Any help would be greatly appreciated!!!!
Kent
Posts: 12,061
Threads: 140
Joined: Dec 2002
With dialog "Enumerate files" you can create this code:
Macro Macro2659
Dir d
foreach(d "C:\folder\*" FE_Dir)
,str path=d.FullPath
,out path
,str name=d.FileName
,
If need variables, replace it to:
Macro Macro2659
str folder="C:\folder"
str files="*"
Dir d
foreach(d F"{folder}\{files}" FE_Dir)
,str path=d.FullPath
,out path
,str name=d.FileName
,
Or use function GetFilesInFolder and pass variables as its second and third arguments.
Posts: 111
Threads: 31
Joined: Sep 2014
Thanks! -and shall continue w. my work now!
Posts: 111
Threads: 31
Joined: Sep 2014
Gintaras, as ALWAYS I truly appreciate your help!!!
What I actually need is a variable for each/every filename in a folder, so that if a folder had these three files
file1.txt
file2.txt
file3.txt
the result would be three variables: var1, var2, var3 (var1 = file1.txt; var2 = file2.txt; & var3 = file3.txt)
The result of the examples you provided appear to yield only one variable, that being for the last file treated by the 'foreach' function.
You mentioned "Or use function GetFilesInFolder and pass variables as its second and third arguments" but after looking through the documentation I can't figure out how to do this???
Thanks Again
Posts: 12,061
Threads: 140
Joined: Dec 2002
Macro Macro2661
ARRAY(str) a
GetFilesInFolder a folder "*"
out a[0] ;;variable 1
out a[1] ;;variable 2
out a[2] ;;variable 3
Posts: 111
Threads: 31
Joined: Sep 2014
Gintaras, THANK YOU ONCE MORE!!! I'm not a programmer nor computer specialist! but as I have shared, I have more than a hundred QM macros w. literally thousands of lines of code to help with my research and it is truly wonderful!
Your last example is what I am after, but I have one more question for now:
What if I create a macro to generate a variable for each 'filenamepath' & a variable for each 'filename.xxx' in a folder w. "GetFilesInFolder" but I don't know how many files are in the folder? Let's say that this macro could be run for any folder on my computer.
Could you provide an example of this?
Posts: 12,061
Threads: 140
Joined: Dec 2002
Macro Macro2661
str folder="$my qm$"
ARRAY(str) a
GetFilesInFolder a folder "*"
int i
for i 0 a.len
,out a[i]
,str filename.getfilename(a[i] 1)
,out filename
,
Posts: 111
Threads: 31
Joined: Sep 2014
Wonderful!!! this string and my other post from yesterday (How to extrapolate a specific element in a filename) have me on my way to creating my next macro.
THANKS!!!!!!! I tell everyone in my research world about QM!
Posts: 111
Threads: 31
Joined: Sep 2014
Gintaras, the last post does indeed do precisely what I want in step one of the macro or function. I have REALLY tried ... read ... etc. ... to do the next steps and with no luck SO I will present an additional question(s) to you.
Here is what I REALLY want this function/macro to do:
1) create a variable for every file found in one of my 'pattern' folders
2) create a variable for the file-path for each file in that folder (C:\folder\filename1{cat}.txt)
3) create a variable for the keyword inside the curly brackets for each file in this folder ("cat" from the above example}
(You graciously showed me how to do this for a single file in the post yesterday "How to extrapolate a specific element in a filename")
Example, the macro/function finds the three files in the folder below and generates 9 variables for this
Folder C:\folder\ (has 3 files)
filename1{cat}.txt
filename2{dog}.txt
filename3{cow}.txt
The 9 variables generated would/could be something like:
varForFilepath1 = C:\folder\filename1{cat}.txt
varForFilename1= filename1{cat}.txt
varForKeyword1 = cat
varForFilepath2 = C:\folder\filename2{dog}.txt
varForFilename2= filename2{dog}.txt
varForKeyword2= dog
varForFilepath3 = C:\folder\filename3{cow}.txt
varForFilepath3 = filename3{cow}.txt
varForKeyword3 = cow
THANKS in ADVANCE for any help you provide!!!!!!!!!!!
Kent
Posts: 12,061
Threads: 140
Joined: Dec 2002
Macro Macro2663
type T37254332 ~path ~name ~keyword
ARRAY(T37254332) a
Dir d
foreach(d "C:\folder\*.txt" FE_Dir)
,T37254332& r=a[]
,r.path=d.FullPath
,r.name=d.FileName
,findrx(r.path "\{(.+?)\}.txt$" 0 1 r.keyword 1)
;results
int i
for i 0 a.len
,T37254332& u=a[i]
,out u.path
,out u.name
,out u.keyword
,
Posts: 111
Threads: 31
Joined: Sep 2014
Gintaras, thank you
BUT
I look at your last post, as I have for the past hour and a half ... reading through the QM documentation ... can't find anything on 'Dir'
and I wonder, 'How would I have ever figured this out on my own? Wow! I know that --YOU KNOW-- that not everyone thinks the same! I truly do appreciate your help and I would gladly pay for it! Really! (I suppose that your site still says that you don't do this for a fee?) Anyway, this is wonderful.
Gintaras, this still leaves me one step short on my end. I don't know how to or can't figure out 'how-to' create a variable for each of the items that is presented in the:
out u.path
out u.name
out u.keyword
of your last post. The BIG QUESTION for me at the moment is:
How do I convert each item that is listed in the 'out result' to a variable that I can use? So that I have:
varForFilepath1 = C:\folder\filename1{cat}.txt
varForFilename1= filename1{cat}.txt
varForKeyword1 = cat
varForFilepath2 = C:\folder\filename2{dog}.txt
varForFilename2= filename2{dog}.txt
varForKeyword2= dog
varForFilepath3 = C:\folder\filename3{cow}.txt
varForFilepath3 = filename3{cow}.txt
varForKeyword3 = cow''
(Gintaras, I am able to create a variable for ONLY the last for each list. For instance:
str keywordx
keywordx=u.keyword
does assign the last keyword generated, 'cow', to 'keywordx')
BUT, as stated, I need to create a variable for every item that your previous code generates?
I can then use these variables to populate a Quick Macro's "List Box". For instance, the List-box would reveal:
Listbox list:
cat
dog
cow
And this choice of one of these would activate the rest of the macro, which has a few hundred lines and about 25 functions. (So my ultimate quest is to populate the 'listbox' with the 'keywords' generated from the folder. Do note, all files in this folder, that are limited to a maximum of 12, must comply with an assigned syntax that includes the "{keyword}" elements and no two files in the folder can have the same/identical 'keyword'.)
AND, you may be tired of hearing it, BUT I DO TRULY APPRECIATE OUR HELP!
Posts: 12,061
Threads: 140
Joined: Dec 2002
Quote:varForFilepath1 = C:\folder\filename1{cat}.txt
varForFilename1= filename1{cat}.txt
varForKeyword1 = cat
varForFilepath2 = C:\folder\filename2{dog}.txt
varForFilename2= filename2{dog}.txt
varForKeyword2= dog
varForFilepath3 = C:\folder\filename3{cow}.txt
varForFilepath3 = filename3{cow}.txt
varForKeyword3 = cow''
variable a[0].path is "C:\folder\filename1{cat}.txt"
variable a[0].name is "filename1{cat}.txt"
variable a[0].keyword is "cat"
variable a[1].path is "C:\folder\filename2{dog}.txt"
and so on
Posts: 111
Threads: 31
Joined: Sep 2014
Ahhh! wonderful!, beautiful!!!!! Thank you! Live & learn!
|