[英]batch script for file system: use of for-loop variable in another nested for-loop
在过去的4到5个小时里,我花了一些时间来修复脚本,并尝试了一些方法,但是仍然无法正常工作。 这是工作部分:
@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%"
下一步将是通过for循环增强代码,因此我不必为我的3k +文件中的每个文件都编辑脚本;)
我非常努力地使此代码运行,但失败了:@echo关闭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
因此,我添加了一个for循环,并且%% f首先被正确填充,而在下一个for循环中则没有。
FOR %%f IN (%source%\*.mp3) DO (
echo %%f
结果是:“ g:\\ music \\ folder \\ Wu-Tang Clan-Gravel Pit(LP Version Clean).mp3”
就像应该的那样。 但是之后
FOR /f "delims=" %%i IN ("%%f") DO SET "result=%%~nxi"
结果,并且随后的每个变量始终为空。
我试图用第二个变量作为“助手”来修复它:
FOR %%f IN (%source%\*.mp3) DO (
SET "helper=%%f"
FOR /f "delims=" %%i IN ("%helper%") DO SET "result=%%~nxi"
甚至在我阅读并加入“ enabledelayedexpansion”之后
FOR /f "delims=" %%i IN ("!helper!") DO SET "result=%%~nxi"
仍然不起作用。
现在我真的需要一些帮助,非常感谢:)
问候凤凰
下一个代码段可以工作。 请注意,不能使用!
来执行SET "band=%result: - =" & SET "song=%"
技巧!
与%
-expansion不同,延迟扩展。 因此,该命令将移至:myset
子例程并通过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
资源 (必读):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.