1

Sometimes I have both a Real Name and an Author Name as a folder in this format as follows: Real Name - as Author Name

Other times I have only an Author Name as a folder in this format as follows: Author Name

I want to strip out the Real Name and the " - as " in cases where a real name exists and set the Author Name only to a variable, or set the Author Name only to that variable when only an author name exists as follows: AN=%something%

Here is my futile attempt:

SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
FOR %%# IN (.) DO (SET CF=%%~f#
      SET NAME1=%%~nx#)
      SET $=%NAME1: - as =$%
for /f "tokens=1* delims=%$%" %%a in ("%NAME1%") do (
      SET part1=%%a
      SET AN=%%b
)

echo AN = %AN%
ENDLOCAL

Can someone please help me strip out the Author Name only and set it to a variable? Thanks in advance.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • you need [delayed expansion](https://stackoverflow.com/a/30284028/2152082). And `set $=%NAME1: - as =$%` should be `set NAME1=!NAME1: - as =!`. And why `%%~nx#` and not `%%~n#`? – Stephan Feb 12 '18 at 13:24

3 Answers3

2
@echo off
SETLOCAL

FOR %%a IN (*.*) DO call :GetAuthor "%%~NXa"
goto :EOF


:GetAuthor

set "string=%~1"

set "author="
set "real=%string: - as =" & set "author=%"
if not defined author set "author=%real%"

echo Folder: "%~1"
echo Author: "%author%"

This is a very short program that don't requires additional explanations, so the simplest way to understand it is remove the @echo off line, execute it and review the code...

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • That solution works fantastic. But I would really like to know how `set "real=%string: - as =" & set "author=%"` works. This line is wrong coded according to help of command __SET__. So I suppose a quirks of `cmd.exe` is used here to get the single string split up into `real` and `author`. I have the favor to ask you explaining this line or posting a link to a page where an explanation for this line can be read. – Mofi Feb 14 '18 at 06:54
  • @Mofi: are you ready to immerse into the incredible abyss? Ok. [Here it is](https://www.dostips.com/forum/viewtopic.php?f=3&t=6429), but remember that I warned you! **`;)`** – Aacini Feb 14 '18 at 14:08
  • Many thanks for the two links. I studied already more than an hour. It is very interesting, but the warning is really well-founded. I have to take surely two or even more hours more to understand all well and document all those string substitution techniques for myself for usage in future. Many thanks once again. – Mofi Feb 15 '18 at 18:35
  • @Mofi: I found another example: https://stackoverflow.com/a/33112178/778560 – Aacini Feb 18 '18 at 04:21
1

Your code with

  1. putting ) on a separate line,
  2. using NAME1 instead of $ on left side of equal sign on line substituting  - as  by character $,
  3. defining the delimiters for second FOR loop correct with $,
  4. 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 /?
Mofi
  • 46,139
  • 17
  • 80
  • 143
0

perhaps:

@echo off
setlocal enabledelayedexpansion
set "string=Real Name - as Author Name"
for /f "tokens=1,2 delims=-" %%a in ("%string%") do (
 set real=%%b
 set "real=!real: as =!"
 echo !real!
)
endlocal
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Thanks! It works perfectly under the condition that both a real name and an author name exist. However, it returns only the delimiter when an author name only exits. Is there some way to bypass the for instruction when the delimiter does not exist in the string, and set "!real!=%string%". Here is the modification: setlocal enabledelayedexpansion FOR %%# IN (.) DO (SET CF=%%~f# SET NAME1=%%~nx#) set string=%NAME1% for /f "tokens=1,2 delims=-" %%a in ("%string%") do ( set real=%%b set "real=!real: as =!" echo !real! ) endlocal – Phaedrus T. Wolfe Feb 12 '18 at 14:07