maybe someone can help me...
I'm trying to rename/delete files in some directories.
While the same code works nicely on Solaris, when
trying the same code under W95 it seems something's causing
a problem. (I do take into account the file.separator variable
which is different under the 2 OSs)
The files are named 1.txt, 2.txt, etc. In the following
I'm just trying to switch a file X with another one Y, given their
numbers (names).
public static void rename(int j, int k) {
String mydir = "C:\\some\\dir";
String currentName = Integer.toString(k) + ".txt";
String newName = Integer.toString(j) + ".txt";
File currentFile = new File(mydir, currentName);
File newFile = new File(mydir, newName);
if(currentFile.renameTo(newFile))
System.out.println(" ok ");
else {
System.out.println(" nope ");
try {
Process app = Runtime.getRuntime().exec("del
"+currentFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
renameTo() and delete() simply don't do anything, that's why
I tryied with DOS's REN or DEL. Here's what I get:
java.io.IOException: CreateProcess: del C:\some\dir\29.txt
error=0
at java.lang.Win32Process.<init>(Win32Process.java:59)
at java.lang.Runtime.exec(Runtime.java:175)
at java.lang.Runtime.exec(Runtime.java:137)
at setnv.main.GP.rename(GP.java:385)
at setnv.main.GP.createIndex(GP.java:322)
at setnv.main.GP.main(GP.java:35)
Now the strange thing is that not only this code
works just fine on Solaris, but renameTo() or delete()
also work under W95 - *IF* I set k (the current name
of the file to rename or delete) to a fix value, i.e.
k_temp=25 or whatever. Now if I put an if(k==25) in front of
the k_temp=25 it stops working !!!!
Am I missing anything ?
--
Tavi Ureche
~~~~~~~~~~~
SCRIBENS - Polytechnique | Applications des inforoutes - CRIM
http://www.info.polymtl.ca/tavi/ | http://www.crim.ca/~ureche
ta...@info.polymtl.ca | ure...@scribens.polymtl.ca | ure...@crim.ca
PGP FingerPrint = 00 DD 0C 74 67 7F 37 B8 03 68 0D 2A 49 98 FB 0A
Tavi Ureche <ure...@scribens.polymtl.ca> writes:
>I'm trying to rename/delete files in some directories.
>While the same code works nicely on Solaris, when
>trying the same code under W95 it seems something's causing
>a problem. (I do take into account the file.separator variable
>which is different under the 2 OSs)
There is a bug concerning renameTo()!!!
See http://java.sun.com/products/jdk/1.1/bugs.html for details.
On this web page a work arround is mentioned: First delete() the file and
then rename the other file.
Towards your problems about exec():
just call "cmd.exe /c del" and "cmd.exe /c ren" on WinNT and
"command.com /c del" ,.... on Win95.
Hope that helps
Vera
--
Vera Travnicek __o
ve...@pool.informatik.rwth-aachen.de _ \<_
(_)/(_)