progress bar use in copying a file <SOLVED!!!>

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

progress bar use in copying a file <SOLVED!!!>

#1 Post by 8-bit »

I want to have a progress bar in a gtkdialog script show the progress of copying a file of unknown size.
The size could be anything from 256 megs to 3 gigs.
But with a progress bar, the user gets some indication of the file copy progress.
None of the gtkdialog progressbar examples show an example that syncs to a process such as copy or some other action you want to show the progress of.
They all seem to just set some number with steps and a delay between them.
That really does not tell me a lot about syncing it to an action.
So, anyone got an example that I can try.

I already tried to use while [action];do progressbar;done and I had to force shutdown of the PC to recover from a copy command that never ended. It would start again after copying the file.

I really do not want to go through that again as shutting down while a file is being copied is dangerous to the hard drive.

Again, anyone good a good example of how to code this?
Last edited by 8-bit on Sat 01 Oct 2011, 07:43, edited 1 time in total.

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#2 Post by 8-bit »

One thing I did notice is that in all the progressbar examples I did not see one that shows the progressbar implemented to show the progress of a procedure.
Such examples would be like a progress bar for copy, or read-in of a big file, or creation of a large file.

It seems that the only thing it shows is a progressbar that is generated by creating a set of numbers that increment in steps with a sleep command in between the increment steps.

I am currently trying to get a progress bar working for a file copy in a gtkdialog script and already have run into problems.

It seems that a line "org_size=$(stat -c %s $sf);echo $org_size" with "$sf" being a file with path, that works fine in a terminal will not work in a gtkdialog script and gives me the filename with path followed by a Permission Denied message.
One way I can see to get around this is to create a bash script that writes the value to a temp file and then reading back in that value.

In trying to get things set up, I even made a programming mistake that caused a file copy to go into an endless loop.
I had to chance a forced reboot to recover from that one.

And just so you know, this is for a script I am writing that is copying a large file to a different directory on a different partition with a new file name.
But rather than just a "Please wait! Copying file." message, I would like to have a progress bar synced to the copy status.
I found two examples for bash and they work, but trying to use the code in my gtkdialog script is leaving me at a loss.

User avatar
puppyluvr
Posts: 3470
Joined: Sun 06 Jan 2008, 23:14
Location: Chickasha Oklahoma
Contact:

#3 Post by puppyluvr »

:D Hello,
Have had a similar issue..
Tried to create a loop to read the size of the file to copy, divide by 10, and use that to control the progress bar, but as of yet to no avail...
Could you post the bash examples you spoke of??
Close the Windows, and open your eyes, to a whole new world
I am Lead Dog of the
Puppy Linux Users Group on Facebook
Join us!

Puppy since 2.15CE...

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#4 Post by Flash »

Is it hard to periodically find the size of a file that's building, as it's building? Would that interfere with the copy process? ROX seems to do it when I rip a track from a CD and also when I download a Puppy iso. I don't know how accurate it is though. If it's possible to periodically measure the size of a file as it grows to its (known) final size, even I can see how that could be used to build a progress bar. :lol:

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

progress bar use in copying a file

#5 Post by L18L »

8-bit wrote:...
It seems that a line "org_size=$(stat -c %s $sf);echo $org_size" with "$sf" being a file with path, that works fine in a terminal will not work in a gtkdialog script and gives me the filename with path followed by a Permission Denied message
...
Why not try a simple

Code: Select all

ls --size $sf
:)

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#6 Post by 8-bit »

puppyluvr wrote::D Hello,
Have had a similar issue..
Tried to create a loop to read the size of the file to copy, divide by 10, and use that to control the progress bar, but as of yet to no avail...
Could you post the bash examples you spoke of??
I actually have two examples one can run from the command line.
I was trying to use parts of one to use a progressbar in gtkdialog.
But for some reason, it wants to try to execute the file I am trying to copy when used in gtkdialog.
the two I called fcopy1 and fcopy2 and they work fine from a terminal.

fcopy1

Code: Select all

#!/bin/bash
# Example: ./test original_file destination_file
usage()
{
   echo "Usage: $0 original_file destination_file"
   exit 1;
}

test $# == 2 || usage
orig_size=$(stat -c %s $1)

>$2
dest_size=0
cp -f $1 $2 &

while [ $orig_size -gt $dest_size ] ; do
   dest_size=$(stat -c %s $2)
   pct=$((( 69 * $dest_size ) / $orig_size ))

    echo -en "\r["
    for j in `seq 1 $pct`; do
        echo -n "="
    done
    echo -n ">"
    for j in `seq $pct 68`; do
        echo -n "."
    done
    echo -n "] "
    echo -n $((( 100 * $pct ) / 69 ))
    echo -n "%"
done
echo
fcopy2

Code: Select all

#!/bin/bash
# File copy with progress indicators
# Example: ./test original_file destination_file

usage()
{
   echo "Usage: $0 original_file destination_file"
   exit 1;
}

test $# == 2 || usage

echo Preparing to copy
orig_size=$(stat -c %s $1)

>$2
dest_size=0
cp -f $1 $2 &

while [ $orig_size -gt $dest_size ] ; do
   dest_size=$(stat -c %s $2)
   pct=$((( 100 * $dest_size ) / $orig_size ))

if [ $pct -lt 10 ] ; then
   echo -en "#  $pct%\b\b\b\b"
else
   echo -en "#  $pct%\b\b\b\b\b"
fi
sleep 1
done
echo

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#7 Post by 8-bit »

L18L,
I tried the line you suggest and it needs further work on the string returned to just get the file size in bytes without the file name.
Also, when the line is used in gtkdialog, I get the same problem of my source file trying to execute.
If I use the file name and not a variable, it works. But if I try to use a variable containing the filename, it tries to execute the file name supplied.

User avatar
puppyluvr
Posts: 3470
Joined: Sun 06 Jan 2008, 23:14
Location: Chickasha Oklahoma
Contact:

#8 Post by puppyluvr »

:D Hello,
Interesting...
I was trying to use "du"..
Close the Windows, and open your eyes, to a whole new world
I am Lead Dog of the
Puppy Linux Users Group on Facebook
Join us!

Puppy since 2.15CE...

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#9 Post by 8-bit »

I think my problem is with the use of "stat" in gtkdialog.
I assume gtkdialog cannot find the "stat" command and rather than kick out an error there, tries in turn to execute each item in return resulting in the error of file name cannot be executed.
I can run the bash scripts with supplied source and destination, but the progress is echoed to a terminal and not my gtkdialog script even if I try to get it there.

It is the little things you expect to work that can be the worst to debug when they fail.

Other applications we use in Puppy work fine at displaying progress of a file transfer, it is just a matter of figuring out how they do it.
And if and when I do, I will have made progress on my script which by the way is a pupsave restore script for Pupsave-backup files.

But, it also works for copying a file from one directory to another.

I know, dumb, dumb me. But it does give a challenge.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#10 Post by L18L »

8-bit wrote:L18L,
I tried the line you suggest and it needs further work on the string returned to just get the file size in bytes without the file name.
Also, when the line is used in gtkdialog, I get the same problem of my source file trying to execute.
If I use the file name and not a variable, it works. But if I try to use a variable containing the filename, it tries to execute the file name supplied.
cut d ' ' -f 1
or one of "speeding up scripts"

sorry for gtkdialog, I am sticking with yad

edited
is the variable double quoted?

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#11 Post by seaside »

8-bit,

You might find this useful -
pv - Shell pipeline element to meter data passing through

pv (Pipe Viewer) can be inserted into any normal pipeline between two processes to give a visual indication of how quickly data is passing through, how long it has taken, how near to completion it is, and an estimate of how long it will be until completion.

To use it, insert it in a pipeline between two processes, with the appropriate options. Its standard input will be passed through to its standard output and progress will be shown on standard error.
Here's a deb package to try - don't think it's missing any dependencies. (I had to call it a pet to attach, so rename .deb before clicking on it to install)

Regards,
s
Attachments
pv_1.1.4-1_i386.pet
Pipe viewer (rename to .deb before clicking to install)
(27.52 KiB) Downloaded 327 times

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#12 Post by 8-bit »

To all, I have tried double quoting my file name variable and that made no difference. The line that works in bash, but not gtkdialog is "org_size=$(stat -c %s "'$sf'")"
It is supposed to return the file size in bytes and does in bash, but not gtkdialog.
Since I am getting a message of not being able to execute the file contained in the string variable, I know that is being passed. Just not the way it should.
Also a few lines down in the script the same slightly modified line is used to get the size of the destination file and I get another message that stat is missing an operand.
So since it seems to recognize both the command and the variable, why is it trying to execute the variable?

Also, thank you seaside for the deb. I will check it out although I am wanting to make this available to those that are not running a version of Puppy that supports deb files.

I also have passed my preliminary script to 2byte for possible modification and inclusion in Pupsave-backup that is the file in Hot-Backup for frugal install Pups.

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

#13 Post by GatorDog »

8-bit, here's an alternative command to get byte count.
I tried a few quick test of "wc" on text and non-text files and seems to work on both.

Code: Select all

org_size=$(wc -c $sf | cut -d" " -f1)
It may work where stat does not.

When I used quoting like your example, "'$sf'" , I got this error-
wc: 'filename.ext': No such file or directory
I removed the single quotes, it worked. And also worked without any quotes.
Does gtkDialog need the variable quoting?

Looking at examples of command substitution, $(....), some examples quoted a variable
and some did not.

YMMV,
GatorDog

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#14 Post by 8-bit »

It was all a matter of syntax in coding.
I now have the application completed and it includes a progress bar to show the progress of the file copy to another path/name.
If you want to check it out, it is in Additional Software - Uitlities: http://www.murga-linux.com/puppy/viewtopic.php?t=72153.
It is called Restore pupsave file hotbkup saved files in the header and the program once extracted is PupsaveRestore.sh

So my scripting problem is fixed without use of external temporary files too.
It was a real challenge!
And how do I feel. :D :D

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#15 Post by seaside »

8-bit wrote:
Also, thank you seaside for the deb. I will check it out although I am wanting to make this available to those that are not running a version of Puppy that supports deb files.
No support of deb files needed, as "pv" is a single binary and ran ok in pup431- it's just "contained" in a deb file.

Regards,
s

Post Reply