mrfitness


I have a query within a loop that will be creating many files. It is based on a (variable) 'where clause'. What I wanted to do was to grab the first 3 characters of the (numeric) variable 'where clause', and create a folder named with that variable, and place the results of the query in that folder.
I think I can get the program to work if I figure out how to tell VFP to create a folder named as part of a variable.
ie.
'a' is the variable that changes each time in the loop. It is a numeric value.
i want to grab the substring of a [eg.: b = subs(a,1,3) ] and create a folder named whatever b is storing and put the results of the query in it (probably use an if statement or something)

Any help would be greatly appreciated. Thanks


Re: Creating a folder using VFP6

Naomi Nosonovsky


lcFolderName = "c:\MyDir" + substr(a,1,3)

md (m.lcFolderName)

I would advise you to use meangful variable names instead of a, b, etc. for several reasons:

1. Maintenance

2. letters a-j are reserved for work areas

If you want to use variable in the SQL, it depends on the location, e.g.

lcTable = 'c:\MyDir\MyTable'

lcNewTable = 'c:\MyDir\MyNewTable'

lcWhere = "MyIntegerField = 5"

select * from (m.lcTable) where &lcWhere into table (m.lcNewTable)





Re: Creating a folder using VFP6

Alex Feldstein

A simple example

lcFolder = "X" + SUBSTR(myvar,1,3)

MD (lcFolder) && to create a folder under the current folder. You could use absolute path too

* use ON ERROR to make sure the folder creation did not fail (e.g. write permissions, parent folder not found, folder already exists)

SELECT field1, field2 FROM myfile INTO TABLE (lcfolder + "mytable")

HTH






Re: Creating a folder using VFP6

mrfitness

Thank you so much Naomi...you've been a great help!
Can you explain the meaning/functionality of 'md' and '(m.lcfoldername)'




Re: Creating a folder using VFP6

Naomi Nosonovsky

Good point about ON ERROR. In VFP9 you may use try/catch.



Re: Creating a folder using VFP6

Alex Feldstein

mrfitness wrote:
Thank you so much Naomi...you've been a great help!
Can you explain the meaning/functionality of 'md' and '(m.lcfoldername)'

MD is short for MKDIR

(m.lcfoldername) is a named-variable where you are using indirection to tell the system to create a folder using the contents of the variable at runtime.




Re: Creating a folder using VFP6

Naomi Nosonovsky

md is a command to create a directory, AFAIK the same as in DOS. You can also use MKDIR command.

When you put variable in parents, it means, that it should be evaluated and it is called name expression. It is sort of a macro, but it is quicker than macro and easier to understand.

On a for profit website I can not name here there is a FAQ describing difference between macros, name expressions and evaluate. I hope I can post a link, though Smile





Re: Creating a folder using VFP6

mrfitness

I am not good at ON ERROR functions...it would be good to have in case the folder was already created Smile




Re: Creating a folder using VFP6

mrfitness

can you give me an example of doing an ON ERROR to handle if the folder already exists




Re: Creating a folder using VFP6

Naomi Nosonovsky

Simplest way

Code Snippet

local lnError

lnError = 0

on error lnError = error()

md (m.lcMyDir)

if m.lnError <> 0

* Error occurred, analyze it

endif