Page 1 of 1

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

Posted: Fri 30 Sep 2011, 07:31
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?

Posted: Fri 30 Sep 2011, 11:52
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.

Posted: Fri 30 Sep 2011, 12:25
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??

Posted: Fri 30 Sep 2011, 13:01
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:

progress bar use in copying a file

Posted: Fri 30 Sep 2011, 13:32
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
:)

Posted: Fri 30 Sep 2011, 16:42
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

Posted: Fri 30 Sep 2011, 16:50
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.

Posted: Fri 30 Sep 2011, 16:54
by puppyluvr
:D Hello,
Interesting...
I was trying to use "du"..

Posted: Fri 30 Sep 2011, 17:32
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.

Posted: Fri 30 Sep 2011, 20:25
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?

Posted: Fri 30 Sep 2011, 22:13
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

Posted: Sat 01 Oct 2011, 03:05
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.

Posted: Sat 01 Oct 2011, 04:43
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

Posted: Sat 01 Oct 2011, 07:41
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

Posted: Sat 01 Oct 2011, 21:25
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