Solved

remove last folder from path

Posted on 2014-03-01
6
464 Views
Last Modified: 2014-03-13
Hello,
I have a Cstring with file path information. As sample: C:\ProrgamData\MyFolder\Logs\
How can I remove the last folder "Logs" from this path?
0
Comment
Question by:Ingo Foerster
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39897888
Windows has the Shell Helper API that can help you with that, All you need to do is calling 'PathAppend()' () and add a ".." to go up one direectory, e.g. like

#include <windows.h>
#include <iostream>
#include "Shlwapi.h"
#include <tchar.h>

#pragma comment(lib,"shlwapi.lib")

using namespace std;

void main( void )
{

  TCHAR acOrig[MAX_PATH] = _T("C:\\ProrgamData\\MyFolder\\Logs\\");

  cout << acOrig << endl;

  PathAppend(acOrig,_T("..")); // go up one directory

  cout << acOrig << endl;
}

  

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 39897890
BTW, which version is Visua C++ are you using? That's because the handling of CStrings differs slightly between them, so in order to show you how to use them with the above, I have to ask...
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 39897970
You can use SHFileOperation() to remove a directory. Search msdn for it's supported OS.

code:

// @param: dir - Fully qualified name of the directory being   deleted,   without trailing //backslash

int silently_remove_directory(LPCTSTR dir) {
 
int len = strlen(dir) + 2; // required to set 2 nulls at end of argument to SHFileOperation.
  char* tempdir = (char*) malloc(len);
  memset(tempdir,0,len);
  strcpy(tempdir,dir);

  SHFILEOPSTRUCT file_op = {
    NULL,
    FO_DELETE,
    tempdir,
    "",
    FOF_NOCONFIRMATION |
    FOF_NOERRORUI |
    FOF_SILENT,
    false,
    0,
    "" };
  int ret = SHFileOperation(&file_op);
  free(tempdir);
  return ret; // returns 0 on success, non zero on failure.
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:jkr
ID: 39897981
Don't you think that deleting an entire directory is a bit overdoing it when it just should be removed from a string? ;o)
0
 
LVL 33

Accepted Solution

by:
sarabande earned 285 total points
ID: 39900330
you could do it by

CString strPath = _T("C:\\ProgramData\\MyFolder\\Logs\\");
strPath.Trim();
int len = strPath.GetLength();
int pos;

while ((pos = strPath.ReverseFind(_T('\\'))) >= 0)
{
     strPath = strPath.Left(pos);
     // we skip one trailing backslash
     if (pos == len-1)
     {
           continue;
     }
     break;
}

Open in new window


note, the resulting path hasn't a backslash at end.

Sara
0
 

Author Closing Comment

by:Ingo Foerster
ID: 39926006
This works very well for me. Many thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

863 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

23 Experts available now in Live!

Get 1:1 Help Now