copy or move files based on file extensions

Microsoft Windows xp professional editio...
October 2, 2009 at 00:51:04
Specs: Windows Vista, p 4 / 4 GB
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\)


See More: copy or move files based on file extensions

Report •


#1
October 2, 2009 at 05:29:07
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.


Report •

#2
October 2, 2009 at 05:34:03
"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 EnableDELAYedExpansion

for /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 felicity

M2


Report •

#3
October 2, 2009 at 11:19:54
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?


Report •

Related Solutions

#4
October 2, 2009 at 13:02:53
All I learned all about FOR, I learned from the following commands:
FOR /?
SET /?


Report •

#5
October 3, 2009 at 05:08:13
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 felicity

M2


Report •


Ask Question