Windows Batch Script: dirname equivalent, get the parent directory of a file








Note: the code below can handle the path contains spaces:
  • Option 1:
    SET filename="C:\Files\A Dir\file1.txt"
    FOR %%F IN (%filename%) DO SET "dirname=%%~dpF"
    ECHO %dirname%
    
  • Option 2:
    SET "filename=C:\Files\A Dir\file1.txt"
    FOR %%F IN ("%filename%") DO SET "dirname=%%~dpF"
    ECHO %dirname%
    



No comments:

Post a Comment