1

the last 4-5 hours I spent trying to get my script fixed, and tried several things, but it still doesn't work as supposed. Here is the working part:

    @echo off
    rem automatically sort MP3s into an existing structure or add folders if needed
    rem example of MP3-file pattern: Wu-Tang Clan - Gravel Pit (LP Version Clean).mp3

    SETLOCAL

    rem set variables
    SET "source=g:\music\folder"
    SET "str=Wu-Tang Clan - Gravel Pit (LP Version Clean).mp3"
    SET "test=%source%\%str%"

    rem split the filename from the remaining path. Take the left part
    rem and assign it to %band% while the right part gets assigned to %song%
    FOR /f "delims=" %%i IN ("%test%") DO SET "result=%%~nxi"

    SET "band=%result: - =" & SET "song=%"

    rem If there is no such folder (%band%), create it
    IF not exist "%source%\%band%" MD "%source%\%band%"

    rem As soon as there definetely is a fitting folder
    rem move the file over there.
    MOVE /-Y "%source%\%result%" "%source%\%band%"

The next step would be to enhance the code by a for-loop so I don't have to edit the script for every single one of my 3k+ files ;)

I tried really hard to get this code going, but failed: @echo off SETLOCAL

    SET "source=g:\music\folder"

    FOR %%f IN (%source%\*.mp3) DO (
    echo %%f

    FOR /f "delims=" %%i IN ("%%f") DO SET "result=%%~nxi"

    SET "band=!result: - =" & SET "song=%"

    IF not exist "%source%\%band%" MD "%source%\%band%"

    MOVE /-Y "%source%\%result%" "%source%\%band%"
    )
    pause

So I added a for-loop and %%f was correctly filled at first and wasn't in the next for-loop.

    FOR %%f IN (%source%\*.mp3) DO (
    echo %%f

That resulted in: "g:\music\folder\Wu-Tang Clan - Gravel Pit (LP Version Clean).mp3"

Like it was supposed to. But after that

    FOR /f "delims=" %%i IN ("%%f") DO SET "result=%%~nxi"

result and every following variable was empty, always.

I tried to fix it with a second variable as a 'helper':

    FOR %%f IN (%source%\*.mp3) DO (
    SET "helper=%%f"
    FOR /f "delims=" %%i IN ("%helper%") DO SET "result=%%~nxi"

Even added 'enabledelayedexpansion' after I read about that and went for

    FOR /f "delims=" %%i IN ("!helper!") DO SET "result=%%~nxi"

still doesn't work.

Now I could really need some help and would really appreciate it :)

regards Phoenix

Hello Fishy
  • 729
  • 5
  • 16
  • You will need [delayed expansion](http://stackoverflow.com/a/10558905/5047996) to put your code in a `for` loop and use the `!variable!` expansion syntax; otherwise, when stating `%variable%`, it will expand to the value present _before_ the entire loop executes; – aschipfl Oct 30 '15 at 00:38

1 Answers1

1

Next code snippet could work. Note that the SET "band=%result: - =" & SET "song=%" trick can't be performed using ! delayed expansion unlike %-expansion. Therefore, that command is moved to the :myset subroutine and executed via call command.

@echo off
SETLOCAL EnableExtensions EnableDelayedExpansion
SET "source=g:\music\folder"
FOR %%f IN (%source%\*.mp3) DO (
  echo %%f
  FOR /f "delims=" %%i IN ("%%f") DO SET "result=%%~nxi"
  call :myset
  IF not exist "%source%\!band!" MD "%source%\!band!"
  MOVE /-Y "%source%\!result!" "%source%\!band!"
)
goto :skipMyset

:myset
  SET "band=%result: - =" & SET "song=%"
goto :eof

:skipMyset
pause

Resources (required reading):

JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • A SUBROUTINE!!!! Of course -.- Now it does its job. Thanks a lot JosefZ for fast and correct answer, made my day :) – Hello Fishy Oct 30 '15 at 13:36