Using a variable in a path ?

Discussions specific to Team Developer 5.2.

Using a variable in a path ?

Postby JaakkoTerhonen » Fri Apr 24, 2015 3:01 am

Hello!

If I use a variable as a path I get a funny result :-)

First I noticed that referring to files by using a Windows variable in the path doesn't work.

Then I tried creating a file and for example creating a file into path

.............. %TEMP%

ends up with a file in

...............<current directory>
....................<new directory named "%TEMP%">

Does this mean I simply can't use variables in path names in Team Developer?

Thanks & best regards,

Jaakko Terhonen
JaakkoTerhonen
 
Posts: 33
Joined: Fri Jan 25, 2008 9:20 am

Re: Using a variable in a path ?

Postby Jeff @ PC Design » Fri Apr 24, 2015 10:11 am

I used SalStrScan() to try to find an answer for you because that function needs a double backslash ('\\') in certain cases to force TD to use the next char. literally. I found this in Help's description:
When searching for a percent character or an underscore character and strString2 is a string literal, you need two backslashes:
SalStrScan ( 'This is a %', '\\%' )

So try this in a test by adding the double backslash pair ('\\') in front of each percent sign: ".............. \\%TEMP\\%"
Hopefully that is the problem. What TD must have been doing in your example was treating %TEMP% literally as a directory name.
Jeff Luther/PC Design http://www.jeffluther.net/TD/
Image
Come join us at Dave Rabelink's "Team Dev. Community Forum".
All TD, TDMobile & SQLBase users welcome!
(http://tdforum.daverabelink.net/index.php)
Jeff @ PC Design
 
Posts: 206
Joined: Fri Feb 08, 2013 2:40 pm
Location: Palm Springs, California

Re: Using a variable in a path ?

Postby Dave Rabelink » Fri Apr 24, 2015 1:51 pm

Unfortunately, TD does not automatically expand system variables (like %TEMP%).

You have to do this yourself, here some options:

Set sTempDir = VisDosGetEnvString ('TEMP')
Set sFullFilePath = sTempDir || "MyFile.txt"

! Use sFullFilePath in TD file functions.


or

The WinApi function ExpandEnvironmentStringsW
https://msdn.microsoft.com/en-us/librar ... 85%29.aspx

The advantage of the WinApi function is that you can use an input string having one or more system variables combined with 'normal' text.

So passing this string :
Code: Select all
"
My computername = %COMPUTERNAME%
My username = %USERNAME%
The temp folder is %TEMP%
"


will result (on my system) in :

Code: Select all
"
My computername = DellLaptop
My username = Dave en Esther
The temp folder is C:\Users\Dave en Esther\AppData\Local\Temp
"
Regards,
Dave Rabelink

Image
Articles on Team Developer Wiki
Download samples from TD Sample Vault

Image
Go forward! Come join the TD, SqlBase & TDMobile community on
Team Developer Community Forum
User avatar
Dave Rabelink
 
Posts: 1655
Joined: Sat Feb 17, 2007 12:01 am
Location: Gouda, The Netherlands

Re: Using a variable in a path ?

Postby JaakkoTerhonen » Sun Apr 26, 2015 4:54 am

Thank You very much Dave - and also Jeff for throwing a good suggestion! Might have worked...

This time my customer found another solution for the need (don't know what..) so I don't have to code the Windows API call, and so the Dave's very good solution just goes to the "very good to know" folder.

Best regards,
Jaakko
JaakkoTerhonen
 
Posts: 33
Joined: Fri Jan 25, 2008 9:20 am


Return to Team Developer 5.2

Who is online

Users browsing this forum: No registered users and 2 guests