Author Topic: Get path without filename  (Read 182 times)

rc

  • Regular Member
  • *
  • Posts: 16
Get path without filename
« on: November 01, 2019, 07:07:46 AM »
Hello again,

i know this is probably an easy task for most of you here:

i want to cut off a file name from a path and concatenate a new file name to it.

Suppose i have a buffer containing a path: "c:/dev/test/task.asm" and i want to cut the file name off so that i just have the path. How am i suppose to do that?
After that i want to concatenate the path and a new file name (thats no problem):
Code: [Select]
mov pbuf, cat$(pbuf, pathBuf, "task2.asm")

But before i can concatene the new file name, i have to cut off the old file name first.
That file name however can vary in length, so i dont know the exact number of bytes to cut off.

As a test i have written the following code:
Code: [Select]
invoke SendMessage,hWin,WM_GETTEXTLENGTH,0,0
mov tl, eax
inc tl              ; 1 extra for zero terminator

invoke GetWindowText,hWin,ADDR buffer1,tl
invoke lstrcmp,ADDR buffer1,ADDR Untitled

.if eax == 0 ; eax is zero means strings are equal       
    fn MsgboxI,hWin,"Please save file first.!","AsmPad",MB_OK,500
.else
    fn MsgboxI,hWin,ADDR buffer1,"AsmPad",MB_OK,500
.endif

This just gets the window title which contains the current path to the saved file.
As a test, it launches a msgbox with the output of the current path or when untitled, a message that sais the user has to save the file first.

Here i want to cut off the filename from buffer1 and concatinate the new filename.

Vortex

  • Member
  • *****
  • Posts: 2030
Re: Get path without filename
« Reply #1 on: November 01, 2019, 07:39:00 AM »
Hello rc,

The library masm32.lib is offering the following path functions :

Code: [Select]
GetAppPath
NameFromPath
GetPathOnly

You can check \masm32\help\masmlib.chm for more information.

rc

  • Regular Member
  • *
  • Posts: 16
Re: Get path without filename
« Reply #2 on: November 01, 2019, 07:45:39 AM »
So quick. This forum is awesome! Thank you. Works like a charm. :)
Really have to dig deeper into the help files!

For others, here is the working code:

Code: [Select]
invoke SendMessage,hWin,WM_GETTEXTLENGTH,0,0
mov tl, eax
inc tl              ; 1 extra for zero terminator

invoke GetWindowText,hWin,ADDR buffer1,tl
invoke lstrcmp,ADDR buffer1,ADDR Untitled

.if eax == 0 ; eax is zero means strings are equal       
    fn MsgboxI,hWin,"Please save file first.!","AsmPad",MB_OK,500
.else
    fn GetPathOnly, ADDR buffer1, ADDR buffer1
    fn MsgboxI,hWin,ADDR buffer1,"AsmPad",MB_OK,500
.endif


TimoVJL

  • Member
  • ***
  • Posts: 476
Re: Get path without filename
« Reply #3 on: November 01, 2019, 08:08:54 AM »
WinAPI Shlwapi PathFindFileName gives a pointer to filename part
May the source be with you

rc

  • Regular Member
  • *
  • Posts: 16
Re: Get path without filename
« Reply #4 on: November 01, 2019, 08:57:57 AM »
Short sidequestion: do i clear a buffer?
Lets say i have a temporary buffer: LOCAL tmpBuf[260]:BYTE
that i want to reuse? Then i have to clear it of course before reusing it.

TimoVJL

  • Member
  • ***
  • Posts: 476
Re: Get path without filename
« Reply #5 on: November 01, 2019, 09:08:33 AM »
if you just copy a new content to it, no need to clear it first ?
zero first byte, if you add something to end of last string.
May the source be with you

rc

  • Regular Member
  • *
  • Posts: 16
Re: Get path without filename
« Reply #6 on: November 01, 2019, 09:42:05 AM »
Oh ok. Thank you.