⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 faq18.htm

📁 C++builder学习资料C++builder
💻 HTM
字号:


<HTML>

<HEAD>

   <TITLE>Delete or move files while displaying the flying folders.</TITLE>

   <META NAME="Author" CONTENT="Harold Howe">

</HEAD>

<BODY BGCOLOR="WHITE">



<CENTER>

<TABLE  BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">



<TR>

<TD>









<H3>

Delete or move files while displaying the flying folders.

</H3>

<P>

 You can easily copy, delete, or move files by calling the API functions

<TT>CopyFile</TT>, <TT>DeleteFile</TT>, and <TT>MoveFile</TT>. These functions are

pretty easy to use, but they do <B>not</B> display the flying folders. Here are

examples of each.

</P>

<pre>

<font color="navy">// copy SOURCE.TXT to DEST.TXT, the FALSE argument tells windows</font>

<font color="navy">// to fail if DEST.TXT already exists. CopyFile returns BOOL</font>

<b>if</b><b>(</b> CopyFile<b>(</b><font color="blue">"C:\\SOURCE.TXT"</font><b>,</b><font color="blue">"C:\\DEST.TXT"</font><b>,</b> FALSE<b>)</b> <b>==</b> FALSE<b>)</b>

    Application<b>-></b>MessageBox<b>(</b><font color="blue">"CopyFile failed"</font><b>,</b> <font color="blue">"Error"</font><b>,</b> MB_OK<b>)</b><b>;</b>



<font color="navy">// MoveFile and DeleteFile also return FALSE if failure.</font>

MoveFile<b>(</b><font color="blue">"C:\\SOURCE.TXT"</font><b>,</b><font color="blue">"C:\\WINDOWS\\TEMP\\DEST.TXT"</font><b>)</b><b>;</b>

DeleteFile<b>(</b><font color="blue">"C:\\SOURCE.TXT"</font><b>)</b><b>;</b>

</pre>

<P>

These functions are easy to use, but unfortunately they don't display the flying folders.

In order to get the folders, you must use the <TT>SHFileOperation</TT> API function. The same

examples that are shown above have been duplicated below to use <TT>SHFileOperation</TT>.

Notice that the move and copy commands can specify a destination directory.

</P>

<pre>

<font color="navy">// make sure to include the file SHELLAPI.H. Its located in the</font>

<font color="navy">// INCLUDE\WIN32 file if you're curious.</font>

<font color="green">#include &lt;shellapi.h></font>



<font color="navy">// copy SOURCE.TXT to DEST.TXT;</font>

<b>char</b> <b>*</b>From <b>=</b> <font color="blue">"C:\\SOURCE.TXT\0"</font><b>;</b>

<b>char</b> <b>*</b>To   <b>=</b> <font color="blue">"C:\\DEST.TXT\0"</font><b>;</b>

SHFILEOPSTRUCT op<b>;</b>

ZeroMemory<b>(</b><b>&</b>op<b>,</b> <b>sizeof</b><b>(</b>op<b>)</b><b>)</b><b>;</b>

op<b>.</b>hwnd  <b>=</b> Handle<b>;</b>  <font color="navy">// Handle of main form or the application</font>

op<b>.</b>wFunc <b>=</b> FO_COPY<b>;</b>

op<b>.</b>pFrom <b>=</b> From<b>;</b>

op<b>.</b>pTo   <b>=</b> To<b>;</b>

op<b>.</b>fFlags<b>=</b> <font color="blue">0</font><b>;</b>

SHFileOperation<b>(</b> <b>&</b>op<b>)</b><b>;</b>



<font color="navy">// move SOURCE.TXT the windows temp directory</font>

<b>char</b> <b>*</b>From <b>=</b> <font color="blue">"C:\\SOURCE.TXT\0"</font><b>;</b>

<b>char</b> <b>*</b>To   <b>=</b> <font color="blue">"C:\\WINDOWS\\TEMP\0"</font><b>;</b>

SHFILEOPSTRUCT op<b>;</b>

ZeroMemory<b>(</b><b>&</b>op<b>,</b> <b>sizeof</b><b>(</b>op<b>)</b><b>)</b><b>;</b>

op<b>.</b>hwnd  <b>=</b> Handle<b>;</b>

op<b>.</b>wFunc <b>=</b> FO_MOVE<b>;</b>

op<b>.</b>pFrom <b>=</b> From<b>;</b>

op<b>.</b>pTo   <b>=</b> To<b>;</b>

op<b>.</b>fFlags<b>=</b> <font color="blue">0</font><b>;</b>

SHFileOperation<b>(</b> <b>&</b>op<b>)</b><b>;</b>



<font color="navy">// send all temp files to the recycle bin</font>

<b>char</b> <b>*</b>File <b>=</b> <font color="blue">"C:\\windows\\temp\\*.TMP\0"</font><b>;</b>

SHFILEOPSTRUCT op<b>;</b>

ZeroMemory<b>(</b><b>&</b>op<b>,</b> <b>sizeof</b><b>(</b>op<b>)</b><b>)</b><b>;</b>

op<b>.</b>hwnd  <b>=</b> Handle<b>;</b>

op<b>.</b>wFunc <b>=</b> FO_DELETE<b>;</b>

op<b>.</b>pFrom <b>=</b> File<b>;</b>

op<b>.</b>fFlags<b>=</b> FOF_ALLOWUNDO<b>;</b>

SHFileOperation<b>(</b> <b>&</b>op<b>)</b><b>;</b>



<font color="navy">// copy all text files in the root drive to</font>

<font color="navy">// the temp directory.</font>

<b>char</b> <b>*</b>From <b>=</b> <font color="blue">"C:\\*.TXT\0"</font><b>;</b>

<b>char</b> <b>*</b>To   <b>=</b> <font color="blue">"C:\\WINDOWS\\TEMP\0"</font><b>;</b>

SHFILEOPSTRUCT op<b>;</b>

ZeroMemory<b>(</b><b>&</b>op<b>,</b> <b>sizeof</b><b>(</b>op<b>)</b><b>)</b><b>;</b>

op<b>.</b>hwnd  <b>=</b> Handle<b>;</b>

op<b>.</b>wFunc <b>=</b> FO_COPY<b>;</b>

op<b>.</b>pFrom <b>=</b> From<b>;</b>

op<b>.</b>pTo   <b>=</b> To<b>;</b>

op<b>.</b>fFlags<b>=</b> <font color="blue">0</font><b>;</b>

SHFileOperation<b>(</b> <b>&</b>op<b>)</b><b>;</b>

</pre>

<P>

<B>Notes:</B>

</P>

<P>

<B>1:</B> When you specify <TT>FO_DELETE</TT>, the files will be sent to the recycle

bin if <TT>fFlags</TT> contains the <TT>FOF_ALLOWUNDO</TT> style. Otherwise they will be

deleted.

</P>

<P>

<B>2:</B> The <TT>pFrom</TT> and <TT>pTo</TT> structure items are char pointers.

They are not arrays. They must point to a string, but the structure doesn't contain any

memory space to actually contain the strings. Don't do this:

<pre>

    op<b>.</b>pFrom <b>=</b> <font color="blue">"C:\\*.TXT"</font><b>;</b>

    op<b>.</b>pTo   <b>=</b> <font color="blue">"C:\\TEMP"</font><b>;</b>

</pre>

</P>

<P>

<B>3:</B> Notice the extra null terminator (added with the trailing '\0') in the

file strings . The MSDN documentation states that the <TT>pTo</TT> and <TT>pFrom</TT>

strings must be double null terminated.

</P>

<P>

<B>4:</B> The strings that <TT>pFrom</TT> and <TT>pTo</TT> point to can contain

more than one string. Each string should be separated with one null terminator. The double

null terminator mentioned above is used to terminate the entire list. Here is

an example:

</P>

<pre>

    <b>char</b> <b>*</b>From <b>=</b> <font color="blue">"C:\\*.txt\0C:\\*.log\0C:\\*.tmp\0"</font><b>;</b>

    op<b>.</b>pFrom   <b>=</b> From<b>;</b>



   <font color="navy">// the From declaration would be a little</font>

   <font color="navy">// cleaner by utilizing white space</font>

   <b>char</b> <b>*</b>From <b>=</b> <font color="blue">"C:\\*.txt\0"</font>

                <font color="blue">"C:\\*.log\0"</font>

                <font color="blue">"C:\\*.tmp\0"</font><b>;</b>

</pre>

<P>

<B>5:</B> When copying or moving files, the <TT>FOF_RENAMEONCOLLISION</TT> style in the

<TT>fFlags</TT> parameter will prevent the function from overwriting existing files. The

shell will create copies that say "Copy of readme.txt" (this is what explorer does).

</P>

<P>

<B>6:</B> You can also specify <TT>FO_RENAME</TT> as the <TT>wFunc</TT> parameter.

The <TT>fFlags</TT> parameter could contain many other advanced styles.

Check the WIN32.HLP file for more info.

</P>





</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -