-4

I'm writing code on C++ that will map to the nearest available drive using "net use * \server...etc" and afterwards I want to open a file on it.

Is there a command that will let me interact with this new drive without needing to know on which drive it was mapped beforehand? (Usually it will be drive Z:, but not always!)

Clearer example:

 system("net use * \\server\folder\ p455word /user:server\user /p:no")

(output: "Server succesfully mapped to drive Z:")

Then I would normally try to open the file:

system("Z:\\folder\mydoc.docx")

My question is on how to do this:

system("*\\\folder\mydoc.docx")

With * being whatever the drive the computer chose to map to, or if there's a workaround to this. Thanks!

J. Doe
  • 1
  • 2
  • 1
    If you can't identify it then you can't use it, it's that simple. But surely the program, with some slight modification, must able to remember which drive letter it assigned? Or it can use an drive-letter-free UNC path. – Cheers and hth. - Alf Jul 04 '16 at 15:57
  • 1
    What's with the downvotes and the smug reply? I've looked this up and can't find the answer. "If you have nothing good to say..." – J. Doe Jul 04 '16 at 15:59
  • 1
    Loads more detail needed, like; what have you researched? What have you tried so far? What were the results? What does your current (minimal, complete example) code look like? I can understand the downvotes - this is a low quality question that needs to do a lot more to show the effort that has been undertaken already to solve the problem. – Jesper Juhl Jul 04 '16 at 16:03
  • You can read the "How to ask" page, to format your questions better – meJustAndrew Jul 04 '16 at 16:05
  • I didn't downvote and my helpful comment wasn't intended to be smug. Maybe it's impossible to answer this without you feeling that it's smug. Do you want us to apologize about providing solutions? – Cheers and hth. - Alf Jul 04 '16 at 16:06
  • 1
    Alf: My comment was before you edited your post and added your drive-letter-free solution. I appreciate your contribution :) – J. Doe Jul 04 '16 at 16:12

1 Answers1

0

If you're looking for some net use-specific magic here, you're going to be disappointed. Your program simply passes a text string to the Windows shell, and has no knowledge or information about the command you've performed. It certainly can't pull out status information about the result of that call, beyond reading the command's text output.

Fortunately, the text output is all you need — your quote shows that the assigned drive letter is right there. You just need to pull it out, then construct the subsequent system calls dynamically by building up a string from the now-known drive letter, and the fixed part of your path.

system is not really designed for any of this, though. Here's how to execute a command and retrieve its output. Extracting the drive letter from the string "Server succesfully mapped to drive Z:" would also be a separate question. In general, when you have a problem, break it down into component parts; then you'll be able to "look it up" with success.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Thanks for your suggestion! However, given how insanely complicated it turned out to be, I found a workaround as suggested in the comments. If anyone has this same problem, you can use its UNC path "\\server\folder" to connect to it without prompting for password regardless of where the drive was mapped. i.e: net use * \\kratos\folder passw0rd /user:kratos\zeus /p:no can then be accesed simply as: explorer \\kratos\folder Regardless if it was mapped on Q: A: or Z: :) – J. Doe Jul 04 '16 at 17:47
  • @J.Doe: That has nothing to do with C++ and is thus out of scope of the question. – Lightness Races in Orbit Jul 04 '16 at 18:31