[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 598
  • Last Modified:

remove last folder from path

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
Ingo Foerster
Asked:
Ingo Foerster
1 Solution
 
jkrCommented:
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
 
jkrCommented:
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
 
Subrat (C++ windows/Linux)Software EngineerCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jkrCommented:
Don't you think that deleting an entire directory is a bit overdoing it when it just should be removed from a string? ;o)
1
 
sarabandeCommented:
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
1
 
Ingo FoersterAuthor Commented:
This works very well for me. Many thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now