Delphi Programming Create and delete folders

Source: Internet
Author: User
Tags response code

Add two TButton components to the form, one to create a new folder, the other to delete the newly created folder, and the design-complete form as shown in Figure 1.

Figure 1 Main interface

You first add a reference to Shellapi in the uses section of your program, and add the following code to the response of the Create button:

Procedure Tform1.btncreateclick (Sender:tobject);

Begin

CreateDirectory (Pchar (Extractfilepath (paramstr (0)) + ' Test '), nil);

End

The program executes the CreateDirectory (Pchar (Extractfilepath (0) + ' Test '), nil) statement at the beginning of its operation, creating a subdirectory Test in the directory where the program instance resides.

When the program is running, click the Delete button to delete the newly created test subdirectory with the following response code:

Procedure Tform1.btndeleteclick (Sender:tobject);

Var

T:tshfileopstruct;

p:string;

Begin

P:=extractfilepath (paramstr (0)) + ' Test ';

With T do

Begin

wnd:=0;

Wfunc:=fo_delete;

Pfrom:=pchar (P);

Pto:=nil;

Fflags:=fof_allowundo+fof_noerrorui;

Hnamemappings:=nil;

lpszprogresstitle:= ' deleting folder ';

Fanyoperationsaborted:=false;

End

SHFileOperation (T);

End

The program can undo the deletion by populating the properties of the Tshfileopstruct type Object T, and of course the final deletion is done through SHFileOperation (t). If the SHFileOperation function returns a value of 0, it indicates that the deletion succeeded or that the deletion failed.

If the user wants to delete the Test subdirectory directly in the program, you can do so directly by calling the RemoveDirectory (Extractfilepath (paramstr (0)) + ' Test ') statement

The program code is as follows:

Unit Unit1;

Interface

Uses

Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,

Dialogs, Stdctrls, Shellapi;

Type

TForm1 = Class (Tform)

Btncreate:tbutton;

Btndelete:tbutton;

Procedure Btncreateclick (Sender:tobject);

Procedure Btndeleteclick (Sender:tobject);

Private

{Private declarations}

Public

{Public declarations}

End

Var

Form1:tform1;

Implementation

{$R *.DFM}

Procedure Tform1.btncreateclick (Sender:tobject);

Begin

CreateDirectory (Pchar (Extractfilepath (paramstr (0)) + ' Test '), nil);

End

Procedure Tform1.btndeleteclick (Sender:tobject);

Var

T:tshfileopstruct;

p:string;

Begin

P:=extractfilepath (paramstr (0)) + ' Test ';

With T do

Begin

wnd:=0;

Wfunc:=fo_delete;

Pfrom:=pchar (P);

Pto:=nil;

Fflags:=fof_allowundo+fof_noerrorui;

Hnamemappings:=nil;

lpszprogresstitle:= ' deleting folder ';

Fanyoperationsaborted:=false;

End

SHFileOperation (T);

RemoveDirectory (Extractfilepath (paramstr (0)) + ' Test ') (Pchar);

End

End.

Save the file, and then press F9 to run the program, the initial picture of the program running as shown in Figure 2.

Figure 2 Initial screen of program operation

Clicking the Create button creates a subdirectory test in the same directory as the program instance, and you can delete the newly created test subdirectory by clicking the Delete button.

This example describes how to create a folder and provides two ways to delete a folder-one is to delete the folder to the Recycle Bin, and the other is to delete it directly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.