0

I'm stuck in a situation.

String tmpfolder = System.getProperty("java.io.tmpdir");
\\this is the path C:\Users\biraj\AppData\Local\Temp\ 
tmpfolder = tmpfolder.replace("\\", "\\\\"); 
Runtime.getRuntime().exec("cmd /c del "+tmpfolder+"IEDriver.dll /f /s /q"); 

When I run this code it does not delete the IEDriver.dll file. But when I give the static path of the temporary folder then it deletes that file:

Runtime.getRuntime().exec("cmd /c del C:\\Users\\biraj\\AppData\\Local\\Temp\\IEDriver.dll /f /s /q"); 

Can anyone explain to me why the first code didn't work? What's wrong in that?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user1254261
  • 142
  • 2
  • 3
  • 12

1 Answers1

1

The problem is that you are changing literal \ into a literal \\ in your second line. When we write code, we use \\ inside a string to represent a literal \ to the program, but your tmpfolder variable already has the correct literal \ inside it.

If you delete the following line, it should work.

tmpfolder = tmpfolder.replace("\\", "\\\\"); 

The easiest way to understand the difference is to just print the string you constructed, as well as the literal string and compare them visually.

System.out.println("cmd /c del "+tmpfolder+"IEDriver.dll /f /s /q");
System.out.println("cmd /c del C:\\Users\\biraj\\AppData\\Local\\Temp\\IEDriver.dll /f /s /q")

Another possible problem is that you need to change

"IEDriver.dll /f /s /q" 

to

 "\\IEDriver.dll /f /s /q"

Of course the visual comparison will answer this question definitively.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • i delete the second line.still its not deleted – user1254261 Apr 16 '14 at 05:16
  • @user1254261, Use the second half of my solution, and see if you can spot the difference. My guess is that you may be missing a `\\ ` in front of `IEDriver.dll`. – merlin2011 Apr 16 '14 at 05:17
  • can you tell me if i could delete the folder while deleting that IEDriver.dll file as it becomes empty after deleting the file inside it. – user1254261 Apr 16 '14 at 05:32
  • @user1254261, See this question to learn how to delete the directory along with the file: http://superuser.com/questions/204406/whats-the-equivalent-to-rm-rf-with-windows-command – merlin2011 Apr 16 '14 at 05:35
  • i dont have the directory name only the file name – user1254261 Apr 16 '14 at 05:39
  • @user1254261, Please ask a new question with details about exactly what paths you have and do not have. – merlin2011 Apr 16 '14 at 05:42