select a file

closed account (28poGNh0)
Hiiiii everyone

I am asking if there is a way to select a file

thanks a lot
closed account (28poGNh0)
bump

I found a couple of results in searching but not exacly what I want ,maybe this will help

when we click the mouse button on a file ,its icon is been selected ,I am asking how to do that with c++ like select("c:/textFile.txt");

PS : a lot of programs do that ,like when the file's conversion finished it suggest you to open the folder containing the file and is select it

sorry for the bad explanaition
thanks a lot
You mean select as in... highlight? Like when you single-click on a file in explorer?

I'm sure there's a way to do this, as I'm pretty sure I've seen installers do something like this (they open an explorer window and highlight the file you've just installed) -- although maybe my memory is bad and I'm just imagining that....

On a hunch I searched for Explorer commandline options thinking you could just fire up Explorer with some params -- and it looks like that would work:

https://support.microsoft.com/kb/130510?wa=wsignin1.0 (You need an MS live account to view this page)

If you can't load the page, here's the juicy bits. The example in bold is probably what you're most interested in:


Syntax
EXPLORER.EXE [/n][/e][,/root,<object>][[,/select],<sub object>]
Switches

/n: Opens a new window in single-paned (My Computer) view for each item
selected, even if the new window duplicates a window that is
already open.

/e: Uses Windows Explorer view. Windows Explorer view is most similar
to File Manager in Windows version 3.x. Note that the default view
is Open view.

/root,<object>: Specifies the root level of the specified view. The
default is to use the normal namespace root (the
desktop). Whatever is specified is the root for the
display.

/select,<sub object>: Specifies the folder to receive the initial
focus. If "/select" is used, the parent folder
is opened and the specified object is selected.


Examples

To open a Windows Explorer view to explore only objects on \\<server name>, use the following syntax:
explorer /e,/root,\\<server name>
To view the C:\WINDOWS folder and select CALC.EXE, use the following syntax:
explorer /select,c:\windows\calc.exe



Sure enough, if I type in explorer /select,c:\Windows\System32\calc.exe at my command prompt, it opens up a new explorer window and highlights the appropriate file.


In your program, the easiest way to do this is probably with a system() call:

 
system( "explorer /select,c:\\Windows\\System32\\calc.exe" );


Although you might want to put the path in quotes, so it can have spaces:

 
system( "explorer /select,\"c:\\Windows\\System32\\calc.exe\"" );


Just replace the path to calc with the path to whatever you want. And remember to use escaped backslashes (\\) for path separators.


EDIT:

Just tested and it works with system.
Last edited on
closed account (28poGNh0)
Thanks Mr @Disch . Thats exactly what I am looking for ,you're amazing.

one problem though ,in this epic thread :http://www.cplusplus.com/forum/beginner/1988/ Mr @Duoas advise not using system("anything") any alterative solution ?

thank again you helped alot
Duoas in that thread wrote:
There's nothing wrong with using system() when used properly.


This is a case of using it properly. I would not worry about it.

The reason system is bad is because there is excessive overhead because it launches a new process -- and it's a security risk because the process you're launching may not be the one you think it is. However both of those reasons do not really apply here because:

1) Our goal is to launch a new process, so the overhead is unavoidable.
2) 'explorer' is a foundational cornerstone of Windows, and if someone's machine is screwed up enough to where launching Explorer launches some other malicious program, then they have much, much bigger problems than your program.


Any alternative solution will have the same 2 'gotchas'. If you really don't want to use system, then the next thing to look at would probably be CreateProcess:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx


But this is significantly more work for little/no gain. I didn't even want to do an example because I didn't want to have to deal with it.




EDIT: I thought it went without saying, but this solution will only work for Windows and is very, very non-portable.
Last edited on
closed account (28poGNh0)
A lot of heavy information

Thanks a lot again

and hoping for new ideas
If you want to do it without the call to system() (since you're playing with Windows either way), here's the Windows way to do it. (Via an example program.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
using namespace std;

#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>

int main()
{
  // This is not the preferred method, but it will work.
  // (You should preferably use the SHGetFolderPath() function instead.)
  string mydocuments = string( getenv("HOMEDRIVE") ) + getenv("HOMEPATH" ) + "\\Documents";
  
  // This is where the filename your user selects will go.
  // Make sure it is long enough. (Especially if you start playing with the OFN_ALLOWMULTISELECT flag.)
  string filename( MAX_PATH, '\0' );

  // This structure tells Windows how you want the Open File Dialog to appear.
  OPENFILENAME ofn;
  ZeroMemory( &ofn, sizeof( OPENFILENAME ) );
  ofn.lStructSize = sizeof( OPENFILENAME );
  
  // We'll add filters for text files (*.txt) and any file (*.*).
  // If you don't want any filters, just comment out this line (== set it to NULL).
  ofn.lpstrFilter = "Text Files\0*.TXT\0Any file\0*.*\0";  
  
  // Where to put the filename the user selects
  ofn.lpstrFile = (char*)filename.c_str();
  ofn.nMaxFile  = filename.size();
  
  // Folder to start in
  ofn.lpstrInitialDir = mydocuments.c_str();
  
  // Title of the dialog
  ofn.lpstrTitle = "Demo -- Choose any file -- nothing will happen.";

  // There are many more options, but right now we just want the user to 
  // select a file that actually exists.
  ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
  
  // Show the dialog!
  if (GetOpenFileName( &ofn ))
  {
    // Success! Yaaaay!
    filename.resize( filename.find( '\0' ) );
    cout << "You chose the file: " << filename << "\n";
  }
  else 
  {
    // Bummer. Failure, dude.
    cout << "You chose to cancel the file operation.\n";
  }
}

When you compile, don't forget to link with the comdlg32 library.

Note that these days Microsoft wants you to play with the Common Item Dialog interface, but that's getting into junk you probably don't care about. And this function API won't go away anytime soon.

Hope this helps.
closed account (28poGNh0)
:) you dont stop amazing me with your knowledg, Thanks a lot @Duoas

As a matter of fact I wrote your code in one of your old answers ,and I really learn a lot from It. But what I am looking for, is the same effect as this line does :
system( "explorer /select,c:\\Windows\\System32\\calc.exe" );
only using one of cpp code if exist

Again thanks a lot ,any new info will help
Topic archived. No new replies allowed.