However I need to be able to also split the input variable into parts: one variable for the filename portion, one for the directory, etc.
I've found that the test case batch code below works for simple filenames but when characters like single quotes, exclamation marks, and such are contained in the input variable the for loop (taken from this Stack Overflow page) fails to output the string properly.
Troublesome filenames (the last example features a Unicode colon not a regular one):
Code: Select all
! !§$%&()=`´'_;,.-#+´^ßöäüÖÄÜ°^^#
%!test2!
Test 'quotes'
test3 ꞉ blah
Test case batch:
Code: Select all
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "input=!cmdcmdline:~0,-1!"
set "input=!input:*" =!"
for %%a in ("!input!") do (
set "folder=%%~dpa"
set "name=%%~na"
)
echo !input!
echo !folder!
echo !name!
set /p prompt=
endlocal
Output of !name! variable above using the example filenames above (in order):
Code: Select all
.-#+'^ßöäüÖÄÜ°^^#
%
'quotes'
blah
The only reason I need to use the cmdcmdline variable in the first place is to handle the ampersand with no spaces path limitation, as otherwise I'd %~1 and %~n1 etc for the input variable (which works so long as delayed expansion is disabled at the time of storing the variable).