Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PLease:  I need program which moves files from one directory to another and renames each file to a unique name before placing it to another directory I m handling csv files.

Posted on 2006-11-16
10
Medium Priority
?
168 Views
Last Modified: 2010-05-18
PLease:  I need program which moves files from one directory to another and renames each file to a unique name before placing it to another directory I m handling csv files.

Files that come in directory where they shoudl be moved from have 10 or more caracters xxxxxxxxxxxxxx.csv  and when they are moved they should be assigned a unique file name of max 8 caracters. Pelase help.
0
Comment
Question by:dm98101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
10 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 17957626


Imports System.IO

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        ' Test the MoveFiles function
        MoveFiles("C:\Temp", "C:\Temp\TempData")

    End Sub

    Private Sub MoveFiles(ByVal SourceDir As String, ByVal DestDir As String)

        If Not Directory.Exists(SourceDir) Then
            MessageBox.Show("Source directory does not exist, correct and try again", _
                "Source Directory Error")
            Return
        End If

        If Not Directory.Exists(DestDir) Then
            Dim results As DialogResult
            results = MessageBox.Show("Destination directory does not exist." & _
                " Do you want to create it " & vbCrLf & DestDir, _
                "Destination Directory Error", MessageBoxButtons.YesNo, _
                MessageBoxIcon.Error, MessageBoxDefaultButton.Button2)
            If results = Windows.Forms.DialogResult.Yes Then
                Directory.CreateDirectory(DestDir)
            Else
                Return
            End If
        End If

        Dim Count As Integer = 1    ' Used to make file name unique
        Dim di As New DirectoryInfo(SourceDir)
        Dim fi() As FileInfo = di.GetFiles("*.csv")
        For Each f As FileInfo In fi
            ' The moved file will have the name, the time it was done as hhmmss format
            ' plus - plus the value of count
            File.Move(f.FullName, DestDir & "\" & Now.ToString("hhmmss") & "-" & _
                Count.ToString & ".csv")
            Count += 1
        Next

    End Sub


Fernando
0
 

Author Comment

by:dm98101
ID: 17963358
how to generate this code in to one exe file?????
0
 

Author Comment

by:dm98101
ID: 17963558
is this monitoring when files are written in source directory ansd pickes them up ???? or?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 64

Expert Comment

by:Fernando Soto
ID: 17966134
Hi dm98101;

To your question, "how to generate this code in to one exe file?????", I guess my next question should be; Are you a programmer?

To your question, "is this monitoring when files are written in source directory ansd pickes them up ???? or?", The above code just moves the files from a source directory to a target directory and checks to see if the directories exist and assignes the moved file a unique name.

Fernando
0
 
LVL 2

Expert Comment

by:saintsairforce
ID: 17980677

Here is an updated program    

import java.io.*;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.*;

/*******************************************************************************
     loadHTML(String)
     
     This loads html files into the home directory
     *******************************************************************************/
     private void loadCSV(String home)
     {
          String fileDiscription="";
          File intputFile,outputFile;
         
          getFiles();
         
          if(cancelFlag!=JFileChooser.CANCEL_OPTION)
          {
               for(int knt=0;knt<selectedFiles.length;knt++)
               {
                    intputFile = new File(directory+"\\"+selectedFiles[knt]);          //sets the location of the input file

                         String outFile="xxxxxxxxxx";
                          while(outFile.length>8)
                       {
                               //outFile = get the file name from the user
                               //check that outFile endsWith .csv
                     }
                    outputFile = new File(home+outFile);                         //sets the location of the output file
                   
                    try
                    {    
                         copyFile(intputFile,outputFile,home);
               }
                    catch(IOException e)
                    {
                         JOptionPane.showMessageDialog(null,"Unable to upload "+selectedFiles[knt],"ERROR",JOptionPane.ERROR_MESSAGE);
                    }
               }
          }
     }
     
     /*******************************************************************************
     getFiles()
     
     This opens a JFileChooser and allows the user to browse their computer for files.
     *******************************************************************************/
     
     private void getFiles()
     {
          JFileChooser chooser = new JFileChooser();

          chooser.setAcceptAllFileFilterUsed(false);               //does not allow user to use the AllFiles type option.
          chooser.addChoosableFileFilter(new csvFilter());     //filters selection to only allow html and htm files
          chooser.setMultiSelectionEnabled(true);                //Allows the user to select multiple files

          try     //this sets the look and feel of the file chooser to the currently selected look and feel
          {
               UIManager.setLookAndFeel(UserMenu.looks[UserMenu.lookNumber].getClassName());
               SwingUtilities.updateComponentTreeUI(this);
          }
          catch(Exception e)
          {
               new ErrorLogger("Error getFiles() - fileLoader.java:    "+e);
          }
         
          cancelFlag = chooser.showOpenDialog(this);
         
          directory = (chooser.getCurrentDirectory()).toString();     //gets the current directory
         
          File tempFiles[] = chooser.getSelectedFiles();
         
          selectedFiles = new String[tempFiles.length];
         
          for(int knt=0;knt<tempFiles.length;knt++)
          {
               selectedFiles[knt] = tempFiles[knt].getName();
          }
     }

/*******************************************************************************
     copyFile(File, File, String)
     
     This method copies the input file to the selected output directory.
     *******************************************************************************/
     
     public void copyFile(File inputFile, File outputFile, String directory) throws IOException
     {
          if((new File(directory).mkdir()));           //creates home directory if needed
         
          InputStream inStream = new FileInputStream(inputFile);
          OutputStream outStream = new FileOutputStream(outputFile);
         
          byte[] buffer = new byte[1024];
          int length;
         
          while ((length = inStream.read(buffer)) > 0)
          {
               outStream.write(buffer, 0, length);
          }
         
          inStream.close();
          outStream.close();
     }

//Another Class to filter out the csv files

import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.*;

public class csvFilter extends FileFilter
{
     /*******************************************************************************
     accept(File)
     
     This filters out files and only allows htm and html files to be selected
     *******************************************************************************/
     
     public boolean accept(File file)
     {
          if (file.isDirectory())
               return true;

          String extension = getExtension(file);
         
          if (extension != null)
          {
               if (extension.equals("csv")&&(file.toString()).length()>=14) //10+.csv
                    return true;
               else
                    return false;
          }
          return false;
     }
     
     /*******************************************************************************
     getDescription()
     
     This returns the discription of the available files
     *******************************************************************************/
     
     public String getDescription()
     {
          return ".htm, .html";
     }
     
     /*******************************************************************************
     getExtension(File)
     
     This returns the extension of a file
     *******************************************************************************/
     
     private String getExtension(File file)
     {
               String ext = null;
               String name = file.getName();
               int index = name.lastIndexOf('.'); //finds the index of the '.' thus any data after this is the extension
               
               if(index>0&&(index<name.length()-1))
               {
                    ext = name.substring(index+1).toLowerCase(); //this gets the extension characters
               }
               
               return ext;
     }
}

As for deleting the file go to this link for a simple example http://javaalmanac.com/egs/java.io/DeleteFile.html
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 18136147
Hi Bob;

I would like to see a resolution to this question because I have posted a working solution to the question.

Fernando
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question