📄 copy folder test.htm
字号:
<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">
<head>
<!--[if gte mso 9]><xml><mso:CustomDocumentProperties>
<mso:Approval_x0020_Level msdt:dt="string"></mso:Approval_x0020_Level>
<mso:Categories msdt:dt="string">Utilities;Delphi Techniques</mso:Categories><mso:Assigned_x0020_To msdt:dt="string"></mso:Assigned_x0020_To></mso:CustomDocumentProperties></xml>
<![endif]-->
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Copy Folder Test</title>
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<!--mstheme--><link rel="stylesheet" type="text/css" href="../_themes/kidsplainbanner2/kids1110.css"><meta name="Microsoft Theme" content="kidsplainbanner2 1110, default">
<meta name="Microsoft Border" content="tlb">
</head>
<body><!--msnavigation--><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td>
<p align="center">
<img border="0" src="../images/ChildDelphi.gif" align="left" vspace="20" width="146" height="113"><font size="6"><strong><img src="_derived/CopyFolder.htm_cmp_kidsplainbanner2110_bnr.gif" width="600" height="60" border="0" alt="Copy Folder Test"></strong></font></p>
<h4 align="center"><br clear="left">
[<a href="../../default.html">Home</a>] [<a href="../index.htm">Projects</a>]
[<a href="../Delphi_Techniques/index.htm">Delphi Techniques</a>] [<a href="../Math_Topics/index.htm">Math
Topics</a>] [<a href="../Library/Default.htm">Library</a>]
[<a href="index.htm">Utilities</a>]</h4>
</td></tr><!--msnavigation--></table><!--msnavigation--><table dir="ltr" border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td valign="top" width="1%">
<p> </p>
<div align="left">
<table cellPadding="5" width="25%" border="0">
<tr>
<td width="150">
<p align="left">
<img height="10" src="../_borders/bd14710_.gif" width="187" border="0"></p>
<p align="left"><b>Available Now</b></p>
<ul>
<li>
<p align="left"><b>
<a href="../Indices/ProgramIndex.htm" target="_blank">All
Programs</a></b>
<ul style="margin-left: 8px">
<li><a href="../Indices/projectsIndex.htm">Puzzles &
Projects</a></li>
<li><a href="../Indices/Delphi_TechniquesIndex.htm">Delphi
Techniques</a></li>
<li><a href="../Indices/math_topicsIndex.htm">Math Topics</a></li>
<li><a href="../Indices/UtilitiesIndex.htm">Utilities</a></li>
<li><a href="../Indices/LibraryIndex.htm">Library Units</a></li>
</ul>
</li>
</ul>
<p style="MARGIN-TOP: 0px; MARGIN-LEFT: 0px">
<img height="10" src="../_borders/bd14710_.gif" width="187" border="0"></p>
<p align="left"><font size="4">Contact</font></p>
<p><font color="#0000ff" size="2"><a href="../../Feedback.htm">
Feedback</a>:</font><font face="Arial" color="#000000"> </font>
<font face="Arial" color="#000000" size="2">Send an e-mail with your
comments about this program (or anything else).</font></p>
<p><img height="10" src="../_borders/bd14710_.gif" width="200" border="0"></p>
<p>
<!--
//<form method="GET" target="_blank" action="http://www.google.com/search">
// <p style="margin-bottom: 0"><img border="0" src="http://www.google.com/logos/Google_Safe.gif" width="115" height="45"></p>
// <p style="margin-top: 0; margin-bottom: 0"><input type="text" name="q" size="25"> </p>
// <p style="margin-top: 6"><input type="submit" value="Search" name="sa"></p>
// <input type="hidden" name="safe" value="vss"><input type="hidden" name="vss" value="1">
//</form>
--></p>
<p>
<!-- Search Google --><a name="Search"></a>
<left></p>
<form action="http://www.google.com/custom" method="get">
<table cellSpacing="0" bgColor="#ffffdd" border="1" width="188">
<tr vAlign="top">
<td width="201">
<a href="http://www.google.com/search?safe=vss">
<img height="53" alt="Google" src="http://www.google.com/logos/Google_Safe.gif" width="128" align="middle" border="0"></a></td>
</tr>
<tr>
<td width="201">
<input maxLength="222" size="27" name="q">
<input type="submit" value="Google Search" name="sa">
<input type="hidden" value="BGC:ffffdd;AH:center;GL:1;S:http://www.delphiforfun.org;AWFID:87962d146996e3f8;" name="cof">
<font face="arial,sans-serif" size="-1">
<input type="hidden" value="delphiforfun.org" name="domains">
<p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">
<input type="radio" value name="sitesearch"> Search WWW</p>
<p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">
<input type="radio" CHECKED value="delphiforfun.org" name="sitesearch">
Search delphiforfun.org</p>
</font>
<p> </td>
</tr>
</table>
</form>
<p>
<!-- Search Google --></p>
<p><img height="10" src="../_borders/bd14710_.gif" width="200" border="0"></left></td>
</tr>
</table>
</div>
<p> </p>
</td><td valign="top" width="24"></td><!--msnavigation--><td valign="top"><table border="0" cellpadding="0" width="100%" cellspacing="0">
<tr>
<td width="73%" valign="top">
<h3 align="left">Problem Description</h3>
<p><font color="#000000">Required: A program to copy all of the files
matching a file mask to a different folder. </font></p>
<h3>Background & Techniques</h3>
<p>I recently had need for a procedure to copy all files in a CD folder to a new folder on a hard disk drive
and reset the "ReadOnly" attributes. . </p>
<p>This program was written to test the resulting new "CopyFolder" function, but it
seems that the program has enough features to perhaps serve as a useful utility for
non-programmers..<br>
<br>
Usage is straight-forward: Select an input folder and an output folder.
The output folder may be existing or a new one. Other
options include: </p>
<ul>
<li>Set a file mask to control which files get selected. A
separate "file masks" page defines masking string options and provides a
few examples. </li>
<li>Set the scope of the operations - files in the input folder only or input folder plus all files in folders contained within the input
folder.</li>
<li>Whether to retain or reset the "readonly" attribute on the
copied files.</li>
<li>How to handle duplicate files, i.e. files already existing in the
target folder. options are<ul>
<li>Ignore the file, do not copy.</li>
<li>Copy the file, replacing the existing file in the the target
folder.</li>
<li>Copy the file only if the source file is newer than the target.</li>
<li>Ask the user whether to overwrite the existing target file.</li>
</ul>
</li>
</ul>
<p>Click the "Copy" button to complete the operation. A list of files copied and the
total count will be displayed when the operation has been completed.<br>
<br>
You may interrupt the operation by clicking the "Stop" button but the copies
of files already processed will remain in the output folder.
If you select "Ask" option for the Duplicate Files radio group, a reply of
"Cancel" when asked will also stop the operation.<br>
</p>
<p><font color="#000000">Non-programmers are welcome to read on, but may
want to <a href="#Download">skip to the bottom of this page</a> to download
executable version of the program.</font></p>
<h4><b><font color="#000000">Notes for Programmers</font></b></h4>
<p>The program calls the "<b>CopyFolder</b>" function contained in unit <b>
UCopyFolder</b>.pas to do most of the work. <b>CopyFolder</b>
copies files matching a given mask from one folder to another. Parameters
are;</p>
<ul>
<li><b>FromFolder</b>: The path to the folder containing the files to be
copied</li>
<li><b>ToFolder</b>: The path to the folder to receiver the files</li>
<li><b>Mask</b>: A file mask to control which files are selected ('*.*' =
All files)</li>
<li><b>DupFileOpts</b>: Four numeric options are available when the file already
exists:<ul>
<li>0 ==> skip the file</li>
<li>1 ==> always copy the file</li>
<li>2 ==> copy the file if it is newer than the existing copy</li>
<li>3 ==> ask the user what action to take</li>
</ul>
</li>
<li><b>CopySubFolders</b>: Files in subfolders of the specified <b>
FromPath</b> folder will also be copied if this parameter is true.</li>
<li><b>ResetReadOnly</b>: If true, input files marked as "Readonly" will
have that attribute removed in the target location.</li>
<li><b>FileExit</b>: The address of optional method (function of object)
specifying a user function to be called before each file is copied.
If the callback procedure is specified, it receives 4 parameters:<ul>
<li><b>Inpath</b>: Path to the input file.</li>
<li><b>OutputPath</b>: Path where the file will be copied.</li>
<li><b>Filename</b>: Name of the file to be copied.</li>
<li><b>Cancopy</b>: Boolean parameter with a default value of true. Set "<b>Cancopy</b>"
to false to skip copying this file.</li>
</ul>
</li>
</ul>
<blockquote>
<p><b>FileExit</b> should return a result value of true if copying is to continue, and
false to abort the copy procedure without copying further files.</p>
</blockquote>
<p> </p>
<p><b>CopyFolder </b>uses a few techniques worth describing for future
reference</p>
<ul>
<li><font color="#000000"><b>Recursive search through a folder and
subfolders </b>is a technique we have used before but filtering files
with a file mask makes the procedure a little more complicated.
The <b>FindFirst</b> function can filter returned records based on a
specified file mask, but since we will likely be recursively calling <b>
CopyFolder</b> with subdirectories of the input directory, <b>
FindFirst</b> must search all file names (with a file name mask of
"*.*")</font></li>
<li><font color="#000000"><b>File mask matching</b> - <b>FindFirst</b>
and successive <b>FindNext</b> calls retrieve file information in
a <b>TSearchrec</b> for each call including the file name, date,
attributes . The Name field is passed to the
<b>MatchesMask</b> function along with the user specified mask. I
could find no user oriented description of mask strings, so I added a tab
sheet to the program with the mask fields described and a few examples. </font></li>
<li><font color="#000000"><b>Overloading a function or procedure with an
optional method call </b>- I decided to provide an optional callback
function passed as a parameter to CopyFolder. The callback
function is called for each file to be copied, passing the
parameters described above. The typical way to make a parameter
optional is to provide an overloaded version without the optional
parameter and from there call the "real" version with a default
value for the missing parameter. (Delphi recognizes the keyword "<b>overload</b>"
in the procedure definition and will accept multiple definitions with
the same name so long as the parameter lists are different.) The tricky part here is that we
cannot pass a "Nil" (no address) value for the callback function because
the function is defined as a method type function "of object"
meaning that it is a method of an object. Method type
functions are passed as a pair of pointers, a pointer to the function
code and a a pointer to the class instance to which it belongs.
I got around the problem by defining a <b>DummyFileExit</b> function
within a <b>TDummyClass</b> definition. And an instance if <b>
TDummyClass </b>name<b>d DummyClass</b>. This <b>DummyfFileExit</b>
may now be passed as the default <b>FileExit</b> for cases where the
user chose not to provide one. <b>CopyFile</b> checks and ignores
any reference to <b>DummyFileExit</b>, so it is not necessary to
actually create the <b>DummyClass</b> instance. Whew!
</font></li>
<li><font color="#000000"><b>Changing file attributes</b> - I only way i
could find to reset the <b>ReadOnly</b> attribute was to copy the (using
the Windows <b>CopyFile</b> funtion) and then use the <b>SetFileAttr</b>
function to reset the attribute if in was set in the input. </font></li>
</ul>
<h3><a name="Download"></a>Running/Exploring the Program </h3>
<ul>
<li>
<font color="#000000"><a href="../Download/CopyFolderTestSource.zip">Download source</a></font><a href="../Download/MonitorOffSource.zip">
</a>
</li>
<li>
<font color="#000000"><a href="../Download/CopyFolderTest.zip">Download executable</a></font><a href="../Download/MonitorOff.zip">
</a>
</li>
</ul>
<h3>Suggestions for Further Explorations</h3>
<p><font color="#000000">I Is there a more direct way to make a method
pointer an optional parameter in a function call?</font></p>
<p> </p>
<div align="left">
<table border="0" cellpadding="2" width="100%">
<tr>
<td width="50%"><b><font size="2">
Original Date: October 30 2006</font></b></td>
<td width="50%">
<p align="right"><b><font size="2">Modified:
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%B %d, %Y" startspan -->February 01, 2007<!--webbot bot="Timestamp" i-checksum="41395" endspan --> </font></b></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<p> </p>
<!--msnavigation--></td></tr><!--msnavigation--></table><!--msnavigation--><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td>
<hr>
<h5 align="center"><font color="#0000FF"> [<a href="../../Feedback.htm">Feedback</a>]
[<a href="../../newsletter.htm">Newsletters (subscribe/view</a>)] [</font><font color="#330033"><a href="../../about_me.htm">About
me</a></font><font color="#0000FF">]</font></h5>
<h5 align="center"><font color="#000080">Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -