我是个菜鸟,想弄清楚如何简化我正在创建的这个 .bat 文件(将电影分类到另一个按字母顺序排列的文件夹中),以便它的行数少得多。我想让它使用循环遍历整个字母表,然后是数字 0-9。任何帮助都将不胜感激。
setlocal EnableDelayedExpansion
::REN Need this for loop with !var! I think
TITLE Automated Symlink Session
set alphachar=A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
:SYMSTART
REN Change directory than find all files in "C:\Movies\" than output to the file named list
cd /d "C:\Movies\"
Dir A* /b > list
REN for each line of the file named list do an mklink and output it to Categorized folder under corresponding letter
for /F "usebackq delims==" %%i in ("C:\Movies\list") do set &(mklink /d "C:\Categorized\A\%%i" "C:\Movies\%%i")
Dir B* /b > list
for /F "usebackq delims==" %%i in ("C:\Movies\list") do (mklink /d "C:\Categorized\B\%%i" "C:\Movies\%%i")
Dir C* /b > list
for /F "usebackq delims==" %%i in ("C:\Movies\list") do (mklink /d "C:\Categorized\C\%%i" "C:\Movies\%%i")
Dir Z* /b > list
for /F "usebackq delims==" %%i in ("C:\Movies\list") do (mklink /d "C:\Categorized\Z\%%i" "C:\Movies\%%i")
Dir 1* /b > list
for /F "usebackq delims==" %%i in ("C:\Movies\list") do (mklink /d "C:\Categorized\#\%%i" "C:\Movies\%%i")
Dir 9* /b > list
for /F "usebackq delims==" %%i in ("C:\Movies\list") do (mklink /d "C:\Categorized\#\%%i" "C:\Movies\%%i")
Dir 0* /b > list
for /F "usebackq delims==" %%i in ("C:\Movies\list") do (mklink /d "C:\Categorized\#\%%i" "C:\Movies\%%i")
REN Clean up
del "C:\Movies\list"
:SYMEND
ECHO Symlinking Complete
Pause
答案1
只需打印 az 然后打印 0-9:
@echo off
setlocal enabledelayedexpansion
set loop=abcdefghijklmnopqrstuvwxyz0123456789
for /l %%i in (0,1,35) do echo !loop:~%%i,1!
将循环计数器保存到变量以便访问它:
@echo off
setlocal enabledelayedexpansion
set loop=abcdefghijklmnopqrstuvwxyz0123456789
for /l %%i in (0,1,35) do (
set lc=!loop:~%%i,1!
echo !lc!
)
答案2
这会满足您的要求吗?显然设置本地这只有一行。抱歉,我是新手,但已经编写批处理文件 25 年了。
For %%i in (a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9) do For /F "usebackq delims==" %%k in ('dir C:\Movies\%%i* /b') do (mklink /d "C:\Categorized\%%i\%%k" "C:\Movies\%%k")
答案3
我最终放弃了在命令行的 set 部分中使用 For 语句。无论我做什么,它都不起作用。这就是我为任何试图做同样事情的人所得到的。
For %%M in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z #) do (mkdir "C:\My Videos\Categorized\"%%M)
For %%N in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do ((Dir "G:\My Videos\TV Shows\"%%N* /b) >> "C:\My Videos\List\%%N.lst")
For %%O in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (For /F "usebackq delims==" %%P in ("C:\My Videos\List\%%O.lst") do (mklink /d "G:\My Videos\Categorized\%%O\%%P" "G:\My Videos\TV Shows\%%P"))
For %%L in (0 1 2 3 4 5 6 7 8 9) do ((Dir "G:\My Videos\TV Shows\"%%L* /b) >> "G:\My Videos\List\#.lst")
For /F "usebackq delims==" %%Q in ("G:\My Videos\List\#.lst") do (mklink /d "G:\My Videos\Categorized\#\%%Q"
也许这个可以更短,但比我之前处理的 36 多行要好得多
这就是我想做的事情,但除非有人能展示一个真正有效的方法,否则我认为它不会起作用
FOR /D %variable IN (FOR /D %variable IN (set) DO command [command-parameters]) DO command [command-parameters]