?
Solved

Replace string and loop

Posted on 2004-09-30
3
Medium Priority
?
454 Views
Last Modified: 2008-03-10
Hi,

I have this piece of code...
What I want to do is to make it loop, and on the second run change strcat(szPath, "\\\\folder1"); into       strcat(szPath, "\\\\anotherfolder"); and on the third run change to strcat(szPath, "\\\\yetanotherfolder"); and so on... Any idea how to go about doing it? Maybe a loop or something?

      // Get current user's favorite folder
      TCHAR szPath[MAX_PATH];

      SHGetSpecialFolderPath(NULL, szPath, CSIDL_FAVORITES, FALSE);

  // THIS IS THE LINE I WANT TO CHANGE
      strcat(szPath, "\\\\folder1");

      SHFILEOPSTRUCT os;
     os.hwnd = NULL;
     os.pFrom = szPath;
     os.pTo = NULL;
     os.wFunc = FO_DELETE;
     os.fFlags =     FOF_NOCONFIRMATION | FOF_NOERRORUI  | FOF_SILENT;
      
     int x = SHFileOperation(&os);
0
Comment
Question by:wiredx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 12188112
PCHAR folders[] = {_T("\\\\folder1"),_T("\\\\yetanotherfolder");
for (int i = 0;i < sizeof(folders)/sizeof(PCHAR);i++) {
    // ...
    strcat(szPath, folders[i]);
    // ...
}
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12188120
> PCHAR folders[] = {_T("\\\\folder1"),_T("\\\\yetanotherfolder");

PCHAR folders[] = {_T("\\\\folder1"),_T("\\\\yetanotherfolder")};
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 12189269
Assumming you know all path names (is that true?), I recommend you to maintain szPath untouched, use an array with rest of path, and append it in every loop.

PCHAR folders[] = {
       "\\\\folder1",
       "\\\\folder1\\yetanotherfolder",
       "\\\\folder2",
       "\\\\folder2\\anothermore"
       "\\\\folder2",
       // etcetera
};

// obtain favorites folder
TCHAR szPath[MAX_PATH];
SHGetSpecialFolderPath(NULL, szPath, CSIDL_FAVORITES, FALSE);

//Then use a secondary buffer to make your full path:
TCHAR szFullPath[MAX_PATH];

//Then mix all in the secondary buffer:
for (int i = 0;i < sizeof(folders)/sizeof(PCHAR);i++) {
    strcpy(szFullPath, szPath);
    strcat(szFullPath, folders[i]);
    // Your folder operations here
}


0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question