0

I need to write simple script. The point is to sort the files in a folder whose name is the first letter of file name. I understand sequence of actions (get first letter of @fname; check if folder exist; create folder; move file) but i don't understand how it all write without block of code(do and end). Does forfiles blocks do/end? Or how I can write this script without block?

2
  • Give an example of what you need to sort. Forfiles may not be the best tool. Commented Jan 30, 2014 at 11:35
  • @foxidrive File test.txt move to folder t, file example.exe to folder named e etc. Commented Jan 30, 2014 at 18:15

2 Answers 2

2

Here's another method:

@echo off
   for %%a in (1 2 3 4 5 6 7 8 9 0 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 (
      if exist "%%a*" md "%%a" 2>nul & move "%%a*" "%%a"
   )
pause
Sign up to request clarification or add additional context in comments.

4 Comments

yes, but he wants a solution without ( blocks ).
@stephen I think OP can't integrate them into Forfiles. That was an or question, but the OP will have to return to confirm or deny. :D
reading the question some times more, you may be right. Anyway, the two solutions shows clearly how powerful those blocks are.
Thanks. I used a similar method. But instead of the set of numbers and letters I user (*) and then used block do (). I originally wanted to use forfiles (He seemed easier) But then I figured out the for and did it all with him. Thank you.
0
@echo off

dir /b /a-d *.* >tmp
echo END OF FILE>>tmp
echo -- start --

:loop
set /p file=<tmp
if "%file%"=="END OF FILE" goto :end
if "%file%"=="tmp" goto :ignore

md %file:~0,1%\%file% 2>nul
move "%file%" "%file:~0,1%\%file%"

:ignore
more +1 tmp >tmp2
del tmp
ren tmp2 tmp
goto :loop

:end
echo -- finish --
exit /b

Puh - I'm starting to love those blocks ^^

No need to check if the folder already exist, mdwill make it when it doesn't exist and give an error if it does exist (2>nul sends it to nirvana)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.