Solved

remove last folder from path

Posted on 2014-03-01
6
451 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
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.

 
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 32

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

705 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

20 Experts available now in Live!

Get 1:1 Help Now