java代码
/**
*
* @param creatVbsPath vbs脚本路径
* @param oldPath 快捷方式路径
* @param newPath 文件路径
* @throws IOException
*/
public static void creatVbsPath(String creatVbsPath, String oldPath, String newPath) {
String content = new String();
content = "Set WshShell=CreateObject(\"WScript.Shell\") : Set oShellLink=WshShell.CreateShortcut(\"" + oldPath + "\") : oShellLink.TargetPath=\"" + newPath + "\" : oShellLink.WindowStyle=1 : oShellLink.Save\n";
File file = new File(creatVbsPath);
/**
* 如果文件不存在,则创建
*/
if (!file.exists()) {
try {
file.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
}
try{
FileOutputStream write = new FileOutputStream(creatVbsPath);
OutputStreamWriter out = new OutputStreamWriter(write);
out.write(content);
out.close(); // 最后记得关闭文件
System.gc();
String[] cpCmd = new String[]{"wscript ", creatVbsPath};
Runtime.getRuntime().exec(cpCmd);
}catch (Exception e){
e.printStackTrace();
}
}
vbs脚本:以.vbs结尾文件
Set WshShell=CreateObject("WScript.Shell") : Set oShellLink=WshShell.CreateShortcut("C:\Users\data-1\Desktop\test\fghfd.lnk") : oShellLink.TargetPath="C:\Users\data-1\Desktop\test1\ttts.txt" : oShellLink.WindowStyle=1 : oShellLink.Save