Jump to content
Sign in to follow this  

Unable to use a variable as part of directory path

Recommended Posts



I've been using AutoIt for around a month and have found it very useful :) Have been able to automate most of the tasks I wanted to.

I have encountered a hiccup and not sure how to get this working.

Suppose there is a folder by name - ConfigurationFolder - in the same directory where my autoscript is present. Inside this folder, there is a file, logfile.txt.

There is an excel sheet which has the data regarding the folder name (ConfigurationFolder) and the file name (logfile.txt). I have an autoit script which reads the excel, captures this information in two variables... say $folder has the folder name and $file has the file name.

Something like... $folder = ConfigurationFolder

$file = logfile.txt

I would be using these two variables to check if the directory exists and check is the file exists. Then make use of the file.

For that, first I need to be able to use the variables as part of the directory path.

Suppose my autoit script is in the location - C:\Users\script.au3

When I try the below autoit script to assign the full path in the variable - $Directory, it doesn't work.

$Directory = @ScriptDir & "\$folder\$file"

"\$folder\$file" remains as it is, i.e., it gets translated to C:\Users\$folder\$file and not C:\Users\ConfigurationFolder\logfile.txt

Can someone please help me out here?



Share this post

Link to post
Share on other sites

$Directory = @ScriptDir & "$folder$file" is not correct but

$Directory = @ScriptDir & "" & $folder & "" & $file

which correct

also read help file -->FAQ

Share this post

Link to post
Share on other sites

There is also a second method.

See this script.

$folder = "myFolder"
$file = "myFile"

$Directory1 = @ScriptDir & "" & $folder & "" & $file

Opt("ExpandVarStrings", 1)
$Directory2 = "@ScriptDir@$folder$$file$"

MsgBox(262144,"",$Directory1 &@LF & $Directory2,0)

My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

Share this post

Link to post
Share on other sites

Thanks a lot for the quick response :) both methods work just fine!!

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
Sign in to follow this