操作系统: Windows 10。
我正在学习批处理脚本。我试图申请循环和如果条件,以排序我的音乐收藏。我的音乐文件夹(带有子目录)有可能将.mp3文件复制为.aac文件(仅在文件名中类似,而不是二进制文件)。使用.BAT脚本,我决定尝试将这种重复的.aac文件移到具有原始.mp3文件的每个目录/子目录中的"aac“目录中。
我使用的代码如下所示:
@echo on %B in ('dir /b /s *.mp3') do (%A in ('dir /b /s *.aac') DO (如果%%~nB==%%~nA (移动%A“%~dpBaac\”))暂停
输出
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 . . .我知道
/F
参数不适合我的情况,但在使用时,它将%%变量替换为foldername的一部分,并且IF条件也会运行。
E:\AUDIO\##########YOUTUBE RIPS - Copy>(FOR /F %A in ('dir /b /s *.aac') DO (IF ##########YOUTUBE == %~nA (move %A "E:\AUDIO\aac\" ) ) )
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\" ) )
The system cannot find the file specified.
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\" ) )
The system cannot find the file specified.
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.发布于 2020-06-22 16:39:30
我发布了一个对我有用的答案,由@aschipfl在评论中建议。
必须用于/F (在您的情况下使用选项字符串"delims=“)来处理像dir这样的命令的输出。不过,您有两个不必要的嵌套循环;只需在一个循环中遍历*.mp3文件,并使用"%%~nI.aac“2> nul”%~dpIaac“& move”%~i“”%~dpIaac“(假设%%I是循环变量)检查是否具有同等名称的2>文件。
因此,最终代码如下:
@echo on
FOR /f delims^=, %%I in ('dir /b /s *.ogg') do (
if exist "%%~nI.mp3" 2> nul md "%%~dpIogg" & move "%%~I" "%%~dpIogg"
)
pause仍然不清楚为什么使用嵌套的for循环而不是上述代码中的if条件不能工作。
https://stackoverflow.com/questions/61856012
复制相似问题