Link to home
Start Free TrialLog in
Avatar of lucour
lucour

asked on

Copy a File based on Find results

Hi,

I have a directory called C:\Bankfiles.  Each day this directory is populated with new .txt files.  The file names are never the same since these are validation reports, and thus generated by the bank.  One day a file name could be jgh10042.txt and the next day it could be ght44589.txt.  Here is what I want to do.  Search inside all of these .txt files in C:\Bankfiles for  customer number "TDCUST022" and whichever file contains this search string, copy it to a folder called C:\CustFound.  So far I have tried the DOS Find command:

Find /c "TDCUST022" C:\BankFiles

This produces the following:


D:\BatTest>find /c "TDMAR10202" D:\Battest\*.txt


---------- C:\BankFiles\JKH47226.TXT: 0

---------- C:\BankFiles\JOH43285.TXT: 0

---------- C:\BankFiles\JOH55272.TXT: 1

So as you can see, the last file (JOH55272.TXT) is the file I want to copy to C:\CustFound. How do I do this ?
Avatar of rin1010
rin1010


lucour,

The Find command returns an exit code signifying whether a search is successful,
which can then be tested using the IF command and the ErrorLevel parameter...
Further commands (e.g., copying files, etc.) can then be processed
depending on the results of the returned exit code...


Here are exit codes returned by Find :

  0  =  Search completed successfully... At least one match found...

  1  =  Search completed successfully... No matches found...

  2  =  Search not completed successfully... Error occurred...


You'll probably need to experiment with it
but methods using an example similar to
the commands you posted would be like:


Find /c "TDMAR10202" C:\BankFiles\JOH55272.TXT
if not errorlevel 1 copy C:\BankFiles\JOH55272.TXT C:\CustFound

...or:

Find /c "TDMAR10202" C:\BankFiles\JOH55272.TXT
if errorlevel 0 copy C:\BankFiles\JOH55272.TXT C:\CustFound


Post back if you want more info on it
or if you don't get the desired results.
 
Avatar of lucour

ASKER

Thanks for the help, rin1010.  However, the info. you gave me I was aware of.  The pickle I am faced with is that the file name is never the same each day.  On this particular day JOH55272.txt contained "TDMAR10102".  However, the next day the file will be a different name.  The only constant is that they are always text files.  I need to identify which of the 4 files in the directory contains this info. and then copy this file only to C:\CustFound.  Do I need a variable setup maybe ?

Thanks again !!
OK I have your problem licked.

Go to my site and download my utility Recurse.  It allows you to iteratively process items in DOS in ways that are difficult or impossible (if you're using real DOS) to do otherwise.

http://ped.deadartists.com/recurse.zip

Extract the files and copy the recurse.exe to a location in your path - ex C:\WINDOWS\COMMAND for Win9X OS and C:\[WINNT|WINDOWS]\SYSTEM32 for NT/2K/XP.

Test that the recurse file is accessible by executing "recurse" at a DOS prompt without specifying the fully-qualified path or CDing to the dir it's in first.  You should receive a popup with syntax help.

Now on to the solution:

Assumptions: We're using the dirs you mentioned above (C:\Bankfiles for the source files and C:\Custfound for the place to copy the files to when they contain the customer number we seek).  Also I am assuming that the BAT files I am creating will be stored at C:\BATCH but you can modify that to suit your environment.


First create a BAT file called C:\BATCH\BANKRUN.BAT and make it contain the following code:

@echo off
dir /b c:\bankfiles\*.txt > c:\process.txt
recurse c:\batch\process.bat c:\process.txt

Next create a second BAT file called C:\BATCH\PROCESS.BAT and make it contain the following code:

@echo off
find /i "TDMAR10202" c:\bankfiles\%1 >nul
IF %ERRORLEVEL%==0 copy c:\bankfiles\%1 c:\custfound

You may want to include a "/y" parameter in the copy command since files will match the next day too if you don't get rid of them, and then you'll be copying over the same file in the next day's move.  The last line would change to look like this if you take that into account and would like to copy over the same-named files on the subsequent runs:

IF %ERRORLEVEL%==0 copy /y c:\bankfiles\%1 c:\custfound

That's it - the BANKRUN.BAT is the master BAT file - simply run it (or schedule it to run) when you want to process the files and the ones that contain your customer number will be moved as you described.

--
Paul Doherty
http://ped.deadartists.com
DOS/Windows Utilities
ASKER CERTIFIED SOLUTION
Avatar of pauldoherty
pauldoherty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lucour

ASKER

Thank you so much, pauldoherty !!!  I just tried this and it works like a charm.  Thanks again.
Paul,

I have used your solution myself to move/file my systems' log files.

It works fine, but is very very slow as my list of 'find...' is very big.

Is there any way to make it faster?  I was thinking that the process.txt file could contain only the find results, but can't seem to get it working, or the syntax right.

Could you offer any advice?


Thanks,

Simon Hawking
Email me with the contents of your files and what you're doing and I'll see if I can help.  

Paul Doherty
bitbucket911 '@' attbi.com
An FYI to anyone who may be reading this now - my website where Recurse can be found is http://powerusers.info - the link above still works now but will stop working in a few months.
Avatar of lucour

ASKER

Thanks to all who suggested Recurse.  This works like a charm.  I think that my problems are solved.    Thanks !