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

want to move files of certain extension to another folder

Status
Not open for further replies.

needscoop

Programmer
Nov 13, 2002
29
US
Is there a quick way to a "blanket" move all files of a certain extension to another directory. This is for archiving purposes.

Example: move all *.xls files from C:\Current to C:\Archive

I've used File.Move to move a specific file, and used
MoveTo() to move all files from an existing folder to a
new (non-existent) folder, but I want to move just
files of a particular extension to another folder
(where the destination folder already exists).


T.I.A.!
 
Yes, there is a way to do it but essentially it uses the method you described in a loop.

Basically, you get the list of files in the directory and move the ones which have the appropriate extension:

DirectoryInfo dirInfo = new DirectoryInfo("D:\\temp");
if ( dirInfo.Exists )
{

FileInfo[] l_files = dirInfo.GetFiles();

// Loop through the files
foreach (FileInfo l_file in l_files)
{
if (l_file.Extension == ".xyz")
{
//do what you need to do
}
}
} ---
dino
 
your loop approach (which I had hoped to avoid) worked fine, thanks!
 
Yeah, I don't know of any way to do it as a group except to encapsulate the above code which would be pretty easy to do... ---
dino
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top