[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Replace string and loop

Posted on 2004-09-30
3
Medium Priority
?
462 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
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

834 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