Find specific files from a directory and copy to a newfolder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lifeh2o
Posts: 2
Joined: 08 Feb 2010 10:51

Find specific files from a directory and copy to a newfolder

#1 Post by lifeh2o » 08 Feb 2010 13:21

I want to find specific files (*quiz*.htm or *.log) from all subdirectories and copy them to a folder, all files should have sepearter name (1.htm,2.htm,3.htm,4.htm.....)
how can i do this?

i used this first
c:\>xcopy *quiz*.htm d:\myfolder /e

but it copies all empty folders then i am trying this but it is still very difficult
for /r c:\ %%s in (*.log) do (for /L %%t in (1 1 1000) do xcopy "%%~dps%%~nxs" D:\quizes\%%t.log)

there could be many files with same name thats why i want to copy them with different names

Need help

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Find specific files from a directory and copy to a newfolder

#2 Post by aGerman » 08 Feb 2010 14:58

lifeh2o

Try if this will work for you.

Code: Select all

@echo off &setlocal
set /a n=0
set /a m=0
for /f "delims=" %%a in ('dir /a-d /b /s "c:\*.log" "c:\*quiz*.htm"') do call :sub "%%a"
goto :eof

:sub
if /i "%~x1"==".log" (
  set /a n+=1
  call set i=%%n%%
) else (
  set /a m+=1
  call set i=%%m%%
)
copy %1 "D:\quizes\%i%%~x1"
goto :eof


Regards
aGerman

lifeh2o
Posts: 2
Joined: 08 Feb 2010 10:51

Re: Find specific files from a directory and copy to a newfolder

#3 Post by lifeh2o » 08 Feb 2010 15:06

Thank you very much
After a lot of tries i made work this simplified code

SetLocal EnableDelayedExpansion
for /r c:\ %%s in (*quiz*.htm) do (
set /a v+=1
copy "%%~dps%%~nxs" D:\quizes\!v!.htm)

You have done this for both files, i am still very curuious about batch files, they help a lot in tedious tasks.

Thanks again, I will learn a lot about batch files in this forum.

Post Reply