I am trying to write a simple Batch File to launch an exe using some basic CMD PRMPT functions. I know exactly what I need to do, but my only problem is I don't know how to detect the Desktop Folder? What if its in Documents and Settings, or if they have a newer computer in Users? Ty for any help :)

Recommended Answers

All 3 Replies

USERPROFILE environment variable works for me with Windows 7

#include <iostream>
using std::cout;
using std::cin;

int main()
{
    char* p = getenv("USERPROFILE");
    if( p )
        cout << p << '\n';
    else
        cout << "USERPROFILE not found\n";
    cin.get();
}
commented: Perfect C++ Example :) +0

I'm not sure you've posted in the correct forum, I think you want the shell scripting forum!

Anyway, I think what you're after is this:

%USERPROFILE%\Desktop

So if you wanted to navigate into the current users Desktop folder in your .bat file, then you would use:

cd %USERPROFILE%\Desktop

Likewise, if you want to pass the path to the Desktop folder as a parameter to the executable you're calling, then in your batch file you could use:

program.exe %USERPROFILE%\Desktop

Or if the program you're calling requires a switch to be specified before the path, then you'll need to do something like this:

program.exe -X %USERPROFILE%\Desktop

where -X is whatever switch you might need to specify before passing the path.

commented: So I used the correct environment variable afterall :) +35
commented: Perfect explanation :) +0

Ty both for your answers :) Going to use Jason's since it is a batch file, but I will keep yours in mind for future needing AD :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.