Solved

search and copy

Posted on 2008-10-24
26
307 Views
Last Modified: 2010-04-21
suppose i have 3 files on cd namely file a,b,c.
batch file code that when run check whether any of these files (a,b,c) are on any of partition or not.
if any of file found delete that file and recopy all files from cd to specified location say C:\cdbackup.
0
Comment
Question by:ankur3020
  • 10
  • 8
  • 8
26 Comments
 
LVL 21

Expert Comment

by:AmazingTech
Comment Utility
How exact match are you wanting to check for before deleting? Exact file name only? Size? Date/time?
0
 

Author Comment

by:ankur3020
Comment Utility
yes file name and size
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
What you could do is test for the 3 files by doing the following:

   set flag=0
   for /f %%f in (fila_a file_b file_c) do (
      del %%f
      set flag=1
   )

Obviously, you'll have to replace file_a, file_b and file_c in the FOR statement with the proper names of the files you want to search for. You'll also have to change to the partitions root drive.

Then, you could do the following:

   if %flag%==1 xcopy d:\*.* c:\cdbackup /s /e /v /c /q /h /r /y
   set flag=0

assuming D: is the letter of your CD drive as shown in the XCOPY commmand.

Is that what you had in mind?
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
Sorry, that was supposed to be a '/r' not an '/f'' (int the FOR statement) as in....

What you could do is test for the 3 files by doing the following:

   set flag=0
   for /r %%f in (fila_a file_b file_c) do (
      del %%f
      set flag=1
   )

Obviously, you'll have to replace file_a, file_b and file_c in the FOR statement with the proper names of the files you want to search for. You'll also have to change to the partitions root drive.

Then, you could do the following:

   if %flag%==1 xcopy d:\*.* c:\cdbackup /s /e /v /c /q /h /r /y
   set flag=0

assuming D: is the letter of your CD drive as shown in the XCOPY commmand.

Is that what you had in mind?
0
 

Author Comment

by:ankur3020
Comment Utility
i used following code. when i used d: in xcopy statement, it copied all files from my d drive.
my cd rom drive is f , so i changed d with f but it did nothing. pls check the code and the code i used is attached.
set flag=0

   for /r %%f in (rem.exe) do 

      (del %%f

      set flag=1)

   

 if %flag%==1 xcopy d:\*.* f:\cdbackup /s /e /v /c /q /h /r /y

   set flag=0

Open in new window

0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
Do not movbe the paranthesis after the DO statement on the FOR line, nor the closing paranthesis.

NOTE: Replace the text "list of files to search for such as rem.exe" with one or more filenames



@echo off

set flag=0

for /r %%f in (list of files to search for such as rem.exe) do (
   if exist %%f (
      del %%f
      set flag=1
   )
)
   
if %flag%==1 (
   echo Copying files from D: to C:\CD BACKUP
   xcopy d:\*.* f:\cdbackup /s /e /v /c /q /h /r /y
)

set flag=
0
 

Author Comment

by:ankur3020
Comment Utility
i re tested but it is doing nothing.  the code i used attached.pls check it,why not working proprly. in the code i am looking for rem.exe on hard drives and if not found then copying files from d drive to backup. where d is my second partiton
@echo off
 

set flag=0
 

for /r %%f in (rem.exe) do (

   if exist %%f (

      del %%f

      set flag=1

   )

)

   

if %flag%==1 (

   echo Copying files from D: to C:\CD BACKUP

   xcopy d:\*.* c:\cdbackup /s /e /v /c /q /h /r /y
 

set flag=0

Open in new window

0
 
LVL 16

Assisted Solution

by:t0t0
t0t0 earned 50 total points
Comment Utility
In which folder on your hard drive is the REM.EXE file that you are testing this batch file on?

What folder did you put this batch file in.

What is the current folder you are in when you run the batch file?

The batch file should be run (or CALLed) from within the same folder OR from a higher folder in the same branch as the folder containing REM.EXE.

To ensure that this is the case...

1) place the batch file somewhere where you would normally call your batch commands from say, C:\BATCH then,

2) change to the root folder of your drive eg, CD \ then,

3) then run your batch file eg,

You're currently at:

C:\>

so enter
   
   C:\BATCH\test.bat

(or whatever you named your batch file)

----------------------------

So, if, as you state, nothing appeared to happen, this can be because of a couple of reasons....

a) the file REM.EXE does not exist OR,

b) REM.EXE DOES exist but not on the same branch AND at or below the folder from where you typed your command (or CALLed the batch file).

To remedy this, you can do one of three things prior to running the batch file...

a) physically change to a folder higher than the folder where REM.EXE is placed (but you still need to be on the same branch though)... If you change to the root folder, all should be okay. OR,

b) place the following command in the batch file on the line before the FOR command:

CD\

c) if you know where REM.EXE is all the time then you won't even have to search for it, you could merely check for it's existence ie,

IF EXIST c:\somewhere\on\the\drive\REM.EXE SET flag=1 instead of the FOR/IF combination - but this assumes the three files (or any one of them) is always going to be found in the same place.

Using the FOR command with the /R command-line switch 'SEARCHES' your drive - BUT ONLY FROM YOUR CURRENT POSITION ONWARDS!! (which is why you need to be on the same level or above it).

Using just an IF EXIST with the full path specified means you can test for the file's exostence no matter what your current folder or branch is. Does that make any sense to you?

As stated above in option (b), I've added the CD \ command to the code which should now work for you.


@echo off
 

set flag=0
 

cd \
 

for /r %%f in (rem.exe) do (

   if exist %%f (

      del %%f

      set flag=1

   )

)

   

if %flag%==1 (

   echo Copying files from D: to C:\CD BACKUP

   xcopy d:\*.* f:\cdbackup /s /e /v /c /q /h /r /y

)
 

set flag=

Open in new window

0
 
LVL 21

Expert Comment

by:AmazingTech
Comment Utility
Hmm..
So if you had the 3 files and the size matched here:

C:\Folder1\A
C:\Folder1\C
C:\Folder2\B

You want all these files deleted and then copied from CDROM: to C:\CDBACKUP keeping the existing folder structure from the CDROM:?

C:\CDBACKUP\WhateverFolder\A
C:\CDBACKUP\WhateverFolder23\C
C:\CDBACKUP\WhateverFolder2WhateverFolder5\B

Or copied to C:\CDBACKUP

C:\CDBACKUP\A
C:\CDBACKUP\C
C:\CDBACKUP\B

The CDROM: could have 100's files but we're just copying the ones found and deleted from C: A,B and C right?
0
 

Author Comment

by:ankur3020
Comment Utility
no,u r right, cd can have even 100 files, we want basically to search all those file and checking if any ot them exist or not. and in case they exist, delete it and then copy all files from cd.
0
 

Author Comment

by:ankur3020
Comment Utility
Dear Mr.t0t0, please note the out comes.

1. I made a file with attached code and place it on c:\. with name sc.bat. i.e. (c:\sc.bat)
2. now i made a text file named 1.txt and placed on c:\ . i.e. c:\1.txt
3. i run the file and it serched 1.txt , deleted it and stops.
4. no copying of data.
5. then i placed 1.txt in d:\ , but code did not able to search it.
6. more over i can have more than 3 files, so code should check all files available at cd and then delete all found on hard disk.
7. f: is my cd rom drive.

pls check code and amend to suit the requirements.
0
 

Author Comment

by:ankur3020
Comment Utility
sorry i forgot to attach code. it is here
@echo off

 

set flag=0

 

cd \

 

for /r %%f in (1.txt) do (

   if exist %%f (

      del %%f

      set flag=1

   )

)

   

if %flag%==1 (

   echo Copying files from f: to C:\CD BACKUP

   xcopy f:\mission\*.* c:\cdbackup /s /e /v /c /q /h /r /y

)

 

set flag=0

Open in new window

0
 
LVL 21

Expert Comment

by:AmazingTech
Comment Utility
OK.

Here's a basic script that will take the filenames from cdrom all subfolders and uses the filenames to run DEL /s /f on the search target. Then a filesync from cdrom to C:\CDBackup.

Let me know if you want any changes.

From your first response to me do you want to delete only when the filename and size match?
Set SearchSource=F:

Set SearchTarget=C:\Files

Set CDBackup=C:\CDBackup

Set SearchForFilesLike=*.*
 

FOR /r "%SearchSource%" %%a in (%SearchForFilesLike%) DO ECHO DEL /s /f "%SearchTarget%\%%~nxa"
 

XCOPY /s /e /c /d /r /y "%SearchSource%" "%CDBackup%\%SearchForFilesLike%"

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 21

Expert Comment

by:AmazingTech
Comment Utility
Sorry. I put an ECHO for testing remove it to actually delete the file. Also the XCOPY was not limiting the copies to SearchForFilesLike.
Set SearchSource=F:

Set SearchTarget=C:\Files

Set CDBackup=C:\CDBackup

Set SearchForFilesLike=*.*
 

FOR /r "%SearchSource%" %%a in (%SearchForFilesLike%) DO DEL /s /f "%SearchTarget%\%%~nxa"
 

XCOPY /s /e /c /d /r /y "%SearchSource%\%SearchForFilesLike%" "%CDBackup%\*.*"

Open in new window

0
 

Author Comment

by:ankur3020
Comment Utility
i tested ur code. it is doing wonderful. but some need to be amended.

1. it search file in c drive only (i want it should search on all partitions)
2. i copied one file at d:\, but code did not delete that.
3. how to change location of backup folder, as u mentioned %cdbackup%,
4. when code runs, it display in cmd, i do not want it.
0
 
LVL 21

Expert Comment

by:AmazingTech
Comment Utility
Hmmm You don't want to see anything?

1. & 2. List out the partitions/folders you want to search to delete here:
Set SearchTarget=C:\Files;D:;"C:\More Files"
Separate them with a ; any folders with a space put them in quotes. Do not add a \ at the end of the path.

3. Change the CD backup folder here:
Set CDBackup=C:\CDBackup

4. Everything is suppressed.

If you want it to only delete and copy mp3 files. Change this:
Set SearchForFilesLike=*.mp3


@ECHO OFF

Set SearchSource=F:

Set SearchTarget=C:\Files;D:;"C:\More Files"

Set CDBackup=C:\CDBackup

Set SearchForFilesLike=*.*
 

FOR %%z in (%SearchTarget%) DO CALL :DELFILES %%z

CALL :SYNCCDROM

GOTO :EOF
 

:DELFILES

FOR /r "%SearchSource%" %%a in (%SearchForFilesLike%) DO DEL /s /f "%~1\%%~nxa" 2>NUL

GOTO :EOF
 

:SYNCCDROM

XCOPY /s /e /c /d /r /y "%SearchSource%\%SearchForFilesLike%" "%CDBackup%\*.*" >NUL

Open in new window

0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
AmazingTech

I welcome you to take this off my hands.

I tried to offer this user advice and admittedly some common sense on his part was expected, however I find it worrying that he continually moves the goalpost rather than be specific from the outset.

Your solution is far too elaborate however, this is not a problem as even the simplest of solutions would be above Ankur3020's understanding and any hope of expecting him to modify the code to suit his particular application is not likely to be an issue here.

Ankar3020 fails to be specific about his requirements and I find much time has been spent trying to help this user.

As far as I can make out, he wants to check his hard drives (he refers to more than one drive throughout this text - C: and D:. I wonder if there are others he hasn't yet mentioned) for the existence of files matched against files on a CD ROM designated F:

At this point, he does not say whether the files on the CD are in a single root folder or whether they are in subfolders.  Moreover, he does not state whether the location of files on the CD match the relative paths of the files on the hard drives.

He then states he wants to delete each file from his hard drive/s for which there is a matching file on the CD (however, the file F:\FolderA\FileA may not be the same as F:\FolderB\FileA, if such a scenario exists in his case - please think about the implications of that for just a moment to realise the point I'm trying to make)

Having deleted the file from the hard drive/s, he then wants to copy the matched file (although he actually says "all files" from the CD, and as he puts it "to specified location say c:\cdbackup". What is he actually saying here because "to A specified location" and "to THE specifed location" mean two different things (think about it)?

Does he mean the matched file is deleted from a specific location on the hard drive and then a file by the same name is then copied from the CD to the same specific location (ie from where the file was delleted - which would merely constitute a COPY as in replacing the file) or does he mean the file is deleted from the hard drive and a matching file is then copied from CD to a fixed 'specified' location such as c:\cdbackup? If the latter, he does not state whether he wants to retain the relative pathname of the file being copied to the hard drive.

Without knowing the full specifications of his requirements one is left having to make assumptions.

I note from your code you have used the /D switch in the XCOPY command, Under one particular application, this would have the effect of skipping an unmodified file and possibly losing it altogether in the generation chain. Please pause and think about the implications of this.

Also, where you asked the Ankar3020 if searching by date was necessary or not he merely replied name and size only (no confirmation of date). Which leads to two observations: 1) your code obviously does not differentiate between filesizes (not that I can see a reason why it should) and 2) you have used the /D switch when it's use is uneeded and could in fact cause problems at a later time given some circumstances.

It is ambiguities like these that make the process of providing assistance on EE tediusly frustrating and often a waste of effort and time.

If Ankar3020 would like to comment on any of the issues I have raised in this reply then he may do so providing he is capable of fully understanding those issues himself

Furthermore, if Ankar3020 would like to be more specific whle at the same time be less vague and less ambiguous about his requirements then it would reduce the amount of time wasted by experts.

Good luck AmazingTech....
0
 
LVL 21

Expert Comment

by:AmazingTech
Comment Utility
Hmmm. Not to worry. I do know exactly what I have put forward as a solution it's merely to see if it resembles close to what is wanted.

I know the size has not been accounted for yet. I can do that too but the script will take much longer to process.  I'll find out if it is actually necessary or for the sake of speed. What is currently posted may be just fine.

I think Ankur3020 is trying to learn and perhaps may find it difficult to convey exactly what is required. Nevertheless I don't really care about points. Just to help people any way I can. Points are just an added bonus.

By the way does it costs people points to post questions?
0
 

Author Comment

by:ankur3020
Comment Utility
Dear Amazing Tech.

your amended code is perfect and now doing all what is required. thanks a lot.

Pls. explain what things are presently compared by code like name, size,date of creation etc.

i wish if u have not added criteria for date of creation of file, pls add that too in comparison /search.

One more thing,  if i add e:\, in search target, and i run the same code on system where there is no e:\,will it give any error or work smoothly. if any error can come, pls add code so that error will not affect code process.

0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 200 total points
Comment Utility
Presently it will delete files based on name only.

You can add e: without the trailing \. It will of course not find the drive but it should not cause any problems.

Test out the attached code will delete files based on Filename and Size. Looks crazy doesn't it. I haven't tried it but it should work.

t0t0 was correct in the XCOPY using /d will only copy files that are newer in F:.
@ECHO OFF

Set SearchSource=F:

Set SearchTarget=E:;C:\Files;D:;"C:\More Files"

Set CDBackup=C:\CDBackup

Set SearchForFilesLike=*.*

 

FOR %%z in (%SearchTarget%) DO CALL :DELFILES %%z

CALL :SYNCCDROM

GOTO :EOF

 

:DELFILES

for /r "%SearchSource%" %%a in (%SearchForFilesLike%) DO for /f "tokens=*" %%b in ('dir /a-d /b /s "%~1\%%~nxa"2^>NUL') do if "%%~zb" == "%%~za" DEL /q /f "%%b" 2>NUL

GOTO :EOF

 

:SYNCCDROM

XCOPY /s /e /c /d /r /y "%SearchSource%\%SearchForFilesLike%" "%CDBackup%\*.*" >NUL

Open in new window

0
 

Author Comment

by:ankur3020
Comment Utility
perfect. thanks a lot.u r genius.  request you to pls look at my another query posted in ur section regarding pop up message when some one access cd rom.

0
 

Author Closing Comment

by:ankur3020
Comment Utility
great work
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
AmazingTech, you know it costs people points to post questions.... Why do you ask?
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
Well, there's a surprise...

I should have updated my browser before posting my previous reply nevertheless, I'm glad for all concerned Ankur3020 has accepted our combined solution.
0
 
LVL 21

Expert Comment

by:AmazingTech
Comment Utility
I've never posted a question so I was wondering why sometimes people offer up 500 points and other times it's less. I want to answer the unanswered questions but found that for my efforts I find too often the question never gets responded by the author and it gets closed by the moderator.
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
That often happens when someone new learns of EE, usually when they are in urgent need of a solution to a problem, and they are willing to chuck their entire 500 points at a question. In some case they fail to return to close a question as they may have also sought advice elsewhere. Sometimes it's preferable to answer questions of less than 500 points for this reason. Having said that, I'm no expert myself.




0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now