Where is my mistake ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jan_D
Posts: 1
Joined: 08 Jan 2017 12:52

Where is my mistake ?

#1 Post by Jan_D » 08 Jan 2017 13:57

I am new here
Sorry, who wants to communicate with me in German, has 200% bonus, everything else the translator has broken

Code: Select all

@ECHO OFF
setlocal enableextensions
CLS

rem generate Testdata
mkdir "VA - The Doome 000"
cd "VA - The Doome 000"
echo X > "01 - Horst P - Titel1.mp3"
echo X > "02 - Beba - es muss.mp3"
echo X > "04 - John Smith - doch gehen.mp3"
cd ..
mkdir "VA - XY Sisters"
cd "VA - XY Sisters"
echo X > "04#Heidi#Intro.mp3"
echo X > "08#Santa C#No name.mp3"
echo X > "13#Children#Games.mp3"
cd ..

rem Delims can only process one character, would be " - ", so I replaced the " - " with "#"
for /f "tokens=1,2,3 delims=#" %%a in ('dir /a /b /s *.mp3') do (
   set track=%%~na&set artist=%%b&set titel=%%c
   echo.Track : %track%
   echo.Artist: %artist%
   echo.Titel : %titel%
   echo ---------------
)
pause

rem My second hurdle is, it works so, but I so only the last entry get, so not to use.
for /f "tokens=1,2,3 delims=#" %%a in ('dir /a /b /s *.mp3') do set track=%%~na&set artist=%%b&set titel=%%c
   echo.Track : %track%
   echo.Artist: %artist%
   echo.Titel : %titel%
   echo ---------------
pause


rem dir clean
rmdir /s /q "VA - The Doome 000"
rmdir /s /q "VA - XY Sisters"

set track=
set artist=
set titel=

endlocal

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Where is my mistake ?

#2 Post by aGerman » 08 Jan 2017 17:27

Code: Select all

@ECHO OFF
setlocal EnableExtensions

mkdir "VA - The Doome 000"
pushd "VA - The Doome 000"
echo X > "01 - Horst P - Titel1.mp3"
echo X > "02 - Beba - es muss.mp3"
echo X > "04 - John Smith - doch gehen.mp3"
popd

for /f "delims=" %%a in ('dir /a /b /s *.mp3') do (
  set "filename=%%~na"
  call :disassemble
)

rmdir /s /q "VA - The Doome 000"

pause
exit /b


:disassemble
for /f "tokens=1,2* delims=|" %%i in ("%filename: - =|%") do (
  set "track=%%i"
  set "artist=%%j"
  set "titel=%%k"
  setlocal EnableDelayedExpansion
  echo.Track : !track!
  echo.Artist: !artist!
  echo.Titel : !titel!
  echo ---------------
  endlocal
)
exit /b


Since this is an international forum I should at least answer bilingual...

In a single command line or a block of command lines (enclosed into paretheses) variables will be expanded to their values only once. That is, before executing the command line or block. Thus, changes of the variable are not accessible in the same line/block. You can enable the delayed expansion and replace surrounding percent signs with exclamation marks. This will break file names with exclamation marks though. You have to toggle between disabled (for assignments) and enabled (for processing) delayed expansion. In order to avoid too many times toggling I call a subroutine for each found file where you can access the variables set in the main code. If you quit the subroutine with exit /b (or goto :eof) the execution of the script continues in the main code.
Probably you can avoid delayed expansion if you use the FOR variables (%%i, %%j, %%k) directly instead of assigning variable track, artist, and title beforehand.

In derselben Kommandozeile oder einem (in Klammern eingeschlossenen) Block von Kommandozeilen, werden Variablen nur einmal zum Wert erweitert. Das passiert noch vor der Ausführung der Kommandozeile oder des Blocks. Somit ist nicht auf Änderungen einer Variable in derselben Zeile/demselben Block zuzugreifen. Du kannst die verzögerte Variablenerweiterung nutzen und umschließende Prozentzeichen durch Ausrufezeichen ersetzen. Das wird aber Dateinamen mit Ausrufezeichen verfälschen. Du musst zwischen ausgeschalteter (für Zuweisungen) und eingeschalteter (für die Verarbeitung) verzögerter Variablenerweiterung wechseln. Um nicht zu oft wechseln zu müssen, rufe ich eine Unterroutine für jede gefundene Datei auf, in der du auf die im Hauptcode gesetzten Variablen zugreifen kannst. Wenn du die Unterroutine mit exit /b (oder goto :eof) beendest, wird die Abarbeitung des Scripts im Hauptcode fortgesetzt.
Wahrscheinlich kannst du die verzögerte Variablenerweiterung vermeiden, wenn du die FOR Variablen (%%i, %%j, %%k) direkt nutzt, anstatt vorher die Variablen track, artist und title zuzuweisen.

Steffen

NunoLava1998
Posts: 10
Joined: 01 Mar 2015 10:06

Re: Where is my mistake ?

#3 Post by NunoLava1998 » 29 Jan 2017 15:07

aGerman wrote:Variablenerweiterung


Love the small length of that word.
-Nuno, a portuguese speaker.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Where is my mistake ?

#4 Post by aGerman » 29 Jan 2017 15:21


Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Where is my mistake ?

#5 Post by Aacini » 30 Jan 2017 15:26

I just realized that some of the Intel CPU instructions are written in German! :shock:

PMADDUBSW: "Multiply and Add Packed Signed and Unsigned Bytes". :mrgreen:

Antonio

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Where is my mistake ?

#6 Post by aGerman » 30 Jan 2017 16:19

German officials couldn't have made it better (ummm ... I mean worse) :lol:

Post Reply