1

I would like to do this in C programming without using string manipulation especially since the path contains a mix of forward and backward slashes.

path = "C:\Program Files\PTC/Creo 4.0/M040/Parametric/bin/parametric.exe";
var searchDir = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(path)));

The path is char array.

char InstallExe[MAX_PATH_LENGTH].

The output I am expecting is

searchDir = "C:\Program Files\PTC/Creo 4.0/M040"
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Deep
  • 21
  • 6
  • Are you looking for a C standard library function or a function in the Windows API? – Andreas Rejbrand Aug 21 '18 at 21:25
  • Any of them should work for me. I am working on a C application which is on Windows platform. – Deep Aug 21 '18 at 21:29
  • I tried Google and found this almost at the top: https://stackoverflow.com/questions/5392735/parent-directory-of-a-file?rq=1 Does that answer help? – Andreas Rejbrand Aug 21 '18 at 21:32
  • 2
    Since you've tagged `winapi` you can use `PathRemoveFileSpec` from `shlwapi.dll`. – Jonathan Potter Aug 21 '18 at 21:32
  • the standard APIs are `dirname` and `pathname`. Have you tried them on Windows? – bruceg Aug 21 '18 at 22:04
  • @JonathanPotter Unfortunately, `PathRemoveFileSpec` doesn't handle foreslashes. `"C:\\abc/def/ghi"` becomes `"C:\\abc"` – zett42 Aug 21 '18 at 22:41
  • 1
    what is wrong with a simple search for the last `/` or ` \ ` ? – Serge Aug 22 '18 at 02:31
  • 1
    also win32 name can containing `/../` for example - need first normalize name by `GetFullPathName` and than call `PathRemoveFileSpec` or yourself remove last \ which is aloso simply – RbMm Aug 22 '18 at 09:10
  • @RbMm Thank you for your help. I tried below code `char SearchFolder[MAX_PATH_LENGTH]; GetFullPathNameA(path, dwDataSize, SearchFolder, &string); PathRemoveFileSpecA(SearchFolder);` Output of GetFullPathNameA statement is `D:\Builds\OSApp\x64\Debug\"C:\Program Files\PTC\Creo 4.0\M040\Parametric\bin\parametric.exe"` Output of PathRemoveFileSpecA is `D:\Builds\OSApp\x64\Debug\"C:\Program Files\PTC\Creo 4.0\M040\Parametric\bin` What I want is `C:\Program Files\PTC\Creo 4.0\M040\Parametric\bin` How do I strip the initial path of the calling application? – Deep Aug 22 '18 at 20:02
  • you pass wrong file path to `GetFullPathName` - it must not containing ". use for example `GetModuleFileNameW`. and stop use A api. use only W – RbMm Aug 22 '18 at 20:34

0 Answers0