windows - split path and take last folder name in batch script

Windows - split path and take last folder name in batch script

You can split a path and extract the last folder name in a Windows batch script using a combination of string manipulation and loop. Here's an example:

@echo off setlocal enabledelayedexpansion set "fullPath=C:\Users\YourUsername\Documents\ExampleFolder" REM Replace backslashes with a unique character (in this case, a pipe "|") set "pathWithPipe=!fullPath:\=|!" REM Split the path using the pipe as a delimiter for /f "tokens=*" %%a in ("!pathWithPipe!") do set "lastFolder=%%~a" REM Replace the pipe back with a backslash set "lastFolder=!lastFolder:|=!" echo Last folder in the path: %lastFolder% endlocal 

Replace C:\Users\YourUsername\Documents\ExampleFolder with your actual path. This script first replaces backslashes with a pipe character, then uses a loop to extract the last segment, and finally replaces the pipe character back with a backslash.

Examples

  1. "Batch script to get the last folder name from a path"

    @echo off set "path=C:\Users\Username\Documents\FolderName" for %%i in (%path%) do set "lastFolder=%%~nxi" echo %lastFolder% 

    Description: Uses a for loop to iterate over the path components and extracts the last folder name using %%~nxi.

  2. "Batch script to split path and extract the last folder"

    @echo off set "path=C:\Users\Username\Documents\FolderName" for %%i in (%path%) do set "lastFolder=%%~ni" echo %lastFolder% 

    Description: Similar to the previous example but uses %%~ni to extract the last folder name without the extension.

  3. "Batch script to split path and get the last folder without full path"

    @echo off set "path=C:\Users\Username\Documents\FolderName" for %%i in (%path%) do set "lastFolder=%%~nxi" echo %lastFolder:*\=% 

    Description: Removes the full path and extracts only the last folder name using string manipulation.

  4. "Batch script to split UNC path and get the last folder"

    @echo off set "path=\\Server\Share\FolderName" for %%i in (%path%) do set "lastFolder=%%~nxi" echo %lastFolder% 

    Description: Works with UNC paths (network paths) and extracts the last folder name.

  5. "Batch script to split path and get the parent folder name"

    @echo off set "path=C:\Users\Username\Documents\FolderName" for %%i in (%path%) do set "parentFolder=%%~dpi" echo %parentFolder:~0,-1% 

    Description: Extracts the parent folder path using %%~dpi and removes the trailing backslash.

  6. "Batch script to split path and get the second-to-last folder name"

    @echo off set "path=C:\Users\Username\Documents\FolderName" for %%i in (%path%) do set "secondToLastFolder=%%~nxi" set "path=%path:*\=%" for %%i in (%path%) do set "secondToLastFolder=%%~nxi" echo %secondToLastFolder% 

    Description: Extracts the second-to-last folder name by first removing the last folder from the path.

  7. "Batch script to split path and get the last two folder names"

    @echo off set "path=C:\Users\Username\Documents\FolderName" for %%i in (%path%) do set "lastTwoFolders=%%~nxi" set "path=%path:*\=%" for %%i in (%path%) do set "lastTwoFolders=%%~nxi\%lastTwoFolders%" echo %lastTwoFolders% 

    Description: Extracts the last two folder names by successively removing folders from the path.

  8. "Batch script to split path and get the last folder name with spaces"

    @echo off set "path=C:\Program Files\Folder Name" for %%i in (%path%) do set "lastFolder=%%~nxi" echo %lastFolder% 

    Description: Handles paths with spaces correctly and extracts the last folder name.

  9. "Batch script to split path and handle paths with parentheses"

    @echo off set "path=C:\Users\Username\Documents\Folder (Name)" for %%i in (%path%) do set "lastFolder=%%~nxi" echo %lastFolder% 

    Description: Addresses paths with parentheses and extracts the last folder name correctly.

  10. "Batch script to split path and get the last folder name with special characters"

    @echo off set "path=C:\Users\Username\Documents\Folder!@#$%^&*()-_+=Name" for %%i in (%path%) do set "lastFolder=%%~nxi" echo %lastFolder% 

    Description: Handles paths with special characters and extracts the last folder name appropriately.


More Tags

codec client-server imap poison-queue vb.net-to-c# keycloak angular2-template gradle-dependencies load-data-infile ear

More Programming Questions

More General chemistry Calculators

More Chemical reactions Calculators

More Mortgage and Real Estate Calculators

More Tax and Salary Calculators