Page 1 of 1

Newbie question (probably!)

Posted: 24 May 2011 14:26
by akira71
I'm trying to create a batch file to search for files with a word in their title, in both a directory and sub-directory and then copy it to a stated location (this doesn't need to be interactive). I'm a complete batch file newbie, so I have not a single clue where to start. I found a file on the net that searches in the root directory only I cannot seem to figure out what to do next, even after reading some information (thick, I know!). Dir doesn't seem to do much but echo directory structures to me (even with a file name). Can anyone help me sort this out? Here's the code:

Code: Select all

@echo off
cls
:sets
echo you can say "DefaultSearch" for default folder
echo DON'T use ""
set /P SearchIn=What folder shall I search in?:
cls
set DefaultSearch=C:\
set /P FileToSearch=What files to search and copy?:
cls
echo you can say Default for the default folder
echo DON'T use ""
set /P CopyTo=Where shall I copy to?:
set Default="C:\test\"
:begin
cd "%c:\%"
copy *.txt "%c:\test\%"
if not %ErrorLevel% == 0 echo some error, errorlevel is %ErrorLevel% & pause
:end
echo Done.
pause
exit

Re: Newbie question (probably!)

Posted: 24 May 2011 19:31
by Ed Dyreen

Code: Select all

set Default="C:\test\"
copy *.txt "%c:\test\%"

don't u mean
set "Default=C:\test\"
copy *.txt "%Default%"

Re: Newbie question (probably!)

Posted: 25 May 2011 01:11
by orange_batch
I'm not gonna figure out all the switches for you for xcopy (type xcopy /?), but...

Code: Select all

set /p "default=Search in what directory?: "
set /p "search=Copy all files matching what characters?: "
set /p "destination=Copy where?: "
for /r "%default%" %%x in ("*%search%*") do xcopy "%%x" "%destination%"


* is a wildcard character.

Re: Newbie question (probably!)

Posted: 27 May 2011 07:48
by akira71
Thanks all :D . Those both worked in their own ways. The end product looks like this (in case anyone else ever needs it):

Code: Select all

@echo off
cls
:sets
set /p "default=Search in what directory?: "
set /p "search=Copy all files matching what characters?: "
set /p "destination=Copy where?: "
for /r "%default%" %%x in ("*%search%*") do xcopy "%%x" "%destination%"
:end
echo Done.
pause
exit

Re: Newbie question (probably!)

Posted: 27 May 2011 08:35
by dbenham
Be careful! Your source directory tree can have different files with the same name but only one will survive in your copy!

For example, the following set of files will give you problems - your destination directory will only have one version of foobar.txt:

Code: Select all

D:\>dir /s sourceDir
 Volume in drive D has no label.
 Volume Serial Number is F8FD-5039

 Directory of D:\sourceDir

05/27/2011  10:23 AM    <DIR>          .
05/27/2011  10:23 AM    <DIR>          ..
05/27/2011  10:19 AM                 7 foobar.txt
05/27/2011  10:23 AM    <DIR>          subDir
               1 File(s)              7 bytes

 Directory of D:\sourceDir\subDir

05/27/2011  10:23 AM    <DIR>          .
05/27/2011  10:23 AM    <DIR>          ..
05/27/2011  10:27 AM                26 foobar.txt
               1 File(s)             26 bytes

     Total Files Listed:
               2 File(s)             33 bytes
               5 Dir(s)  73,244,889,088 bytes free


Dave Benham