Solved

Replace string and loop

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now