Search Contents and Move

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Search Contents and Move

Post by alanimal » Thu Jan 29, 2009 10:28 pm

Hi there,

I would like to do this if possible.

Search a folder containing thousands of .eml files.
If they have the text "RRR_" within their contents, I want to have them moved to a different folder.

e.g. Search C:\Temp
If any *.eml files contain the text "RRR_"
Move the files to C:\sorted.

There could be several thousand files.

File example: C:\TEMP\asdfgr123a2grl.eml (the file names are random characters)
Criteria: any .EML file containing "RRR_"

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Thu Jan 29, 2009 10:51 pm

Quick thought:
Use the Dos FIND command and use RunProgram to call the batch file, and use Macro Scheduler commands to parse the results and move the files between directories.

RunProgram> cmd /c find /C "RRR_" c:\temp\*.eml > filelist.txt

This will result with a list of files (filelist.txt) that have "RRR_". They will have a number >0 after the colon. The number represents the number of lines that contain the string. Here is a sample extract

Code: Select all

---------- FIREFOX.EXE_TRACE.TXT: 0

---------- GETENVVALUE.TXT: 0

---------- TEMP.TXT: 1

---------- TEST.TXT: 1
Now parse the filelist.txt for those lines with number >0
Use Position> and MidString> to extract the file names and write to a variable that will be a list of the files to Move.

Now just use normal Move command
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Jan 29, 2009 11:01 pm

Here's another way. A quick and dirty sample script. Bob's DOS method will work as well. I have no idea which way will be faster. With 1000s of files speed may be a concern.

Code: Select all

//Set thesethree lines as needed
Let>FileLoc=C:\temp\*.eml
Let>FileDest=C:\sort\
Let>TextToFind=RRR_

GetFileList>%FileLoc%,list
Separate>list,;,item
Let>kk=0
Repeat>kk
  Add>kk,1
  Let>pathfilename=item_%kk%
  Separate>pathfilename,\,fname
  Let>filename=fname_%fname_count%
  Readfile>pathfilename,result
  Separate>result,TextToFind,test
  If>%test_count%>1
    Let>CF_OVERWRITE=1
    CopyFile>pathfilename,%FileDest%%filename%
    Message>copying %pathfilename% to%CRLF%%FileDest%\%filename%
  EndIf
Until>kk,%item_count%
Last edited by JRL on Thu Jan 29, 2009 11:03 pm, edited 1 time in total.

alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Post by alanimal » Thu Jan 29, 2009 11:03 pm

thanks - does this look correct? for first part?

RunProgram>cmd /c find /c "PCC_" C:\Inetpub\mailroot\Drop\*.eml >filelist.txt

alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Thanks JRL

Post by alanimal » Thu Jan 29, 2009 11:15 pm

I have tried below = but the files are not being copied across to the dest folder? can you please help me?

by the way your a legend!
[/code]

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Fri Jan 30, 2009 12:12 am

Enhancing Bob's suggestion, this DOS command line only prints to the file the names of the files containing the string (so you don't need the second search step).

Code: Select all

RunProgram>cmd /c findstr /i /s /m "rhubarb" c:\temp\*.txt>c:\temp\tart.txt
Note that there are no spaces before or after the ">".

alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Hey JRL

Post by alanimal » Fri Jan 30, 2009 12:35 am

YUS! got it working however:

I run the script and the system has an error saying it runs out of memory.

There are 40,000 email files it has to go through.

Any ideas?
thanks in advance

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Fri Jan 30, 2009 12:42 am

How many will it handle? Do you think it would it work if you did the files in batches by first letter, a*.eml b*.eml etc. ? If so we could make a loop to do that.

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Fri Jan 30, 2009 3:33 am

Here's another idea, this one scans the files one by one and if it finds the target text writes the name of the file to a txt file. This would be a good way to test if it is going to work before you run a version that actually moves the files, you can check the text file and it will show you the list of files that would be moved. I suggest you try it with a subset of files eg a*.eml because scanning 40,000 files is going to take a very long time.

Code: Select all

Let>RP_WAIT=1
DeleteFile>c:\temp\flist.txt
GetFileList>c:\temp\*.txt,files
Separate>files,;,file_names
If>file_names_count=0,end
Let>k=0
Repeat>k
Let>k=k+1
DeleteFile>c:\temp\tart.txt
let>myfile=file_names_%k%
RunProgram>cmd /c findstr /i /s /m "rhubarb" %myfile%>c:\temp\tart.txt
FileSize>c:\temp\tart.txt,size
If>size=0,jump
Readln>c:\temp\tart.txt,1,datafile
WriteLn>c:\temp\flist.txt,res,datafile
Label>jump
Until>k,file_names_count
Label>end


User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Fri Jan 30, 2009 4:34 am

Hey ME_again. Thanks for the reminder about findstr. I am still thinking DOS 3.0, and forgot about the newer command. :oops:

:idea: Remember edlin?

Both FIND and FINDSTR will result in a list of the files almost immediately, but the FINDSTR will be better, needs no parsing to reduce the list, I like it!


And that was a nice touch associating rhubarb with tart. 8)
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Fri Jan 30, 2009 5:59 am

Bob Hansen wrote:And that was a nice touch associating rhubarb with tart. 8)
That was for Marcus, one of the foundations of British cuisine :lol:

alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Rhubarb and Tart

Post by alanimal » Tue Feb 03, 2009 1:49 am

Hi Me_Again,

Its me again hehe

I have tried the code you supplied. Thanks Heaps. I am trying to avoid the memory error I am receiving with the other option.

My .eml files are located C:\Inetpub\mailroot\mail\
An example of a file name is ddf23122ee33404f.eml (totally random)
an example of the contents of one of these .eml files is as follows:

Code: Select all

%A2%B       <b>
%C290120090833%D00%E08%G015526%O010270%Q000000%P0158%R000000%S0000%T000000%U0000%V010270%W00052920%X00037130%Y0%z5D8C
As you can see it is the "PCC_" that I am wanting to filter by.

Code: Select all

Let>RP_WAIT=1
DeleteFile>C:\Inetpub\mailroot\mail\test\flist.txt
GetFileList>C:\Inetpub\mailroot\mail\*.eml,files
Separate>files,;,file_names
If>file_names_count=0,end
Let>k=0
Repeat>k
Let>k=k+1
DeleteFile>C:\Inetpub\mailroot\mail\test\tart.txt
let>myfile=file_names_%k%
RunProgram>cmd /c findstr /i /s /m "PCC_" %myfile%>C:\Inetpub\mailroot\mail\test\tart.txt
FileSize>C:\Inetpub\mailroot\mail\test\tart.txt,size
If>size=0,jump
Readln>C:\Inetpub\mailroot\mail\test\tart.txt,1,datafile
WriteLn>C:\Inetpub\mailroot\mail\test\flist.txt,res,datafile
Label>jump
Until>k,file_names_count
Label>end

I run the above, and the only thing that happens is the command prompt (cmd.exe) is executed by macro scheduler and it just sits there doing not much else!

Any ideas?

(i wish I could come up with code as quick as you guys!)
cheers
AM from New Zealand
[/code]

alanimal
Junior Coder
Posts: 40
Joined: Thu Jun 09, 2005 11:57 pm

Post by alanimal » Tue Feb 03, 2009 2:05 am

where the is for the email contents - it is supposed to be - forum has chomped it up

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Tue Feb 03, 2009 2:30 am

1. On the forum Disable HTML Code and see if that fixes the coding errors on the posting.

2. If you did cut/paste from forum be sure to remove trailing spaces that are not wanted.

3. Have you tried to single step through the code?
Using the WatchList, are the variables what you expect them to be?

4. What OS do you have? If using WIN9x, you may need to replace cmd with command.com
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts