在.BAT Windows 10中无法正确解释FOR循环变量和命令

操作系统:Windows 10。

我正在学习BATCH脚本。我试图应用FOR循环和IF条件对我的音乐收藏进行排序。我的音乐文件夹(带有子目录)可能具有.mp3文件的重复副本,如.aac文件(仅在文件名中相似,而不是二进制文件)。使用.BAT脚本,我决定尝试将这些重复的.aac文件移动到具有原始.mp3文件的每个目录/子目录中的“ aac”目录中。

我使用的代码如下:

@echo on
FOR %%B in ('dir /b /s *.mp3') do (
  FOR %%A in ('dir /b /s *.aac') DO (
    IF %%~nB==%%~nA (move %%A "%%~dpBaac\")
  )
)
pause

输出

E:\AUDIO\##########YOUTUBE RIPS - Copy>FOR %B in ('dir /b /s *.mp3') do (FOR %A in ('dir /b /s *.aac') DO (IF %~nB == %~nA (move %A "%~dpBaac\" ) ) )

E:\AUDIO\##########YOUTUBE RIPS - Copy>(FOR %A in ('dir /b /s *.aac') DO (IF 'dir == %~nA (move %A "E:\AUDIO\##########YOUTUBE RIPS - Copy\aac\" ) ) )

E:\AUDIO\##########YOUTUBE RIPS - Copy>(IF 'dir == 'dir (move 'dir "E:\AUDIO\##########YOUTUBE RIPS - Copy\aac\" ) )
The system cannot find the file specified.

E:\AUDIO\##########YOUTUBE RIPS - Copy>(IF 'dir == b (move /b "E:\AUDIO\##########YOUTUBE RIPS - Copy\aac\" ) )

E:\AUDIO\##########YOUTUBE RIPS - Copy>(IF 'dir == s (move /s "E:\AUDIO\##########YOUTUBE RIPS - Copy\aac\" ) )

E:\AUDIO\##########YOUTUBE RIPS - Copy>(FOR %A in ('dir /b /s *.aac') DO (IF b == %~nA (move %A "E:\aac\" ) ) )

E:\AUDIO\##########YOUTUBE RIPS - Copy>(IF b == 'dir (move 'dir "E:\aac\" ) )

E:\AUDIO\##########YOUTUBE RIPS - Copy>(IF b == b (move /b "E:\aac\" ) )
The system cannot find the file specified.

E:\AUDIO\##########YOUTUBE RIPS - Copy>(IF b == s (move /s "E:\aac\" ) )

E:\AUDIO\##########YOUTUBE RIPS - Copy>(FOR %A in ('dir /b /s *.aac') DO (IF s == %~nA (move %A "E:\aac\" ) ) )

E:\AUDIO\##########YOUTUBE RIPS - Copy>(IF s == 'dir (move 'dir "E:\aac\" ) )

E:\AUDIO\##########YOUTUBE RIPS - Copy>(IF s == b (move /b "E:\aac\" ) )

E:\AUDIO\##########YOUTUBE RIPS - Copy>(IF s == s (move /s "E:\aac\" ) )
The system cannot find the file specified.

E:\AUDIO\##########YOUTUBE RIPS - Copy>pause
Press any key to continue . . .

我知道

FOR / F

参数不适合我的情况,但是使用时,它将%% variables替换为foldername的一部分,并且IF条件也运行。

E:\AUDIO\##########YOUTUBE RIPS - Copy>(IF ##########YOUTUBE == ##########YOUTUBE (move E:\AUDIO\##########YOUTUBE "E:\AUDIO\aac\" ) )
The system cannot find the file specified.

E:\AUDIO\##########YOUTUBE RIPS - Copy>(IF ##########YOUTUBE == ##########YOUTUBE (move E:\AUDIO\##########YOUTUBE "E:\AUDIO\aac\" ) )
^CThe system cannot find the file specified.

更新:我使其在以下评论的帮助下工作。然而

  1. 我必须删除嵌套的
  2. 我不得不用/ lim与delims
  3. 对于/ F delims ^ =,在('command')中,^是必需的

可行的解决方案是:

@echo on
FOR /f delims^=, %%I in ('dir /b /s *.aac') do (
   if exist "%%~nI.mp3" 2> nul md "%%~dpIaac" & move "%%~I" "%%~dpIaac"
   )
pause

可能是什么原因? “打开/ F ...”是否不会打开并读取每个文件,这对我来说是不必要的?