Newbie question (probably!)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
akira71
Posts: 3
Joined: 24 May 2011 14:13
Location: Edinburgh, Scotland

Newbie question (probably!)

#1 Post by akira71 » 24 May 2011 14:26

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Newbie question (probably!)

#2 Post by Ed Dyreen » 24 May 2011 19:31

Code: Select all

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

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

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Newbie question (probably!)

#3 Post by orange_batch » 25 May 2011 01:11

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.

akira71
Posts: 3
Joined: 24 May 2011 14:13
Location: Edinburgh, Scotland

Re: Newbie question (probably!)

#4 Post by akira71 » 27 May 2011 07:48

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Newbie question (probably!)

#5 Post by dbenham » 27 May 2011 08:35

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

Post Reply