Computing.Net > Forums > Programming > copy or move files based on file extensions

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

copy or move files based on file extensions

Reply to Message Icon

Name: HaMaDa
Date: October 2, 2009 at 00:51:04 Pacific
OS: Windows Vista
CPU/Ram: p 4 / 4 GB
Product: Microsoft Windows xp professional edition
Subcategory: Batch
Tags: FOR command
Comment:

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\)



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: October 2, 2009 at 05:29:07 Pacific
Reply:

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.


0

Response Number 2
Name: Mechanix2Go
Date: October 2, 2009 at 05:34:03 Pacific
Reply:

"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


0

Response Number 3
Name: HaMaDa
Date: October 2, 2009 at 11:19:54 Pacific
Reply:

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?


0

Response Number 4
Name: Razor2.3
Date: October 2, 2009 at 13:02:53 Pacific
Reply:

All I learned all about FOR, I learned from the following commands:

FOR /?
SET /?


0

Response Number 5
Name: Mechanix2Go
Date: October 3, 2009 at 05:08:13 Pacific
Reply:

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


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: copy or move files based on file extensions

Win2k batch: copy only new files? www.computing.net/answers/programming/win2k-batch-copy-only-new-files/10386.html

Batch file xcopy based on date www.computing.net/answers/programming/batch-file-xcopy-based-on-date/15692.html

Moving Messages based on content www.computing.net/answers/programming/moving-messages-based-on-content/15376.html