On a WIndows10 64-bit with a normal batch file I am trying to extract the current directory name (not the full path) using the following line nested inside a larger loop recursing directories (Full file below):
for %%f in (!currentDirectoryNoTrailingSlash!) do set currentDirectoryName=%%~nxf
It works fine unless there is a space in the current directory name???
Good:
currentDirectory "C:\TEMP\Test1\"
currentDirectoryNoTrailingSlash "C:\TEMP\Test1"
currentdirectoryname "Test1"
Fail:
currentDirectory "C:\TEMP\Test 2\"
currentDirectoryNoTrailingSlash "C:\TEMP\Test 2"
currentdirectoryname "2"
The whole file is below. Any guidance welcome.
Thanks
Audo
Whole test file:
-------------------------------------------------------------------------------------------------------
Code: Select all
@echo off
cls
setlocal enabledelayedexpansion enableextensions
REM Set the root path where files end up
set destinationPathRoot=C:\Temp\New
REM Get all the files in the directory and sub-directories
for /f "tokens=* delims=|" %%G in ('dir /s /a-d /b *.*') do if not %%~xG==.bat (
REM Set the source file path
set sourceFilePath=%%G
REM Get the current directory and remove the trialing slash
set currentDirectory=%%~dpG
set currentDirectoryNoTrailingSlash=!currentDirectory:~0,-1!
echo currentDirectory "!currentDirectory!"
echo currentDirectoryNoTrailingSlash "!currentDirectoryNoTrailingSlash!"
REM Get only the name of the current directory
for %%f in (!currentDirectoryNoTrailingSlash!) do set currentDirectoryName=%%~nxf
REM Create the path to the destantion path based on the current directory name
set destinationPath=!destinationPathRoot!\!currentDirectoryName!
echo currentdirectoryname "!currentDirectoryName!"
REM if the destination doesn't exist, create it
if not exist !destinationPath! (
REM mkdir !destinationPath!
)
REM Set the destination file based on the destination path and the current file name
set destinationFilePath=!destinationPath!\%%~nG.mp4
copy "!sourceFilePath!" "!destinationFilePath!"
)
)
endlocal