728x90
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | private void makelnk(String targetPath, String name) throws IOException, NoSuchMethodException, ScriptException { File lnkFolder = new File(System.getProperty("user.dir") + "\\lnk"); if(!(lnkFolder.exists())) lnkFolder.mkdirs(); File scriptFile = new File("whatever.js"); scriptFile.createNewFile(); try (PrintWriter script = new PrintWriter(scriptFile)) { script.printf("try {\n"); script.printf("wshshell = WScript.CreateObject(\"WScript.Shell\")\n"); String line = System.getProperty("user.dir") + "\\lnk\\" + name + ".lnk"; String lines2[] = line.split("\\\\"); line = ""; for(int i=0; i<lines2.length; i++) { if(i == lines2.length - 1) { line += lines2[i]; } else line += lines2[i] + "\\\\"; } line.substring(0,line.length()-10); System.out.println(line); script.printf("shortcut = wshshell.CreateShortcut(\"%s\")\n", line); String lines[] = targetPath.split("\\\\"); line = ""; for(int i=0; i<lines.length; i++) line += lines[i] + "\\\\"; script.printf("shortcut.TargetPath = \"%s\"\n", line); script.printf("shortcut.WindowStyle = 1\n"); script.printf("shortcut.HotKey = \"\"\n"); script.printf("shortcut.Description = \"%s\"\n", ""); script.printf("shortcut.WorkingDirectory = \"%s\"\n", ""); script.printf("shortcut.Save()\n"); script.printf("} catch (err) {\n"); // Commented by default script.printf("/*WScript.Echo(\"name:\")\nWScript.Echo(err.name)\n"); script.printf("WScript.Echo(\"message:\")\nWScript.Echo(err.message)\n"); script.printf("WScript.Echo(\"description:\")\nWScript.Echo(err.description)\n"); script.printf("WScript.Echo(\"stack:\")\nWScript.Echo(err.stack)\n"); script.printf("*/\n"); script.printf("WScript.Quit(1)\n"); script.printf("}\n"); script.flush(); script.close(); Runtime.getRuntime().exec("cscript " + System.getProperty("user.dir")+"\\"+"whatever.js"); } } | cs |