Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm a noob trying to figure out how to simplify this .bat file I'm creating (sorts out movies into another alphabetized folder) so that it will have far fewer lines. I want to get it to use loop(s) that cycles through the whole alphabet and than numbers 0-9. Any help on this would be greatly appreciated.

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
share|improve this question

3 Answers 3

up vote 1 down vote accepted

To simply print a-z and then 0-9:

@echo off
setlocal enabledelayedexpansion
set loop=abcdefghijklmnopqrstuvwxyz0123456789
for /l %%i in (0,1,35) do echo !loop:~%%i,1!

To save the loop counter to a variable so you can access it:

@echo off
setlocal enabledelayedexpansion
set loop=abcdefghijklmnopqrstuvwxyz0123456789
for /l %%i in (0,1,35) do (
    set lc=!loop:~%%i,1!
    echo !lc!
)
share|improve this answer

Will this do what you want. Obviously set local That is all one line. sorry I am new here, but have written batch files for 25 years now.

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")
share|improve this answer
4  
Please check your formatting. You have to indent code by 4 spaces, or select the code and then press Ctrl-K. Thanks! –  slhck Jan 17 '13 at 8:57
    
Thank you slchk I couldn't' figure out how to make it code. –  Michael Mantion Jan 19 '13 at 21:47

I finally ended up giving up on using the For statement inside the set part of the command line. No matter what I did it didn't work. This is what I ended up with for anyone trying to do the same thing.

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"

Perhaps this can be even shorter but it's much better than the 36+ lines I had to deal with before

This is what I was trying to do, but unless some else can show one that does actually work I don't think it does

FOR /D %variable IN (FOR /D %variable IN (set) DO command [command-parameters]) DO command [command-parameters]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.