Using the progress bar to find files

I am writing an application that will search for a specific file or files from the appropriate path. During the search, I need to expand the progress bar, which should be executed in accordance with the search. so how can i do this? and if possible, send the code?

+6
java
source share
5 answers

It's a difficult question. I donโ€™t remember any instance of non-indexed search showing a progress bar. (Can someone prove their mistake to me?)

I would suggest the following method (an extension of the Benny Hallett proposal) that could provide a greater degree of detail.

Suppose you are looking for a specific file name pattern for the entire file system (for example, in unix, to search for all * .jpg files in /)

Start by dividing the execution line into N parts (where N is the number of directories in the root path of your search).

Each time you delve into the heirachy directory, the total length of the process line that has been assigned to the parent directory is divided by the number of subdirectories contained. When the catalog search is completed, the part that was provided to it is added to the progress bar. In even more detail, you can divide the selection by the number of files + directories in the current directory.

This method should ensure that you only need to go through the directory structure once and better handle uneven directories. (Unevenly, I mean a mixture of directories with high and low search costs)

As an example, suppose the following directory structure:

/ clipart photos family holiday wallpapers anime landscapes 

(each indentation indicates a level deeper in the directory tree and suggests that all directory traversal is performed in alphabetical order)

You start by viewing the '/' and you see that there are three directories (clipart, photos and wallpapers), and therefore you first divide the progress bar into one third.

Then you look for the directory in the directory, and when done, update your progress bar to one-third. Then you go inside the photos and see that there are two subdirectories. This means that when you finish the search in the family, you add one sixth to the progress bar, since there are two subdirectories (family and holiday), and the progress for each of them is half one third allocated for photos.

So in short:

clip completion adds one third

completion of a photo / family adds one masterpiece

completion of photos / holidays adds one masterpiece

the completion of the wallpaper / anime adds one masterpiece

the completion of wallpaper / landscape adds one masterpiece

Total 1.0 (or 100%) (ignoring floating point precision)

+8
source share

To use most Progress Bar controls, you need to know 2 things.

  • How many things do you need to do
  • How much have you done.

For the first, it would probably be useful to use the number of files in a directory (for one directory) or the number of subdirectories (for recursive access to directories)

Secondly, you need to update the progress bar every time you process a file or directory.

There's a tutorial on using Progress Bars in Swing: here

The basic code you will need

 JProgressBar progressBar = new JProgressBar(0, getNumberOfFiles()); 

And then update it using

 progressBar.setValue(getCurrentNumberProcessed()); 

If the original number of files or directories is unknown, you can call progressBar.setIndeterminate(true) to create one of them, I have no idea how long it will take the progress bars. From there, you can determine how many files or directories you need to process before doing

 progressBar.setIndeterminate(false); progressBar.setMaximum(numberOfFiles); 
+4
source share

Your search program (class SearchFile - preferably singleton) should have a field that is updated as you move through the search. For example private double searchProgress; , and then update this field during the actual search.

 double searchProgress = 100/noOfTotalFiles; searchProgress = searchProgress + searchProgressIncrement; 

And provide a public getter method for your search program.

 public double getProgress(){ return searchProgress;} 

Step 2: Use a simple topic to poll progress for each second (as necessary) from the program call.

 progressBar.setIndeterminate(false); progressBar.setMaximum(100); Thread t = new Thread(){ @override public void run(){ progressBar.setValue((int)SearchFile.getProgress()); } } 

Make sure you have the correct logic to know how long you run this thread periodically. For example, update the flag in SearchFile, which says that the search is complete.

 while(SearchFile.isRunning()){ Thread.sleep(1000); //sleep for 1 sec progressBar.setValue((int)SearchFile.getProgress()); } 

you can still improve it ...

+1
source share

I used the Catchwa method. I set the achievement range to 14000, because the OS on my system has about as many channels. When I found an empty directory, I added a fractional, weighted amount to the progress bar. The amount is based on depth and normalizes with a range. In each passage of the subtree, you end up with an empty directory, and the weight of all empty sub-folders in the directory contains the weight of the directory, but is broken into pieces. When I found a non-empty directory, I saved the number of sub-folders on the map. I got using Qt:

 emit findProgressBar_setRange(14000); ... if (dir.size()) { m_dirsAtDepth[++m_depth] = dir.size(); } else { qreal product = 1.00; for (int i = 1; i <= m_depth; ++i) { product *= m_dirsAtDepth[i]; } int x = qRound((1.00 / product) * 14000); emit findProgressBar_addValue(x); } 

It works pretty smoothly and cheaply. I also offer the user the exact option of the execution step, where I first calculate the total number of disks, which can be expensive.

+1
source share

Such a thing will depend on several factors. Are you looking for a file recursively? What files are you looking for? Are you looking for several types? How do you want to update the progress bar regarding search progress?

Also do you know how to use streams? Such a problem is more than likely to lead to the use of threads (one to find a file, the other to update the progress bar). If threads were not used, then you risk that your user interface freezes until the search is complete.

0
source share

All Articles