aspose file tools*
The moose likes General Computing and the fly likes creating a link Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » General Computing
Bookmark "creating a link" Watch "creating a link" New topic
Author

creating a link

Daniel Layher
Greenhorn

Joined: Jul 14, 2006
Posts: 19
Hi. I'm making a program that will search a network drive for copies of the same file. Once it does that it is supposed to make a copy of those files, delete them, and turn them into shortcuts. Unfortunately, i can't seem to figure out how to create an .lnk file. If anybody has a solution, please, let me know.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61220
    
  66

Please reserve this forum for advanced Java questions. I've moved this on to the General Computing forum as the format for such files is not Java-specific.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sanjaya Sugiarto
Ranch Hand

Joined: Mar 25, 2004
Posts: 229
Perhaps the shortcut command answers your question


<a href="http://www.wi.hs-furtwangen.de" target="_blank" rel="nofollow">Business Information Technology - Hochschule Furtwangen University, Germany</a>
Daniel Layher
Greenhorn

Joined: Jul 14, 2006
Posts: 19
unfortunately, i'm using java and i'm not sure how to use that in the program. Any suggestions???
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

Originally posted by Daniel Layher:
unfortunately, i'm using java and i'm not sure how to use that in the program. Any suggestions???


You can call any OS program from java via the Runtime.exec() series of methods -- just build the command along with any parameters and call it. You can also send the input and read the output from that program.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

Another option... The "lnk" file is merely a binary file. The format of this file is actually documented (just do a google search for the file format). You can also create the binary file yourself using the standard java io packages.

Henry
Daniel Layher
Greenhorn

Joined: Jul 14, 2006
Posts: 19
Originally posted by Henry Wong:


You can call any OS program from java via the Runtime.exec() series of methods -- just build the command along with any parameters and call it. You can also send the input and read the output from that program.

Henry


I mostly understand how to ust the Runtime.exec(), but i'm still not sure how to use it to access shortcut.exe... is there anywhere where there is an example or explanation of how to accomplish this?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

Originally posted by Daniel Layher:


I mostly understand how to ust the Runtime.exec(), but i'm still not sure how to use it to access shortcut.exe... is there anywhere where there is an example or explanation of how to accomplish this?


I don't know about anyone else... but I generally open up a command window. Find the program that I want to run. Run it, to get the right combination of parameters.

And when I figure out the exact calling signature of the program that I want, and make sure it works, I transpose that to a Runtime.exec() call.

Henry
Daniel Layher
Greenhorn

Joined: Jul 14, 2006
Posts: 19
in case anybody has the same problem i did, i found code that made shortcuts for me... i seem to only have problems setting the icons, but i can live with it... either way, this is it...

import java.io.*;
import java.util.ArrayList;

public class shortcut extends iconSelect {

private byte answer[];
private int offset;
private byte buf[];
private ByteArrayOutputStream bytestream;
/** Creates a new instance of pscc */
public shortcut() {
}

public void WindowsShortcut(String fulllinkpath, String fulltargetpath, String arguments, String workingdirectory, String iconfile)
throws IOException
{
answer = new byte[1024];
offset = 1024;
buf = new byte[8];
bytestream = new ByteArrayOutputStream(125);
buildLink(fulltargetpath, arguments, workingdirectory, fulllinkpath, setIcon(fulltargetpath));
File linkfile = new File(fulllinkpath);
mkpath(linkfile);
writeFile(linkfile);
}

private final void buildLink(String fulltargetpath, String arguments, String workingdirectory, String fulllinkpath, String iconfile)
throws IOException
{
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
write(buf, 0, 4);
if(iconfile != null)
{
int iconfilelen = write(iconfile);
writeBackward(iconfilelen, 2);
}
if(arguments != null)
{
int argumentslength = write(arguments);
writeBackward(argumentslength, 2);
}
if(workingdirectory != null)
{
int tplength = write(workingdirectory);
writeBackward(tplength, 2);
}
File destf = new File(fulltargetpath);
File linkf = new File(fulllinkpath);
String rp = getRelativePath(linkf.getParent(), destf.getParent());
if(rp != null)
{
if(rp.length() > 0)
{
rp = rp + File.separator + destf.getName();
}
else
{
rp = destf.getName();
}
int rplen = write(rp);
writeBackward(rplen, 2);
}
int postfileoff = offset;
buf[0] = 0;
write(buf, 0, 1);
int finalpathoff = offset;
buf[0] = 0;
write(buf, 0, 1);
write(fulltargetpath);
int localpathoff = offset;
buf[0] = 0;
write(buf, 0, 1);
buf[0] = 16;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
write(buf, 0, 4);
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
write(buf, 0, 4);
buf[0] = 3;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
write(buf, 0, 4);
int voltablen = (localpathoff - offset) + 4;
writeBackward(voltablen, 4);
int fileinfooff = offset - 28;
int tmp = finalpathoff - fileinfooff;
writeBackward(tmp, 4);
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
write(buf, 0, 4);
tmp = localpathoff - fileinfooff;
writeBackward(tmp, 4);
buf[0] = 28;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
write(buf, 0, 4);
buf[0] = 1;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
write(buf, 0, 4);
buf[0] = 28;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
write(buf, 0, 4);
tmp = postfileoff - fileinfooff;
writeBackward(tmp, 4);
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
buf[4] = 0;
buf[5] = 0;
buf[6] = 0;
buf[7] = 0;
write(buf, 0, 8);
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
write(buf, 0, 4);
buf[0] = 1;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
write(buf, 0, 4);
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
write(buf, 0, 4);
int filelen = (int)destf.length();
writeBackward(filelen, 4);
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
buf[4] = 0;
buf[5] = 0;
buf[6] = 0;
buf[7] = 0;
write(buf, 0, 8);
write(buf, 0, 8);
write(buf, 0, 8);
buf[0] = 32;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
write(buf, 0, 4);
int flags = 2;
if(rp != null)
{
flags += 8;
}
if(workingdirectory != null)
{
flags += 16;
}
if(arguments != null)
{
flags += 32;
}
if(iconfile != null)
{
flags += 64;
}
writeBackward(flags, 4);
buf[0] = -64;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
buf[4] = 0;
buf[5] = 0;
buf[6] = 0;
buf[7] = 70;
write(buf, 0, 8);
buf[0] = 1;
buf[1] = 20;
buf[2] = 2;
buf[3] = 0;
buf[4] = 0;
buf[5] = 0;
buf[6] = 0;
buf[7] = 0;
write(buf, 0, 8);
buf[0] = 76;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
write(buf, 0, 4);
bytestream.close();
}

private final int write(byte source[], int srcoffset, int length)
{
int newoffset = offset - length;
if(newoffset < 0)
{
length += newoffset;
srcoffset -= newoffset;
newoffset = 0;
}
int atbuff = newoffset;
int atsrc = srcoffset;
for(int i = 0; i < length; i++)
{
answer[atbuff] = source[atsrc];
atbuff++;
atsrc++;
}

offset = newoffset;
return length;
}

private final int write(String text)
throws IOException
{
OutputStreamWriter writeout = new OutputStreamWriter(bytestream);
writeout.write(text, 0, text.length());
writeout.close();
byte bytes[] = bytestream.toByteArray();
bytestream.reset();
return write(bytes, 0, bytes.length);
}

private final int write(byte abyte)
{
int rc = 0;
if(offset > 0)
{
offset--;
answer[offset] = abyte;
rc = 1;
}
return rc;
}

private final int writeForward(int value, int length)
{
int i = 0;
int rc = 0;
for(; i < length && i < 4; i++)
{
byte abyte = (byte)value;
rc += write(abyte);
value >>>= 8;
}

return rc;
}

private final int writeBackward(int value, int count)
{
int rc = 0;
byte tmp[] = new byte[count];
int i;
for(i = 0; i < count && i < 4; i++)
{
tmp[i] = (byte)value;
value >>>= 8;
}

return write(tmp, 0, i);
}

private final String getRelativePath(String from, String to)
{
int flen = from.length();
int tlen = to.length();
char sep = File.separatorChar;
int prevsep = -1;
int sepcount = 0;
StringBuffer fromb = new StringBuffer(from);
StringBuffer tob = new StringBuffer(to);
int i = flen - 1;
if(fromb.charAt(i) != sep)
{
fromb.append(sep);
flen++;
}
i = tlen - 1;
if(tob.charAt(i) != sep)
{
tob.append(sep);
tlen++;
}
i = 0;
for(boolean ok = true; ok && i < flen && i < tlen; i++)
{
char achar = Character.toLowerCase(fromb.charAt(i));
if(achar == Character.toLowerCase(tob.charAt(i)))
{
if(achar == sep)
{
prevsep = i;
sepcount++;
}
}
else
{
ok = false;
}
}

if(sepcount == 0)
{
return null;
}
if(i == flen && i == tlen)
{
return "";
}
prevsep++;
fromb.delete(0, prevsep);
tob.delete(0, prevsep);
int state = 0;
for(i = 0; i < fromb.length()
{
char achar = fromb.charAt(i);
if(achar == sep)
{
state = 0;
i++;
}
else
if(state < 2)
{
fromb.setCharAt(i, '.');
state++;
i++;
}
else
{
fromb.deleteCharAt(i);
}
}

i = fromb.length() - 1;
if(i > 0 && fromb.charAt(i) != sep)
{
fromb.append(sep);
}
fromb.append(tob.toString());
flen = fromb.length();
i = flen - 1;
if(fromb.charAt(i) == sep)
{
fromb.delete(i, flen);
}
return fromb.toString();
}

private final void mkpath(File file)
throws IOException
{
ArrayList need = new ArrayList();
File check = file.getParentFile();
int max = 0;
for(; !check.exists(); check = check.getParentFile())
{
max++;
need.add(check);
}

for(int i = 0; i < max; i++)
{
check = (File)need.get(i);
check.mkdir();
need.set(i, null);
}

check = null;
need = null;
}

private final void writeFile(File dest)
throws IOException
{
int len = answer.length - offset;
FileOutputStream ds = new FileOutputStream(dest);
ds.write(answer, offset, len);
ds.close();
}


//public static void main(String args[])
//{
//(String fulllinkpath,String fulltargetpath,String arguments,String workingdirectory,String iconfile)

//try{
//WindowsShortcut a=new WindowsShortcut("c:/a.lnk","c:/sandro/winhelp.exe","","c:/","c:/icon.ico");
//}
//catch(Exception e){}
//}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating a link