wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to create a batch file using java program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to create a batch file using java program?" Watch "How to create a batch file using java program?" New topic
Author

How to create a batch file using java program?

Janki Dani
Greenhorn

Joined: Apr 05, 2012
Posts: 8
Hi,

I need to create a batch file using java program which can perform following actions.

Copied directories and sub directories from source folder older than 10 days to destination folders.
Delete copied directories from source folder and create short cuts for that in source folder.

Many thanks,
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

If your question is how to create a batch file, the asnwer is that you do it the same way you'd create any other file. You create a FileWriter or FileOutputStream, you write to it, and then you close() it.

If your question is what commands you need to put into that batch file to make it do the things you want, then that has nothing to do with Java, and this site is not an appropriate venue to ask that quesiton.

Why do you want to create the batch file with Java though? Why not just write it by hand?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Or are you trying to create a batch file that will execute a Java program, and that Java program will do those things you need done?

I think you need to back up a bit and explain your actual requirement, rather than asking how to implement specific solutions.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Please stop posting the same question in different threads. That's annoying and confusing.
Janki Dani
Greenhorn

Joined: Apr 05, 2012
Posts: 8
Apologies if my question is confusing.

There is a requirement that I want to move some folders from source to destination every 10 days and then create a shortcuts for the moved folder in source drive.

I am very new to Java. I am just wandering that if I can automate the above process through java.

Appreciate your time in this.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Koen Aerts wrote:Please stop posting the same question in different threads. That's annoying and confusing.


I have locked the other thread an provided a link there to this one so we can have one coherent discussion.
Janki Dani
Greenhorn

Joined: Apr 05, 2012
Posts: 8
@Koen: apologies but as I have mentioned before I am new to this forum and Java and sorry about the comment but I dont find the design of the forum is user friendly.
Janki Dani
Greenhorn

Joined: Apr 05, 2012
Posts: 8
Thanks Jeff for doing that.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
Continuing the discussion from the other (closed) thread:

Yes, you can perform the file operations and I/O you've described using Java. I recommend you consult the File I/O / handling / operations tutorials.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Janki Dani wrote:Apologies if my question is confusing.


It wasn't the question that was confusing, it was the fact that you posted it twice.

There is a requirement that I want to move some folders from source to destination every 10 days and then create a shortcuts for the moved folder in source drive.


  • Moving the folders can easily be done with Java, though I probably wouldn't create a Java program just to do that.
  • Doing it periodically (such as every 10 days) can be done by running your Java app as a service that is always running, and letting it handle its own scheduling, or, more appropriately, by using the Windows Task Scheduler to launch your program on the schedule needed.
  • Creating the shortcuts is not something that can be done directly with Java, as far as I know, but I could be wrong about that. If they're just a normal file with a particular extension and whose contents indicates the path to the target, then yes, you should be able to do it with Java. If they're a special type of file, then you'd have to use JNI or invoke a Windows command from your Java program.


  • So, all in all, while it may be possible to do this in Java, I don't think Java is an appropriate tool for this job.
    Janki Dani
    Greenhorn

    Joined: Apr 05, 2012
    Posts: 8
    thanks for your reply.
    Chris Janicki
    Greenhorn

    Joined: Aug 30, 2006
    Posts: 21

    Java 7 seems to add tools for links/shortcuts. See createLink() in the java.nio.file.Files class, new in Java 7. There's a short tutorial here: Dr. Dobbs
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11398
        
      16

    Be aware that "batch file" has a very specific meaning to folks who have been around a while (i.e. old farts, like me). It referred to a file named *.bat and could be run on the DOS/CMD line - much like a Unix shell script.

    So when you say "create a batch file using Java", it sounds like you want to use Java to create what is basically a text file with a .bat extension.

    What it sounds like you REALLY want is to write a java program that can move/update various files on your system, and then run it at a certain period/date/time.

    Is that correct?


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19718
        
      20

    fred rosenberger wrote:Be aware that "batch file" has a very specific meaning to folks who have been around a while (i.e. old farts, like me).

    What, did you just call me an old fart?


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to create a batch file using java program?