split path and take last folder name in batch script
batch file get folder name from path
how to get folder name in cmd
batch file path manipulation
batch file search for folder name
batch file find path
batch split string by slash
batch file loop through folders
I want to split the string (having a path) with
\and take last folder name in a variable. Please help.
I want FOLDER3 in a variable.
I tried with the command below which is working if the the last character is not
for %f in (C:\FOLDER1\FOLDER2\FOLDER3) do set myfolder=%~nxf
It is not working if the last character is
Also it is not working if variable is used like : for %f in (%mypath%) do set myfolder=%~nxf
@echo off set MYDIR=C:\FOLDER1\FOLDER2\FOLDER3\ set MYDIR1=%MYDIR:~0,-1% for %%f in (%MYDIR1%) do set myfolder=%%~nxf echo %myfolder%
Windows batch file: get last folder name from path, JosefZ explains the obvious problems with your code, but he failed to point out a subtle problem, though his code fixed it: FOR /R (as well as the The trick is that we take the desired path, back up one level to create a folder mask for substitution and then replace the folder mask with nothing. To test it, simple copy and paste into a command script (.cmd) in any directory, then run it.
for %f in (C:\FOLDER1\FOLDER2\FOLDER3\.) do set myfolder=%~nxf
for %f in (C:\FOLDER1\FOLDER2\FOLDER3.) do set myfolder=%~nxf
How do I get the last part of directory from a command line, %~nA will output the file name, %~xA will output the file extension. count the directory separators and split your path/file string based on that number. If you want to perform the same within a batch script, you need to Split directory name & filename from a string #1 Post by santoshsri » 29 Jan 2009 18:56 Wondering if there is any way to split directory name & filename from a string that contains a complete path of file including filename.
When your current folder contains spaces then try this:
@echo off for %%f in ("%CD%") do set LastPartOfFolder=%%~nxf echo %LastPartOfFolder%
Get current folder name by a DOS command?, Shortest way I have found: for %I in (.) do echo %~nxI. or within a .bat script: for %%I in (.) do echo %%~nxI. or in .bat with Get value in variable. for %%I in (.) Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more split path and take last folder name in batch script
Windows batch file: get last folder name from path, Windows batch file: get last folder name from path - batch-file. than at parse time [and CLI parses all the (command block in parentheses) at once] Next script Because this path is split to the last item, also known as the leaf, the command displays only the file names. The Resolve parameter tells Split-Path to display the items that the split path references, instead of displaying the split path. Like all Split-Path commands, this command returns strings.
🤬 split path and take last folder name in batch script, @echo off set MYDIR=C:\FOLDER1\FOLDER2\FOLDER3\ set MYDIR1=%MYDIR:~0,-1% for %%f in (%MYDIR1%) do set myfolder=%%~nxf echo %myfolder%. Reporting: Batch renaming file in a folder with the folder name This post has been flagged and will be reviewed by our staff. Thank you for helping us maintain CNET's great community.
Split-Path, It can also get items that are referenced by the split path and tell whether the path specified part of a path, such as the parent folder, a subfolder, or a file name. this path is split to the last item, also known as the leaf, the command displays Given a variable %A, containing a path and file: %~nA will output the file name, %~xA will output the file extension. The following example uses the pipe character | as a delimiter. The pipe is an invalid character for files and paths and should not appear in a path. This will allow for spaces in paths and filenames.
%MYDIR:~0,-1%appears to be some sort of substring method, but can someone explain what is happening in the for-loop ?
- there is the last char missing in myfolder! so it actually doesn't work. input folder: ..\blalalalal\Control-Freak returns: Control-Frea
- It looks like it is becuase you didn't have a trailing slash at the end. If you append one, the above will work.
- Alternatively you could take away the line
set MYDIR1=%MYDIR1:~0,-1%which actually does delete the last character. To make it safe you can check if there is a trailing slash and if so delete the last char and if not let the string as it is :)
- When there are spaces in the path above method doesn't work. For example if the path is set MYDIR=C:\FOLDER1\FOLDER2\FOLDER 3\ it will return 3 instead of FOLDER 3