Your code with
- putting
)
on a separate line,
- using
NAME1
instead of $
on left side of equal sign on line substituting - as
by character $
,
- defining the delimiters for second FOR loop correct with
$
,
- adding two more lines to work also for folder names not containing real name
is as follows:
@echo off
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
FOR %%# IN (.) DO (
SET "CF=%%~f#"
SET "NAME1=%%~nx#"
)
SET "AN="
SET "NAME1=%NAME1: - as =$%"
FOR /F "tokens=1* delims=$" %%I in ("%NAME1%") do (
SET "part1=%%I
SET "AN=%%J"
)
IF NOT DEFINED AN SET "AN=%NAME1%"
ECHO AN = %AN%
ENDLOCAL
By the way: It would be better to use |
instead of $
two times in code above because it is impossible that a folder name contains the vertical bar character while it is possible that a folder name contains dollar sign although not very likely.
Another solution using a subroutine which can be called for each subdirectory in current directory by replacing
for %%# in (.) do call :GetNames "%%~nx#"
for example by
for /D %%# in (*) do call :GetNames "%%~nx#"
in the commented code below:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Run subroutine GetNames with name of current directory without path.
for %%# in (.) do call :GetNames "%%~nx#"
endlocal
goto :EOF
:GetNames
set "FolderName=%~1"
rem Get the string after " - as " which is the author name.
set "AuthorName=%FolderName:* - as =%"
rem If the author name is identical to folder name, then there
rem is not string " - as " included in folder name which means
rem the folder name is the author name.
if "%AuthorName%" == "%FolderName%" (
set "RealName="
goto OutputNames
)
rem Use delayed expansion to remove author name from folder
rem name to get real name in addition to author name.
setlocal EnableDelayedExpansion
set "RealName=!FolderName:- as %AuthorName%=!"
endlocal & set "RealName=%RealName%"
:OutputNames
echo Folder name: %~1
echo Real name: %RealName%
echo Author name: %AuthorName%
goto :EOF
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?