Jump to content

DirCopy and environment variables


Recommended Posts

Here is the issue:

If FileExists("C:\Documents and Settings\%UserName%.jaz\Favorites") Then

DirCopy ("C:\documents and settings\%UserName%\Favorites", "C:\documents and settings\%UserName%.jaz\Favorites", 1)

EndIF

I want to copy the favorites over from the recently logged in user to a previously logged in user with the same name, but with a different domain extension (.jaz). I want to do this for anyone that logs in from the previous domain and not just a specific user.

If I run the command without the environmental variable (%username%) it works or if I use the windows cmd line with the environmental variable than it works fine, but if I put it in with DirCopy of FileExists than it does not work.

Why is that ? Is there another way to do this?

Thanks in advance

Edited by autoitNOW
An ADVOCATE for AutoIT
Link to comment
Share on other sites

There are two possibilities. Search for ExpandEnvStrings or EnvGet() in the helpfile.

The first one allows you to use the syntax you tried, the second allows you to retrieve environment vars directly.

Or you can just use @UserProfileDir instead as a user may have a different directory than his own user name.

DirCopy (@UserProfileDir & "\Favorites", "c:\documents and settings\%UserName%.jaz\Favorites", 1)

The reason is: When you use @ComSpec to run the command, the command line interpreter (that will be cmd.exe in your case) substitues %UserName% to the username. Only if you tell AutoIt to do the substitution by using Opt('ExpandEnvStrings', 1) AutoIt will substitute %UserName%.

But as stated above, I'd really suggest to use @UserProfileDir (or %USERPROFILE% if you insist to use environment vars) instead as it's more fault tolerant.

Edited by sugi
Link to comment
Share on other sites

Opt('ExpandEnvStrings', 1) does it for me.

When I did a search on Environment Variable this command did not come up, so I missed it. It really needs to show up when you do the Environment Variable search and as a related item for EnvGet(). Environment Variables are very common in batch files so this is a bit of an issue.

Thanks Sugi

An ADVOCATE for AutoIT
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...