CMD for unable to move files due to string butcher

2019-09-04 23:36发布

问题:

I'm trying to format my USB stick for use in my car as music database. However, my radio can only playback one folder at a time, so I thought it would be helpful to move all titles of an artist to the artist's folder.

Since this are like 32GB of music that would be a lot of work by hand.

I've come up with the following script to do the job (G:\ being the USB drive)

FOR /F %%T IN ('dir G:\ /B /A:D') DO (
    FOR /R %%D IN (G:\%%T\*.*) DO MOVE "%%d" "G:\%%T"
)

So far this works, but the 'dir G:\ /B /A:D' command ends up butchering the folder names in this fashion: "FOLDER NAME" -> "FOLDER". So it essentially just picks up the first word for some reason.

Please help.

回答1:

The FOR metavariable is one of the few places in batch that is case-sensitive. The %%D mst match the %%d - or vice-versa.

(but you'd probably do well to include "delims=" between the FOR/f and the metavariable...) to ensure that the entire string is assigned to the metavariable.



回答2:

Change your outer FOR command:

for /f "tokens=*" %%T in ('dir /b /a:d "G:\"') DO (
 ...
)