DosTips.com

A Forum all about DOS Batch
It is currently 27 Mar 2017 20:18

All times are UTC-06:00




Post new topic  Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Where is my mistake ?
PostPosted: 08 Jan 2017 13:57 
Offline

Joined: 08 Jan 2017 12:52
Posts: 1
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


Top
   
PostPosted: 08 Jan 2017 17:27 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 2555
Location: Germany
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


Top
   
PostPosted: 29 Jan 2017 15:07 
Offline

Joined: 01 Mar 2015 10:06
Posts: 10
aGerman wrote:
Variablenerweiterung


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


Top
   
PostPosted: 29 Jan 2017 15:21 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 2555
Location: Germany
Yeah. German language is mad :lol:
http://www.bbc.com/news/world-europe-22762040


Top
   
PostPosted: 30 Jan 2017 15:26 
Offline
Expert

Joined: 06 Dec 2011 22:15
Posts: 1329
Location: México City, México
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


Top
   
PostPosted: 30 Jan 2017 16:19 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 2555
Location: Germany
German officials couldn't have made it better (ummm ... I mean worse) :lol:


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 6 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Bing [Bot], MSNbot Media and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited