• Tim (unregistered)

    wow...

  • Dube (unregistered)

    that's not even a copy routine, it's a rename!

    made in platfrom independent java makes it even more a wtf hehe

  • Joe (unregistered)

    Reminds me of a saying..."When all you have is a hammer, EVERYTHING looks like a nail"

  • Volmarias (cs)

    Java doesn't actually have an OS-level file copy method that can be called (you have to actually read in the file and write it out to a new file!), so I can understand their twisted logic to some degree. But... a batch file? For the love of god, why?

  • Look at me! I'm on the internets! (unregistered)

    Anything wrong with:

    process.exec("copy file1 file2"); // windows

    You could add a System.getProperty("os.name") and make it universal.

    Of course, I'd probably want to uses an abstract factory to do this -- It's much more enterprisy.

  • RaCeR (unregistered)

    It's not quite rename though is it?

    If you try to rename a file as itself, that file will instead be deleted. A poor attempt, to be thorough it should compare the source and dest names, as well as the MD5 sum of the batchfile, C-, must try harder.

  • DOA (cs)

    That hurt my brain

  • Jim (unregistered)

    Anyone notice that this moves the file, instead o0f copying?

    sure, it copies, but then it deletes the source!

    sarcastic clap

  • Mike5 (unregistered)

    The real WTF is that this should be the CodeSOD?

    Mike5

  • Ben (unregistered) in reply to Volmarias
    Volmarias:
    Java doesn't actually have an OS-level file copy method that can be called (you have to actually read in the file and write it out to a new file!)

    This, of course, is the real WTF!

  • Ben (unregistered) in reply to Volmarias
    Volmarias:
    Java doesn't actually have an OS-level file copy method that can be called (you have to actually read in the file and write it out to a new file!)

    This, of course, is the real WTF!

  • ailivac (cs)

    I can't believe no one else has mentioned another huge error in this. I guess you people haven't worked with a unix shell enough to know which characters are OK to use in command lines and which need special processing.

    This is obviously designed to run on windows, where directories commonly have names like "My Documents," "Program Files" and "Application Data." All of those have a character in them that will make the batch file call fail instantly.

  • GuntherVB (cs) in reply to Look at me! I'm on the internets!
    Look at me! I'm on the internets!:
    Anything wrong with:

    process.exec("copy file1 file2"); // windows

    You could add a System.getProperty("os.name") and make it universal.

    my eyes!

  • bkendig (cs) in reply to Jim
    Jim:
    sure, it copies, but then it deletes the source!

    So, just copy it back then!

  • Phlo (unregistered) in reply to Tim
    if (sourceFile.exists()){ sourceFile.delete(); }
    I wonder if the programmer also burns the original papers after taking a copy of them... oO
  • Greatest CodeSOD ever (unregistered)

    Yes, this is utterly stupid. First, a batch-file. Second, it's wasting time on copying when it could just use "move" instead. Third, it doesn't check if the copy operation was a success, and just deletes the source file when it's done.

    That is, it checks if the source file exists, AFTER it has attempted to copy it.

    Perfect, beautiful!

  • Nojazz Song Hurts (unregistered)

    it has a tidy API, only a couple of obvious bugs (for example: someone could re-write that batch file to do something different), and it's a great hack!

    so what if it is not system independent, but not everyone needs that.

    sure, it's named wrong: it should be something like 'copyThenDeleteOriginal', but that's not much of a WTF. How this code leaves you speechless is beyond me.

  • anon (unregistered) in reply to Jim
    Jim:
    Anyone notice that this moves the file, instead o0f copying?

    sure, it copies, but then it deletes the source!

    sarcastic clap

    huh?

  • ailivac (cs) in reply to Ben
    Ben:
    Volmarias:
    Java doesn't actually have an OS-level file copy method that can be called (you have to actually read in the file and write it out to a new file!)

    This, of course, is the real WTF!

    Plenty of languages don't have built-in routines to do high level operations like copying files, but most have libraries available to do it (such as Perl's File::Copy).

    The true WTF is that Java doesn't have a standard or sane way of installing extra libraries, so programmers have to reinvent broken wheels like this all the time.

  • Alex (unregistered)

    Another cool feature is that it will only "work" one time on any given PC, since it doesn't overwrite the batch file it already exists, so it just keeps running the first batch file ever created by the method. Genius!

  • Kittown (unregistered)

    This is a pretty bad implementation of a pretty legit idea. Sometimes you HAVE to use batch files to do those things from java to make the result more predictable and less dependent on particlar jvm problems. In some cases this helps to speed up the file copying procces by 2-5 times or so comparing to common file copying methods from Apache Ant. There is usually more that one command in that batch, though, and there are all kinds of symbol quoting code, as well as checks for the system this is used on.

  • DOOM (unregistered)

    I always wanted a back door built into a system that would be hard to spot . The batch file sounds like the perfect place to put in my thanks (delete all files) when I get fired for doing crap like this.

  • jaspax (cs)

    Now that was a WTF.

    The best part was where it deletes the file after it's copied it. The second-best part was that none of the methods are synchronized, creating all kinds of race conditions.

  • alarm (unregistered) in reply to Kittown

    Util.copyFile(new File("C:\Util.class"), new File("C:\copyFile.bat")); System.out.println("Problem solved.");

  • Asd (unregistered) in reply to ailivac
    ailivac:
    The true WTF is that Java doesn't have a standard or sane way of installing extra libraries, so programmers have to reinvent broken wheels like this all the time.
    Bwuh? Just drop commons-io.jar into your classpath. Most Java applications use dozens of open source libraries.

    Java 7 or 8 is supposed to get a new and improved file system.

    I also noticed their IO util methods don't seem to throw IOException. I wonder do they use RuntimeExceptions (unchecked exceptions), or, more likely, just ignore them.

  • T $ (cs) in reply to Nojazz Song Hurts
    Nojazz Song Hurts:
    it has a tidy API, only a couple of obvious bugs (for example: someone could re-write that batch file to do something different), and it's a great hack!

    so what if it is not system independent, but not everyone needs that.

    sure, it's named wrong: it should be something like 'copyThenDeleteOriginal', but that's not much of a WTF. How this code leaves you speechless is beyond me.

    I'd say the fact that he fit so many problems into such a short amount of code would be a definite reason for being speechless. I suppose there's worse things he could have done: thrown both functions together, ask for the source and destination in the function rather than handing them in, not actually displayed anything in the catch, not checking if the batch file exists before returning. No matter how bad code gets, there is ALWAYS a worse way to do it (the WTF calculators taught us that)

  • Anon (unregistered) in reply to Alex

    Um, but the batch file does copy %1 %2, so you can keep running the same batch file every time with different arguments, as this one does.

  • ikke (unregistered)

    If I'm not mistaken, an existing batfile is not modified but it is executed nontheless. And a used batchfile is not removed so chances are that one exist after the first "copy".

  • Asd (unregistered)

    I vaguely recall that to execute dos commands from Java you have to run "cmd /c copy" rather than just "copy". Maybe I am misremembering but that could have been the trigger for this sorry batch mess.

  • rd (unregistered) in reply to bkendig
    bkendig:
    Jim:
    sure, it copies, but then it deletes the source!

    So, just copy it back then!

    Brilliant!

  • Packrat (unregistered)

    Let's see:

    It moves rather than copies.

    It 'confirms' the move by checking for the existence of the source file, rather than the destination.

    It'll die horribly if either the source or destination names have spaces in them.

    It won't work on *nix or, I presume, on the Mac.

    It won't work if the program doesn't have write and execute access to the root directory--i.e. it requires what are usually administrator rights.

    It will produce undefined results if two instances are run at once either by separate threads or separate processes.

    Have I missed anything else? Just what what kind of idiot wrote this?

  • Aaron (cs)

    I'm quite impressed. First it makes a call to a parameterless method which tries to create a batch file in a hard-coded location which might very well not have write permission, opens the file twice (write, then append), doesn't trap any exceptions, doesn't quote the file names, and only does this if the file doesn't already exist (doesn't care if it was modified). Then it launches the batch file as its own process regardless of what might be in it, checks for the existence of the source file after attempting to copy it, deletes the source file even if the source and destination were the same, and essentially eats any exceptions during the "copy" process (but would still throw exceptions encountered during the batch file creation). And on top of this, is named copy when it clearly moves.

    The best part though, is that somebody has already written a FileIO class that could probably do this in two lines - we don't know that it has a readFile method, but it definitely has a writeFile.

    My Java's just a bit rusty but I think I count no less than 10 different errors. It's very Agile though, I'll give them that. Way to think outside the process... er, box.

  • snoofle (unregistered) in reply to Tim
    Comment held for moderation.
  • SuperousOxide (cs) in reply to ailivac
    ailivac:
    Plenty of languages don't have built-in routines to do high level operations like copying files, but most have libraries available to do it (such as Perl's File::Copy).

    But Java is not a language that normally shies away from providing high level functionality in its standard libraries.

  • n9ds (cs)

    And don't forget that he's assuming that (even in Windows) there is a C: drive.

  • DoubleMalt (unregistered) in reply to GuntherVB

    Moreover the approach won't work because process.exec() does no path resolving (and cannot call built-in commands of the shell). It depends on a existing file with the exact name given. Relative paths are resolved against the current wurking directory. The smart author of the code snippet aptly avoided this pitfall. ;)

  • zolf (unregistered)

    WTF

  • DoubleMalt (unregistered) in reply to GuntherVB
    GuntherVB:
    Look at me! I'm on the internets!:
    Anything wrong with:

    process.exec("copy file1 file2"); // windows

    You could add a System.getProperty("os.name") and make it universal.

    my eyes!

    Forgot to quote. My remark was in reply to this points to the text above

  • troels (unregistered)

    I choose not to believe this. It isn't true.

  • JohnFx (unregistered) in reply to Phlo

    There is a million dollar idea in there. A combination photocopier/shredder.

  • Someone You Know (cs) in reply to rd
    rd:
    bkendig:
    Jim:
    sure, it copies, but then it deletes the source!

    So, just copy it back then!

    Brilliant!

    "Brillant!" is the word you're searching for, I'm sure.

  • John Doe (unregistered)

    The "utility" doesn't check if the batch file has actually been created (the user doesn't have to be authorized, apart from the fact that a C drive is present, as has been noted). It also doesn't clean up its mess, but rather reuses the file! What if someone added a "format c:" to it? Lastly, I don't understand the need to "cd" if absolute paths to the file are passed. It's mind boggling that such a few lines of code can contain so many problems.

  • John Doe (unregistered) in reply to Aaron
    Aaron:
    Way to think outside the process... er, box.
    I wish there was at least some thinking involved, so this piece of junk would never have been written...
  • tobjar (unregistered)
    "Brillant!" is the word you're searching for, I'm sure.

    stick to coding, dude. leave the copy editing to people who know how shit is spelled.

  • JBQ (unregistered)

    Write once, look stupid everywhere.

  • Mainc Mailman (unregistered) in reply to tobjar
    tobjar:
    "Brillant!" is the word you're searching for, I'm sure.

    stick to coding, dude. leave the copy editing to people who know how shit is spelled.

    You indeed correctly spelled 'shit', but "Someone You Know" also correctly spelled 'Brillant!'.

    Look it up, dude.

  • Gil (unregistered) in reply to Packrat

    Yes, you missed the fact that if anything in the try actually goes wrong, there is no way the calling can know if it was successful short of testing for the presence of the new file.

  • Ant (unregistered)

    Hey now, you've got to give the guy some credit: this would execute faster than Vista's built in copy routine.

  • dkf (unregistered) in reply to tobjar
    Comment held for moderation.
  • snoofle (unregistered) in reply to Mainc Mailman
    Mainc Mailman:
    tobjar:
    "Brillant!" is the word you're searching for, I'm sure.

    stick to coding, dude. leave the copy editing to people who know how shit is spelled.

    You indeed correctly spelled 'shit', but "Someone You Know" also correctly spelled 'Brillant!'.

    Look it up, dude.

    Good one!

Leave a comment on “My Kind of copyFile”

Log In or post as a guest

Replying to comment #:

« Return to Article