🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #13357

Re: GetPartialFileName

#13357
From: Ed Mulroy [TeamB]
Date: Fri, 17 Oct 2003 13:46
71 lines
2685 bytes
I do not think the Windows API provides a function to do that.

See if this does it for you.

-----------------------
char *GetBaseName(char *name, char *base_name)
  {
  if ((name == NULL) || (base_name == NULL))
      return NULL;

  if (name[0] = '\0') // if name is empty
      {
      base_name[0] = '\0';
      return base_name;
      }

  // note: strrchr finds the LAST occurance of a char
  char *slash_ptr = strrchr(name, '/');
  char *dot_ptr = strrchr(name, '.');
  char *bslash_ptr = strrchr(name, '\\');

  char *p = slash_ptr;

  // if there is a '\\' later in the string
  if (bslash_ptr > slash_ptr)
      p = bslash_ptr;

  // if there is no '\\' or '/' in name
  if (!p)
      p = name;

  // if the file name has no period but
  // a dir name in the path has one
  if (dot_ptr < p)
      dot_ptr = NULL;

  // if name does not end in '/' or '\\'
  if (p[1] != '\0')
      {
      int copy_len;

      if (dot_ptr != NULL) // if there is a period
          copy_len = dot_ptr - p - 1;
      else
          copy_len = strlen(p + 1);

      strncpy(base_name, p + 1, copy_len);
      base_name[copy_len] = '\0';
      }
  else // it's just a path with no file name
     {
     base_name[0] = '\0';
     }

  return base_name;
  }
-----------------------

"Stephane Baillargeon" <sbaillargeon@microids.com> wrote in message
news:3f8f7e6f@newsgroups.borland.com...
> Is there a function in the WinAPI that can give me the filename part and
> leave behind the directory?
>
> Ex:
>
> "C:\Path1\Path2\Filename.fil"
> to this
> "Filename.fil"
>
> I know I can parse the string (substr) and hard code it myself but if
> there's already something in the WinAPI, why bother :)
Technical Details
Message-ID: <3f8ff2c8$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <3f8f7e6f@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,5015eee32f4897a5
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-10-17 07:10:35 PST
From: "Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <3f8f7e6f@newsgroups.borland.com>
Subject: Re: GetPartialFileName
Date: Fri, 17 Oct 2003 09:46:30 -0400
Lines: 73
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
NNTP-Posting-Host: 24.74.175.102
Message-ID: <3f8ff2c8$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1066398408 24.74.175.102 (17 Oct 2003 06:46:48 -0700)
X-Authenticated-User: edmulroy
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail