Solved

Replace string and loop

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

749 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