Solved

Replace string and loop

Posted on 2004-09-30
3
431 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 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Issue accessing member variable of atomic class 16 116
convert char array to number in c 5 85
passing data from one form to another form in c++ 27 79
Precision Problem in C++ 7 31
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…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

776 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