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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving files from one directory to another

Status
Not open for further replies.

wdellin

MIS
Joined
Feb 8, 2002
Messages
36
Location
US
Sorry if this has been answered but I have not found an example for what I'm trying to do.

I'm trying to:

1. Get a list of all filenames(not directories or . and ..) currently in a directory.
2. Use the contents of that list to move just those files to another directory.

This seems so easy, maybe I'm just making it hard. I cannot see how to get just the filenames using the dir command. I know I need to do a while loop to get thru the list once I have it.

Any help or direction would be greatly appreciated.

 
Code:
$dir="/what/ever/"
opendir DIR $dir;
@files=readdir DIR;
closedir DIR;

foreach $file (@files) {
  if (-f "$dir$file") {  # true if $dir$file is a regular file
    #move $file, use a system command, or if the file is to moved on the same drive use perl's rename command
  }
}
not tested, btw

HTH
--Paul


It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
Paul

Thank you for your help. I'm not sure if I explained this scenario clearly. I'm trying to read all files when I start so only those files, not new ones being written to the directory while I'm running get's moved.

Will the opendir or readdir, read all files in the directory at once? In perl can I pipe the output to a file and loop thru that file for the file names to move?

I guess I'm trying to get a snap shot of the directory at a certain time and move only the files in the snap shot while other files are still being written to the directory.

thanks
 
@files=readdir DIR will read all the filenames in the current directory, including . and ..
open FH2 ">filelist.txt";
foreach(@files) {print FH2 "$_\";}
close FH2;

You could stillend up with locked files though

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
Another option would be to use the file timestamp to solve your problem. You could simple store the current time of the system in a variable and any files with a timestamp greater than that file will be ignored.

Just another look on things


haunter@battlestrata.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top