Borrar filtros
Borrar filtros

how to use waitbar to show file copying status

12 visualizaciones (últimos 30 días)
Amit
Amit el 4 de Ag. de 2017
Editada: Jan el 8 de Ag. de 2017
This is my code under a push button in GUI. I want that it should show the progress of the file copying status with the use of "waitbar" command or by any other means .It will be very helpful if someone can provide a sample code, though help are available in matlab but I m unable to figure it out how it can be used for displaying file copying status. Thanks
% --- Executes on button press in pushbutton44.
function pushbutton44_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton44 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global dirpath_import;
dirpath_import= uigetdir();
set(handles.edit38,'String',dirpath_import);
importpath=get(handles.edit38,'String');
exportpath=get(handles.edit1,'String');
copyfile(importpath,exportpath);
msgbox('Import/Copy Complete');

Respuesta aceptada

Jan
Jan el 4 de Ag. de 2017
Editada: Jan el 4 de Ag. de 2017
While it does not matter, that the code is part of a callback, the relevant part is only:
copyfile(importpath, exportpath)
During this command Matlab is blocked an there is no chance for any progress indicators. If this is really required, you can create your own function to copy the file in blocks of e.g. 1MB:
function myCopyFile(source, dest)
FileInfo = dir(source);
FileSize = FileInfo.bytes;
waitH = waitbar(0, sprintf('Copying %.2f MB', FileSize/1e6);
inFID = fopen(source, 'r');
if inFID == -1
error('Cannot open file for reading: %s', source);
end
outFID = fopen(dest, 'w');
if outFID == -1,
fclose(inFID);
error('Cannot open file for writing: %s', dest);
end
chunk = 1e6;
nChunk = ceil(FileSize / chunk);
iChunk = 0;
while ~feof(inFID)
iChunk = iChunk + 1;
waitbar(iChunk / nChunk, waitH);
data = fread(inFID, chunk, '*uint8');
fwrite(outFID, data, 'uint8');
end
fclose(inFID);
fclose(outFID);
end
UNTESTED
Attention: File handling is a critical job. If the disk is full or the destination is on a network drive and loosing the connection, this cheap and naive implementation of a copyfile does not show a warning and might leave the file system in a corrupted state. The user can kill the copying by Ctrl-c also. I would not dare to do this in a productive system. Care for backups. Check the file system. Insert consistency checks, e.g. by counting the number or read and written bytes by catching the output of fread and fwrite.
  3 comentarios
Amit
Amit el 8 de Ag. de 2017
Hi Sir, Your help was able to solve my problem(for file copying) but I m stuck again. What I want to do is copy a folder (total content of the folder). So can u guide how to proceed. I m unable to get the Folder size. Thanks in advance
Jan
Jan el 8 de Ag. de 2017
Editada: Jan el 8 de Ag. de 2017
function S = GetFolderSize(FilePath)
% Get size of files contained in a folder
% Author: Jan Simon, Licence: CC BY-SA 3.0
FileList = dir(FilePath);
FileList(ismember({FileList.name}, {'.', '..'})) = [];
isFolder = [FileList.isdir];
S = sum([FileList(~isFolder).bytes]); % Sum file file sizes
for iFile = find(isFolder) % Recursion for folders
S = S + GetFolderSize(fullfile(FilePath, FileList(iFile).name));
end
end
UNTESTED Note that running this can need a lot of time already.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 5 de Ag. de 2017
You could use system() to start the copy, and you could add the & character to the command so that it returns immediately. Then you could go into a loop using dir() to check the size of the destination file and use that to update a waitbar.
However, watch out for these factors:
  • on some operating systems, there is no visible result in the target directory until the point at which the copy finishes, at which time the file suddenly appears, complete.
  • on MacOS, the temporary file that appears is typically named as the file name plus a ".part" extension, so it would be that file name whose size you had to monitor
  1 comentario
Amit
Amit el 5 de Ag. de 2017
Thanx...i think what u have suggested will be system dependent..rather i wanted to make it system independent so that portability of code is not an issue..

Iniciar sesión para comentar.

Categorías

Más información sobre Dialog Boxes en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by