Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Please anyone can help me, i just a new learner in the world of batch files,
my q is:
i want a patch to copy or move my files based on the file extension to diff folders
for ex:
*.bat files to be copied to ---> Bat Folder
*.txt files to be copied to ---> Txt Folder
and so on
i know how to make it with copy or xcopy, but i want to make it with FOR command as i want to practice it cause i don't understand it completely and i can't find a good place on internet give a simple explanation about FOR for new learners, so i decided to practice it by using it in my daily work.i made it in some ways but my question is:
am i going on the right way or i don't understand how FOR command is working???
and is there a another way to make it done than what i made.
thanks in advance
here is the codes::: at first i'm making the folders then copying
FOR %%J in (TXT_Files BAT_Files CMD_Files ZIP_Files) DO (MD C:\TESTCOPY\%%J)FOR /R "C:\TESTCOPY" %%F IN (*.TXT *.BAT *.CMD *.ZIP) DO ( MOVE C:\TESTCOPY\*.TXT C:\TESTCOPY\TXT_Files\ && MOVE C:\TESTCOPY\*.BAT C:\TESTCOPY\BAT_Files\ && MOVE C:\TESTCOPY\*.CMD C:\TESTCOPY\CMD_Files\ && MOVE C:\TESTCOPY\*.ZIP C:\TESTCOPY\ZIP_Files\)
:: can i make the 2 step in one? detect if the folders is exist or to make folders and to copy files each in it's folder?
::ANOTHER WAY
FOR %%J in (TXT_Files BAT_Files CMD_Files ZIP_Files) DO (MD C:\TESTCOPY\%%J)MOVE C:\TESTCOPY\*.TXT C:\TESTCOPY\TXT_Files\
MOVE C:\TESTCOPY\*.BAT C:\TESTCOPY\BAT_Files\
MOVE C:\TESTCOPY\*.CMD C:\TESTCOPY\CMD_Files\
MOVE C:\TESTCOPY\*.ZIP C:\TESTCOPY\ZIP_Files\:: ANOTHER WAY
FOR %%J in (TXT BAT CMD ZIP) DO (MD C:\TESTCOPY\%%J)FOR %%F IN (TXT BAT CMD ZIP) DO ( MOVE C:\TESTCOPY\*.%%F C:\testcopy\%%F\ && MOVE C:\TESTCOPY\*.%%f C:\testcopy\%%F\ && MOVE C:\TESTCOPY\*.%%F C:\testcopy\%%F\ && MOVE C:\TESTCOPY\*.%%F c:\testcopy\%%F\)

I'd do it like this:
FOR %%a IN (TXT BAT CMD ZIP) DO MD "C:\TESTCOPY\%%a_Files" & MOVE "C:\TESTCOPY\*.%%a" "C:\TESTCOPY\%%a_Files\"EDIT: Fixed typo.

"FOR %%J in (TXT_Files BAT_Files CMD_Files ZIP_Files) DO (MD C:\TESTCOPY\%%J)"
That chunk is OK. If the dest exists you'll get an error but it'll work.
To do the MOVEs, assuming the files to move are in the current directory:
==========================
@echo off & setLocal EnableDELAYedExpansionfor /f "tokens=* delims= " %%a in ('dir/b/a-d *.txt *.bat *.cmd *.zip') do (
set ext=%%~Xa
set ext=!ext:~1!
echo move "%%a" C:\TESTCOPY\!ext!_Files\
)
=====================================
Helping others achieve escape felicityM2

Thank you guys for trying to help me, i really appreciate your help very much.
i'll use your examples and re-post my result .this one i can understand:
FOR %%a IN (TXT BAT CMD ZIP) DO MD "C:\TESTCOPY\%%a_Files" & MOVE "C:\TESTCOPY\*.%%a" "C:\TESTCOPY\%%a_Files\"* but this one (don't be upset Mechanix2Go) but as i'm new in batch coding so i didn't understand these 2 lines:
set ext=%%~Xa
set ext=!ext:~1!anyway thanks again, i'll try to understand how it works and re-post.
aa last thing, do any one know where i can find a place to read about FOR in detail to learn from, but in easy way?

set ext=%%~Xa
set ext=!ext:~1!The first line get the extension WITH THE DOT.
The second line drops the DOT [.].
=====================================
Helping others achieve escape felicityM2

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |