Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Switching to network directories 1

Status
Not open for further replies.

TalosBlack

Programmer
Jan 6, 2004
33
US
I am trying to use the _chdir() command inorder to jump to a network directory and investigate that directories file tree. I have the program querry the user for the destination drive. The program outputs an error for using the _chdir command (not a compilation error just the command fails and i see the error through error checking within the program). However, i'm almost certain that this command works because if i compile the program on network folder and let it run the it works properly. (The program jumps through the file tree telling me all folders and files in the tree. It does this making use of the changedir when it comes across new folders.) I also displayed the path of the folders it jumps into and try to enter the path how it was displayed. This still gave me an error. I pressume there is some hidden character or something that is being recognized by the computer that isn't being shown on the screen. Any help in this area would be great thanks.

John
 
The first thing I'd suggest is the good old "\" problem may be in evidence:
Code:
   // wrong
   char* pszDir1 = "adir\bdir\ndir";
   // right
   char* pszDir2 = "adir\\bdir\\ndir";
   ...
   // won't work:
   _chdir(pszDir1);
   // *may* work:
   _chdir(pszDir2);
This may be the cause but without seeing an example of your code, I can't be sure. I've seen this too many times not to suggest it first.

Hope this helps.


[tt]________________________________________________________________
[pc2]Roger
Life is a game of cards in which the deck contains only jokers.[/tt]
 
Yeah i have it so i am qued to enter the post and used the \\ first and then tried singal slashes. In fact since i was trying to go to a network drive i used \\\\ instead of the normal \\ preceding the drive and then \\ for the rest of the exstension. ex "\\\\remote\\new folder" instead of "\\remote\new folder".


//queries user on initial to be searched
cout<<&quot;Please enter your initial path. Enter \\\\ where a \\ is necessary:&quot;<<endl;

cin>>initialpath;
cout<<endl;

//changes working directory to that entered
_chdir(initialpath);

That that _chdir() is the one giving me problems. If i'm already on a network drive later _chdir() works. For examlpe:


//trying to save path wo file
char pathname[MAX_PATH];
strcpy(pathname, pathandfile);
strcat(pathandfile,&quot;\\&quot;);
strcat(pathandfile,c_file.name);
cout<<pathandfile<<endl;

if(c_file.attrib & _A_SUBDIR)
{
if(_chdir(pathandfile)==0)//this chdirworks
{ Search(); //recursive function
_chdir(&quot;..&quot;); //pops to time when done
}

If I output the path here it will be for example &quot;\\remote\new folder&quot; even though it won't let me input it. The program allows itself to say go to &quot;\\remote\new folder\next folder&quot; but any directory i try to intially enter that is of this type fails. I am able to go to say &quot;C:\New Folder\Whatever&quot; and the program goes with out a hitch. Also if i were to have a link to a network drive my program will figure out the target and then jump to the drive i just dont' seem to be able to input it at square one. Any further help would be great.

Thanks

John
 
I didn't realise you were using UNC names ([tt]\\computer-name\dir[/tt]); I though you were using mapped drives ([tt]X:\computer-name[/tt]).

I strongly suspect that the [tt]_chdir()[/tt] function doesn't work with UNC names (well, obviously it doesn't; as you've found). I suggest you use the Windowstm API function [tt]SetCurrentDirectory()[/tt] instead.

[tt]________________________________________________________________
[pc2]Roger
Life is a game of cards in which the deck contains only jokers.[/tt]
 
I tried the SetCurrentDirectory() function. At first i seemed to be having the same problem but after going to MSDN.com i realized that the function didn't need me to but in double slashes and it required a final \ at the end of the statement the function worked well. Thanks a lot for your help.

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top