COPY FILES OR DIRECTORIES
uses ShellAPI;
function CopyAllFiles(sFrom, sTo: string; Protect: boolean): boolean;
{ Copies files or directory to another directory. }
var F: TShFileOpStruct; ResultVal: integer; tmp1, tmp2: string;
begin
FillChar(F, SizeOf(F), #0);
Screen.Cursor := crHourGlass;
try
F.Wnd := 0;
F.wFunc := FO_COPY;
{ Add an extra null char }
tmp1 := sFrom + #0;
tmp2 := sTo + #0;
F.pFrom := PChar(tmp1);
F.pTo := PChar(tmp2);
if Protect then
F.fFlags := FOF_RENAMEONCOLLISION or FOF_SIMPLEPROGRESS
else
F.fFlags := FOF_SIMPLEPROGRESS;
F.fAnyOperationsAborted := False;
F.hNameMappings := nil;
Resultval := ShFileOperation(F);
Result := (ResultVal = 0);
finally
Screen.Cursor := crDefault;
end;
end;
uses ShellAPI;
function CopyAllFiles(sFrom, sTo: string; Protect: boolean): boolean;
{ Copies files or directory to another directory. }
var F: TShFileOpStruct; ResultVal: integer; tmp1, tmp2: string;
begin
FillChar(F, SizeOf(F), #0);
Screen.Cursor := crHourGlass;
try
F.Wnd := 0;
F.wFunc := FO_COPY;
{ Add an extra null char }
tmp1 := sFrom + #0;
tmp2 := sTo + #0;
F.pFrom := PChar(tmp1);
F.pTo := PChar(tmp2);
if Protect then
F.fFlags := FOF_RENAMEONCOLLISION or FOF_SIMPLEPROGRESS
else
F.fFlags := FOF_SIMPLEPROGRESS;
F.fAnyOperationsAborted := False;
F.hNameMappings := nil;
Resultval := ShFileOperation(F);
Result := (ResultVal = 0);
finally
Screen.Cursor := crDefault;
end;
end;
*- COPY DELETE MOVE FUNCTION
uses ShellApi;
function CopyDir(const fromDir, toDir: string): Boolean;
var fos: TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
with fos do begin wFunc := FO_COPY;
fFlags := FOF_FILESONLY;
pFrom := PChar(fromDir + #0);
pTo := PChar(toDir)
end; Result := (0 = ShFileOperation(fos));
end;
function MoveDir(const fromDir, toDir: string): Boolean;
var fos: TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
with fos do begin
wFunc := FO_MOVE;
fFlags := FOF_FILESONLY;
pFrom := PChar(fromDir + #0);
pTo := PChar(toDir)
end;
Result := (0 = ShFileOperation(fos));
end;
function DelDir(dir: string): Boolean;
var fos: TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
with fos do begin
wFunc := FO_DELETE;
fFlags := FOF_SILENT or FOF_NOCONFIRMATION;
pFrom := PChar(dir + #0);
end;
Result := (0 = ShFileOperation(fos));
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if cCopyDir('d:\download', 'e:\') = True then
ShowMessage('Directory copied.');
end;
function CopyDir(const fromDir, toDir: string): Boolean;
var fos: TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
with fos do begin wFunc := FO_COPY;
fFlags := FOF_FILESONLY;
pFrom := PChar(fromDir + #0);
pTo := PChar(toDir)
end; Result := (0 = ShFileOperation(fos));
end;
function MoveDir(const fromDir, toDir: string): Boolean;
var fos: TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
with fos do begin
wFunc := FO_MOVE;
fFlags := FOF_FILESONLY;
pFrom := PChar(fromDir + #0);
pTo := PChar(toDir)
end;
Result := (0 = ShFileOperation(fos));
end;
function DelDir(dir: string): Boolean;
var fos: TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
with fos do begin
wFunc := FO_DELETE;
fFlags := FOF_SILENT or FOF_NOCONFIRMATION;
pFrom := PChar(dir + #0);
end;
Result := (0 = ShFileOperation(fos));
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if cCopyDir('d:\download', 'e:\') = True then
ShowMessage('Directory copied.');
end;
*- COPY CUT PAST DELETE UNDO
edit1.perform(wm_copy,0,0); // edit1 deki metni panoya kopyalar.
edit1.perform(wm_cut,0,0); // Kes işlemini yapar.
edit1.perform(wm_paste,0,0); // Panodaki metni edit1 e yapıştırır.
edit1.perform(wm_clear,0,0); // edit1.clear 'de aynı işi görür.
Edit1.perform(em_undo,0,0); // Yapılan son işlemi geri alır.
Bu kodları memo, listbox, combobox, richtext bileşenleri içinde kullanabilirsiniz.
*- COPY CUT PASTE
Kesme, Kopyalama ve Yapıştırma işlemlerini, Klavye kullanılarak yapmak oldukça kolaydır. Bu işlemler menü elemanları vasıtasıyla da yapılabilir. Şayet bileşen, bu komutları aldığında ne yapacağını biliyorsa, Windows mesajlarını kullanmak en uygun hareket tarzıdır.
Kesme;
if GetFocus <> 0 then { Seçili bir pencere varmı? }
SendMessage( GetFocus, WM_CUT, 0, 0
Kopyalama;
if GetFocus <> 0 then { Seçili bir pencere varmı? }
SendMessage( GetFocus, WM_COPY, 0, 0
Yapıştırma;
if GetFocus <> 0 then { Seçili bir pencere varmı? }
SendMessage( GetFocus, WM_PASTE, 0, 0);
COPY DELETE MOVE FUNCTION
uses ShellApi;
function CopyDir(const fromDir, toDir: string): Boolean;
var fos: TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
with fos do begin
wFunc := FO_COPY;
fFlags := FOF_FILESONLY;
pFrom := PChar(fromDir + #0);
pTo := PChar(toDir)
end;
Result := (0 = ShFileOperation(fos));
end;
function MoveDir(const fromDir, toDir: string): Boolean;
var fos: TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
with fos do begin
wFunc := FO_MOVE;
fFlags := FOF_FILESONLY;
pFrom := PChar(fromDir + #0);
pTo := PChar(toDir)
end;
Result := (0 = ShFileOperation(fos));
end;
function DelDir(dir: string): Boolean;
var fos: TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
with fos do begin
wFunc := FO_DELETE;
fFlags := FOF_SILENT or FOF_NOCONFIRMATION;
pFrom := PChar(dir + #0);
end;
Result := (0 = ShFileOperation(fos));
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if cCopyDir('d:\download', 'e:\') = True then
ShowMessage('Directory copied.');
end;
COPY FILE
Uses ShellApi, ShlObj;
procedure CopyFiles(const FromFolder: string; const ToFolder: string);
var Fo : TSHFileOpStruct; buffer : array[0..4096] of char; p : pchar;
begin
FillChar(Buffer, sizeof(Buffer), #0);
p := @buffer;
StrECopy(p, PChar(FromFolder)); //this is folder that you want to copy
FillChar(Fo, sizeof(Fo), #0);
Fo.Wnd := Application.Handle;
Fo.wFunc := FO_COPY;
Fo.pFrom := @Buffer;
Fo.pTo := PChar(ToFolder); //this is where the folder will go
Fo.fFlags := 0;
if ((SHFileOperation(Fo) <> 0) or (Fo.fAnyOperationsAborted <> false)) then
ShowMessage('File copy process cancelled')
end;
CopyFiles('Kopyalamak istediğin klasörün yolu ve adı','Kopyalamak istediğin dizin ve ad yani hedef');
***YADA***
Hedef dizin, c:\deneme\ olsun . Proğramı otomatik olarak bulur ve hedef dizine koyalar.
CopyFile(pchar(ExtractFileName(Application.ExeName)),pchar('C:\deneme\' + ExtractFileName(Application.ExeName)), True);
***YADA***
CopyFile('C:\\Autoexec.bat', 'A:\\Backup\\Autoexec.bat', False);
CopyFile(PChar(Edit1.Text), PChar(Edit2.Text), False);
***YADA***
uses ShellAPI;
function CopyAllFiles(sFrom, sTo: string; Protect: boolean): boolean;
{ Copies files or directory to another directory. }
var F: TShFileOpStruct; ResultVal: integer; tmp1, tmp2: string;
begin
FillChar(F, SizeOf(F), #0);
Screen.Cursor := crHourGlass;
try
F.Wnd := 0;
F.wFunc := FO_COPY;
{ Add an extra null char }
tmp1 := sFrom + #0;
tmp2 := sTo + #0;
F.pFrom := PChar(tmp1);
F.pTo := PChar(tmp2);
if Protect then
F.fFlags := FOF_RENAMEONCOLLISION or FOF_SIMPLEPROGRESS
else
F.fFlags := FOF_SIMPLEPROGRESS;
F.fAnyOperationsAborted := False;
F.hNameMappings := nil;
Resultval := ShFileOperation(F);
Result := (ResultVal = 0);
finally
Screen.Cursor := crDefault;
end;end;
DOSYA KOPYALAMA ISLEMI-WINDOWSU KULLANARAK
Function CopyFiles(const Source,Destination: String): Boolean;
var
SHFileOpStruct : TSHFileOpStruct;
begin
if FileExists(Source) then
begin
FillChar(SHFileOpStruct,SizeOf (TSHFileOpStruct),#0);
with SHFileOpStruct do begin
Wnd:=Application.Handle;
wFunc:=FO_COPY;
fFlags:=FOF_ALLOWUNDO;
hNameMappings:=nil;
pFrom:=PChar(Source+#0+#0);
pTo:=PChar(Destination+#0+#0);
end;
Result := ShFileOperation(SHFileOpStruct ) = 0;
end else Result := False;
end;
// Kullanımı:
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
if not CopyFiles('C:\\windows\\notepad. exe', 'C:\\sil\\notepad.exe') then
showmessage('Kopyalama islemi basarisiz');
end;
COPY
uses shellapi;
function FileManager(xSourcePath, xDestPath, xPara: string): Boolean;
var
PFileMsg: TSHFileOpStruct;
mNowPath: string;
begin
Result := False;
FillChar(PFileMsg, sizeof(PFileMsg), #0);
if pos('.', xpara) = 0 then
exit;
mNowPath := GetCurrentDir;
if xSourcePath <> '' then
if not DirectoryExists(xSourcePath) then
begin
showmessage('The source path does not exist !');
exit;
end;
if xDestPath <> '' then
if not DirectoryExists(xDestPath) then
begin
showmessage('The destination path does not exist !');
exit;
end;
if SetCurrentDirectory(Pchar(xSourcePath)) then
begin
with PFileMsg do
begin
if Owner is TForm then
Wnd := TForm(Owner).Handle
else
Wnd := Application.Handle;
if xDestPath <> '' then
begin
wFunc := FO_COPY;
PTo := pChar(xDestPath);
fFlags := FOF_MULTIDESTFILES + FOF_NOCONFIRMATION;
end
else
begin
wFunc := FO_DELETE;
fFlags := FOF_ALLOWUNDO + FOF_NOCONFIRMATION;
end;
pFrom := PChar(xPara + #0#0);
end;
SHFileOperation(PFileMsg);
SetCurrentDirectory(Pchar(mNowPath));
Application.ProcessMessages;
Result := True;
end;
end;
Example CopyFile :
FileManager('C:Demo', 'C:Temp', '*.*');
Example DeleteFile: (delete C: Demo * . * )
FileManager('C:Demo', '', '*.*');
***YADA***
uses ShellAPI;
procedure TForm1.BtnCopyClick(Sender: TObject);
var
fileOp: TShFileOpStruct;
fromDir: string;
toDir: string;
begin
FillChar(fileOp, Sizeof(TShFileOpStruct), 0);
fromDir := DirectoryListBox1.Directory + '*.*'#0;
toDir := DirectoryListBox2.Directory + #0;
with fileOp do
begin
wnd := Handle;
wfunc := FO_COPY;
pFrom := PChar(fromDir);
pTo := PChar(toDir);
fFlags := FOF_ALLOWUNDO;
fAnyOperationsAborted := false;
hNameMappings := nil;
lpszProgressTitle := nil;
end;
SHFileOperation(fileOp);
end;
***YADA***
Procedure FileCopy( Const sourcefilename, targetfilename: String );
Var S, T: TFileStream;
Begin
S := TFileStream.Create( sourcefilename, fmOpenRead );
try
T := TFileStream.Create( targetfilename,
fmOpenWrite or fmCreate );
try
T.CopyFrom(S, S.Size ) ;
finally
T.Free;
end;
finally
S.Free;
end;
End;
***YADA***
procedure FileCopy(const FromFile, ToFile: string);
var
FromF, ToF: file;
NumRead, NumWritten: Word;
Buf: array[1..2048] of Char;
begin
AssignFile(FromF, FromFile);
Reset(FromF, 1); { Record size = 1 }
AssignFile(ToF, ToFile); { Open output file }
Rewrite(ToF, 1); { Record size = 1 }
repeat
BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
BlockWrite(ToF, Buf, NumRead, NumWritten);
until (NumRead = 0) or (NumWritten <> NumRead);
CloseFile(FromF);
CloseFile(ToF);
end;
***YADA***
procedure CopyFile(FromFileName, ToFileName: string);
var
FromFile, ToFile: File;
Begin
AssignFile(FromFile, FromFileName); {Assign FromFile to FromFileName}
AssignFile(ToFile, ToFileName); { Assign ToFile to ToFileName }
Reset(FromFile); { Open file for input }
try
Rewrite(ToFile); { Create file for output }
try
if LZCopy(TFileRec(FromFile).Handle, TFileRec(ToFile).Handle) < 0
then
raise EInOutError.Create('Error using LZCopy')
finally
CloseFile(ToFile); { Close ToFile }
end;
finally
CloseFile(FromFile); { Close FromFile }
end;end;
Hiç yorum yok:
Yorum Gönder