- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
wow...
Admin
that's not even a copy routine, it's a rename!
made in platfrom independent java makes it even more a wtf hehe
Admin
Reminds me of a saying..."When all you have is a hammer, EVERYTHING looks like a nail"
Admin
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?
Admin
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.
Admin
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.
Admin
That hurt my brain
Admin
Anyone notice that this moves the file, instead o0f copying?
sure, it copies, but then it deletes the source!
sarcastic clap
Admin
The real WTF is that this should be the CodeSOD?
Mike5
Admin
This, of course, is the real WTF!
Admin
This, of course, is the real WTF!
Admin
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.
Admin
my eyes!
Admin
So, just copy it back then!
Admin
Admin
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!
Admin
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.
Admin
huh?
Admin
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.
Admin
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!
Admin
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.
Admin
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.
Admin
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.
Admin
Util.copyFile(new File("C:\Util.class"), new File("C:\copyFile.bat")); System.out.println("Problem solved.");
Admin
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.
Admin
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)
Admin
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.
Admin
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".
Admin
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.
Admin
Brilliant!
Admin
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?
Admin
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.
Admin
To add to a prior poster's suggestion:
Think of the possibilities!
Admin
But Java is not a language that normally shies away from providing high level functionality in its standard libraries.
Admin
And don't forget that he's assuming that (even in Windows) there is a C: drive.
Admin
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. ;)
Admin
WTF
Admin
Admin
I choose not to believe this. It isn't true.
Admin
There is a million dollar idea in there. A combination photocopier/shredder.
Admin
"Brillant!" is the word you're searching for, I'm sure.
Admin
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.
Admin
Admin
stick to coding, dude. leave the copy editing to people who know how shit is spelled.
Admin
Write once, look stupid everywhere.
Admin
You indeed correctly spelled 'shit', but "Someone You Know" also correctly spelled 'Brillant!'.
Look it up, dude.
Admin
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.
Admin
Hey now, you've got to give the guy some credit: this would execute faster than Vista's built in copy routine.
Admin
Admin