Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Files Based on Content

Status
Not open for further replies.

tbohon

Programmer
Apr 20, 2000
293
US
I have a directory with 1000 or so files and need to copy only those containing the phrase Med Admin Rpt to another directory. This is a one-time, 'hurry up' thing and, of course, I'm under the gun.

Any help/thoughts appreciated.

Tnx.

Tom

(Note: similar post in Unix scripting forum - doesn't matter how I get this done, the boss just wants it 'yesterday'!)

"My mind is like a steel whatchamacallit ...
 
well.. what code have you tried?
This is tek-tips not tek-letmedoallyourworkforyou.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I've tried several .ksh approaches and am working on my 2nd Perl approach.

Never mind - with your attitude I'll figure it out myself.

"My mind is like a steel whatchamacallit ...
 
This issue has been resolved.

"My mind is like a steel whatchamacallit ...
 
You got fired?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Never mind - with your attitude I'll figure it out myself.

Always the best solution.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Glad I could help :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
If your boss wanted it done yesterday, he should have asked you yesterday ...

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Although, my mates comments are quite amusing, figured I'd throw this out there for shits and giggles:

Code:
[gray]#!/usr/bin/perl[/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]File::Copy[/green] [red]qw([/red][purple]move[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]File::Spec::Functions[/green] [red]qw([/red][purple]catfile[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$srcdir[/blue] = [red]'[/red][purple]/foo[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]$destdir[/blue] = [red]'[/red][purple]/bar[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]$pattern[/blue] = [red]'[/red][purple]Med Admin Rpt[/purple][red]'[/red][red];[/red]

[url=http://perldoc.perl.org/functions/opendir.html][black][b]opendir[/b][/black][/url][red]([/red]DIR, [blue]$srcdir[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't opendir [blue]$srcdir[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[olive][b]while[/b][/olive] [red]([/red][black][b]my[/b][/black] [blue]$file[/blue] = [url=http://perldoc.perl.org/functions/readdir.html][black][b]readdir[/b][/black][/url][red]([/red]DIR[red])[/red][red])[/red] [red]{[/red]
	[black][b]my[/b][/black] [blue]$filesrc[/blue] = [maroon]catfile[/maroon][red]([/red][blue]$srcdir[/blue], [blue]$file[/blue][red])[/red][red];[/red]

	[olive][b]next[/b][/olive] [olive][b]if[/b][/olive] ! [url=http://perldoc.perl.org/functions/-X.html][black][b]-f[/b][/black][/url] [blue]$filesrc[/blue][red];[/red]
	
	[black][b]my[/b][/black] [blue]$matches[/blue] = [fuchsia]0[/fuchsia][red];[/red]
	[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red][black][b]my[/b][/black] [blue]$fh[/blue], [blue]$filesrc[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't open [blue]$filesrc[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
	[olive][b]while[/b][/olive] [red]([/red]<[blue]$fh[/blue]>[red])[/red] [red]{[/red]
		[blue]$matches[/blue]++, [olive][b]last[/b][/olive] [olive][b]if[/b][/olive] [red]/[/red][purple][purple][b]\Q[/b][/purple][blue]$pattern[/blue][purple][b]\E[/b][/purple][/purple][red]/[/red][red];[/red]
	[red]}[/red]
	[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url][red]([/red][blue]$fh[/blue][red])[/red][red];[/red]
	
	[olive][b]if[/b][/olive] [red]([/red][blue]$matches[/blue][red])[/red] [red]{[/red]
		[black][b]my[/b][/black] [blue]$filedest[/blue] = [maroon]catfile[/maroon][red]([/red][blue]$destdir[/blue], [blue]$file[/blue][red])[/red][red];[/red]
		[maroon]move[/maroon][red]([/red][blue]$filesrc[/blue], [blue]$filedest[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't move: [blue]$![/blue][/purple][red]"[/red][red];[/red]
	[red]}[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/closedir.html][black][b]closedir[/b][/black][/url][red]([/red]DIR[red])[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]File::Copy - Copy files or filehandles[/li]
[li]File::Spec::Functions - portably perform operations on file names[/li]
[/ul]
[/tt]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top