文件系统的批处理脚本:在另一个嵌套的 for 循环中使用 for 循环变量
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+ 文件中的每一个文件都编辑脚本 ;)
我非常努力地让这段代码运行起来,但失败了:
@回声关闭
设置本地
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"
结果,后面的每个变量始终为空。
我试图用第二个变量修复它 'helper':
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"
还是不行。
现在我真的需要一些帮助,非常感谢:)
问候凤凰
下一个代码片段可以工作。请注意,与 %
-expansion 不同,SET "band=%result: - =" & SET "song=%"
技巧无法使用 !
延迟扩展来执行。因此,该命令被移动到 :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
资源(必读):
我花了最后 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+ 文件中的每一个文件都编辑脚本 ;)
我非常努力地让这段代码运行起来,但失败了: @回声关闭 设置本地
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"
结果,后面的每个变量始终为空。
我试图用第二个变量修复它 'helper':
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"
还是不行。
现在我真的需要一些帮助,非常感谢:)
问候凤凰
下一个代码片段可以工作。请注意,与 %
-expansion 不同,SET "band=%result: - =" & SET "song=%"
技巧无法使用 !
延迟扩展来执行。因此,该命令被移动到 :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
资源(必读):