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 :)

6 Years
Discussion Span
Last Post by MasterGberry

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';
        cout << "USERPROFILE not found\n";

Edited by Ancient Dragon: n/a

Votes + Comments
Perfect C++ Example :)

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:


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.

Votes + Comments
Perfect explanation :)
So I used the correct environment variable afterall :)

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 :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.