Hacker News new | ask | show | jobs
by Ciantic 81 days ago
Let me present you my favorite, how do you figure out dirname, basename and filename in batch script?

    set filepath="C:\some path\having spaces.txt"

    for /F "delims=" %%i in (%filepath%) do set dirname="%%~dpi" 
    for /F "delims=" %%i in (%filepath%) do set filename="%%~nxi"
    for /F "delims=" %%i in (%filepath%) do set basename="%%~ni"

    echo %dirname%
    echo %filename%
    echo %basename%
It is just as intuitive as one would expect.
1 comments

  $file = Get-ChildItem "C:\some path\having spaces.txt"

  Write-Output $file.DirectoryName
  Write-Output $file.Name
  Write-Output $file.BaseName
Or if that's still to verbose:

  $file = gci "C:\some path\having spaces.txt"

  echo $file.DirectoryName
  echo $file.Name
  echo $file.BaseName

People should really get over their aversion against powershell.
You could just one line it too:

  Get-Item "C:\some path\having spaces.txt" | select DirectoryName, Name, Basename