Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8449595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:45:11+00:00 2026-06-10T10:45:11+00:00

I’m trying to get a backup program to take a folder selected by the

  • 0

I’m trying to get a backup program to take a folder selected by the user using JFileChooser and copy it to a destination also selected using the same method.

The only problem is that it doesn’t put all the contents of the selected folder into a folder in the destination named the same thing, and I really dont know why. I did some Googling, and I didnt find anything useful.

Here’s the code:

package main;

import java.io.File;
import java.util.ArrayList;

public class test {

BackgroundWorker bw;
static ArrayList bgWorker = new ArrayList();
ArrayList al = new ArrayList(); // this is the list of files selected to
                                // back up
String dir = ""; // this is the path to back everything up to selected by
                    static // the user
boolean bwInitiallized = false;

public void startBackup() throws Exception {
    Panel.txtArea.append("Starting Backup...\n");

    for (int i = 0; i < al.size(); i++) {
        /**
         * THIS IS WHERE I NEED TO CREATE THE FOLDER THAT EACH BACKUP FILE
         * WILL GO INTO EX: SC2 GOES INTO A FOLDER CALLED SC2 AND RIOT GOES
         * TO RIOT, ALL WITHIN THE DIRECTORY CHOSEN
         */
        File file = new File((String) al.get(i));
        File directory = new File(dir);

        // File dirFile = new File(dir + "\\" + file.getName());
        // if (!dirFile.exists())
        // dirFile.mkdir();

        bw = new BackgroundWorker(Panel.txtArea, file, directory);
        bgWorker.add(bw);
        bwInitiallized = true;
        bw.execute();

        /**
         * follows to the bottom of the txtarea
         */
        int x;
        Panel.txtArea.selectAll();
        x = Panel.txtArea.getSelectionEnd();
        Panel.txtArea.select(1, x);

    }
    clearList(); // method not included in this example that deletes all the
                    // contents of the al array list.
}

public static void cancel() {
    BackgroundWorker bg;
    if (bwInitiallized) {
        bwInitiallized = false;
        Panel.txtArea.append("Cancelling...\n");
        for (int i = 0; i < bgWorker.size(); i++) {
            // BackgroundWorker bg = (BackgroundWorker) bgWorker.get(i);
            bg = (BackgroundWorker) bgWorker.get(i);
            bg.cancel(true);
        }
        Panel.txtArea.append("Canceled backUp!\n");
    } else {
        Panel.txtArea.append("Cannot Cancel! Not Initiallized!\n");
    }
}
}

What I think the problem is: I believe that for whatever reason the destination file path needs to have to the name of the folder included, but I tried that and it didnt help.

Does anyone know what I’m doing wrong?

This is the code that makes the JFileChooser:

public void fileChooserToDestination() {
    LookAndFeel previousLF = UIManager.getLookAndFeel();
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
    }
    JFileChooser jfc = new JFileChooser();
    try {
        UIManager.setLookAndFeel(previousLF);
    } catch (UnsupportedLookAndFeelException e) {
    }

    jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    if (jfc.showDialog(null, "Select Directory") == JFileChooser.APPROVE_OPTION) {
        File file = jfc.getSelectedFile();
        dir = file.getPath();
        Panel.txtArea.append("User selected " + file.getPath()
                + " for the destination...\n");
        try {
            startBackup();
        } catch (Exception e) {
        }

    } else {
        Dialogs.msg("You canceled selecting a destination folder! Returning to main screen...");
        al.clear();
        Panel.txtArea.append("User cancelled the destination selection..."
                + "\n");
    }

    return;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:45:12+00:00Added an answer on June 10, 2026 at 10:45 am

    Parts of the code I need are missing. I can’t see where you’re making your decisions about how to append the source file to the destination path, so I wrote this quick example it illustrate the point…

    File sourcePath = new File("/path/to/be/backed/up");
    File destPath = new File("X:/BackupHere");
    
    // Get all the files from sourcePath
    List<File> listFiles = getFilesFrom(sourcePath);
    
    for (File toBackup : listFiles) {
    
        // Now we need to strip off the sourcePath
        // Get the name of the file
        String fileName = toBackup.getName();
        // Get parent folder's path
        String path = toBackup.getParent();
        // Remove the source path from file path
        path = path.substring(sourcePath.getPath().length());
    
        // Append the file name to the path
        path = path + File.separator + fileName;
    
        // Now we have the name of the back up file
        String backupFile = destPath + path;
    
        System.out.println("Backup to " + backupFile);
    
    }
    

    Basically, you need to strip of the “source path” (the directory you want to copy). You then use the resulting value to append to the “backup path” value and you should then have a suitable path.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.