DosTips.com
http://www.dostips.com/forum/

Where is my mistake ?
http://www.dostips.com/forum/viewtopic.php?f=3&t=7644
Page 1 of 1

Author:  Jan_D [ 08 Jan 2017 13:57 ]
Post subject:  Where is my mistake ?

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

Author:  aGerman [ 08 Jan 2017 17:27 ]
Post subject:  Re: Where is my mistake ?

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

Author:  NunoLava1998 [ 29 Jan 2017 15:07 ]
Post subject:  Re: Where is my mistake ?

aGerman wrote:
Variablenerweiterung


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

Author:  aGerman [ 29 Jan 2017 15:21 ]
Post subject:  Re: Where is my mistake ?

Yeah. German language is mad :lol:
http://www.bbc.com/news/world-europe-22762040

Author:  Aacini [ 30 Jan 2017 15:26 ]
Post subject:  Re: Where is my mistake ?

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

Author:  aGerman [ 30 Jan 2017 16:19 ]
Post subject:  Re: Where is my mistake ?

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

Page 1 of 1 All times are UTC-06:00
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/