Mysterious files when converting Windows Internet Shortcuts

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Mysterious files when converting Windows Internet Shortcuts

Postby TheDoctor » Sat Apr 01, 2006 11:56 am

I'm in the process of migrating from Windows to Linux (OpenSuSE 10.0 using KDE). One of the problems is that format of a Windows Internet Shortcut (identified via a .url extension) is different to its Linux equivalent.

I've written a script to convert the one to the other. It works, but when it's run it produces, in addition to its intended results, two empty files called 1 and 2.

This is no big deal in itself. I have to clean up the directory anyway. But I don't like mysterious side effects, since they have a habit of jumping up and biting you when you least expect it. I particular, I'm worried that, in the unlikely case that I have files called 1 and 2 in a directory, I'll end up with important data overwritten.

The script goes as follows:

#!/bin/sh
# convert Windows Internet shortcuts
# into their Linux equivalent
# March 2006

iscreate() {
# Creates the Linux desktop link

# First: concatenate the parts of the link name
fname=""
if [ $# -gt 2 ]
then
while [ $# -gt 2 ]
do
fname=`echo $fname " " $1`
shift
done
fname=`echo $fname " "`
fi
fname=`echo $fname $1`

# Then create the desktop link file
echo "[Desktop Entry]" > $fname.desktop
echo "Encoding=UTF-8" >> $fname.desktop
echo "Type=Link" >> $fname.desktop
echo "Name=$fname" >> $fname.desktop
echo "URL=$2" >> $fname.desktop

echo "Converted \"$fname\": $2"
}

isgetlink() {
# Reads each link and passes the details
# to the desktop lin creation function

count=0
read
while [ $? = 0 ]
do
iscreate $REPLY
count=`expr $count + 1`
read
done

if [ $count > 1 ]
then
echo "$count Internet shortcuts converted"
fi
}

cd "$1"
grep ^URL= *.url | awk 'BEGIN{FS=".url:URL="}; {print $1 " " $2}' | isgetlink


I've been told that my code is difficult to follow, so here's what it does:

The purpose of the script is to take all the Windows Internet Shortcuts in a folder and create desktop links pointing to the same urls. It can either be run inside the directory containing the Windows Shortcuts or with the directory as a parameter.

~ If a directory has been specified in the command, the working directory is changed to that.

~ The grep command extracts all the lines from the .url files that specify the actual urls they link to and pipes them to the awk command.

~ Because the default output from grep prepends the filename, then each line that is presented to the awk comand begins with the filename and ends with the url. These are separated by a specific string (which can't appear anywhere else on the line), so awk can be made to output lines in the format filename space url. These lines are piped to the function isgetlink.

~ isgetlink simply reads the output from awk and uses each line of output as a parameter to the function iscreate.

~ Because the filename can contain spaces, iscreate may read it as more than one parameter. So the function begins by putting the pieces of the filename back together again. Because I've put a space between the filename and the url, then I know the last parameter - however many parameters there are - is the url. (If the url contains a space, it's held as "%20".) So the filename consists of all the parameters, except the last one, concatenated with a space in between each pair.

~ Once iscreate has the filename and the url, it creates the desktop link.

Simple, and reminds me of why I fell in love with Unix in my first brief encounter with HP-UX some twenty years ago. And it works. But I'd like to know why I'm getting those two unasked-for files.

Anyone got any ideas?
TheDoctor
LXF regular
 
Posts: 325
Joined: Mon Jan 02, 2006 9:02 pm

RE: Mysterious files when converting Windows Internet Shortc

Postby MartyBartfast » Sat Apr 01, 2006 1:03 pm

Its

if [ $count > 1 ]

that's doing it.

Effectively this is executing a command (where $count is 2)
2 > 1

the output from this command is null and will go to a file called 1.

The if statement is evaluating the status of issuing a command 2, which I think will always give a status of 127, which equates to "true", therefore the statements in the if block will always be executed, regardles of the value of $count.

what you should have is

if [ $count -gt 1 ]


Try it for yourself by entering this command in a console terminal:


ls -l 1
if [ 2 > 1 ]; then echo "ok"; fi
ls -l 1

then try

if [ 2 -gt 1 ]; then echo "ok"; fi

HTH
Martyn

Edited several times to try and explain better :?
User avatar
MartyBartfast
LXF regular
 
Posts: 816
Joined: Mon Aug 22, 2005 7:25 am
Location: Hants, UK

Postby TheDoctor » Sat Apr 01, 2006 4:22 pm

Damn! I've not even looked again at my code and I can see you're right. I was forgetting which programming language I was using.

Thank you muchly. :)

Added later: The more I think about it, the more I feel that, had this not been pointed out to me, it would definitely have lept out and bit me sooner or later.
TheDoctor
LXF regular
 
Posts: 325
Joined: Mon Jan 02, 2006 9:02 pm

Postby TheDoctor » Sat Apr 01, 2006 6:11 pm

In fact, the script as originally posted will not work with just one .url file in a directory. This is because the default for grep is to print the filename, except when it only finds one matching file, when it omits it.

To make the script work in all cases, grep has to have a -H option, to force the filename into the output every time.

Just in case anyone finds themself in the same position as me, the fully-working script, with no side-effects, is:

#!/bin/sh
# convert Windows Internet shortcuts
# into their Linux equivalent
# March 2006

iscreate() {
# Creates the Linux desktop link

# First: concatenate the parts of the link name
fname=""
if [ $# -gt 2 ]
then
while [ $# -gt 2 ]
do
fname=`echo $fname " " $1`
shift
done
fname=`echo $fname " "`
fi
fname=`echo $fname $1`

# Then create the desktop link file
echo "[Desktop Entry]" > $fname.desktop
echo "Encoding=UTF-8" >> $fname.desktop
echo "Type=Link" >> $fname.desktop
echo "Name=$fname" >> $fname.desktop
echo "URL=$2" >> $fname.desktop

echo "Converted \"$fname\": $2"
}

isgetlink() {
# Reads each link and passes the details
# to the desktop lin creation function

count=0
read
while [ $? -eq 0 ]
do
iscreate $REPLY
count=`expr $count + 1`
read
done

if [ $count -gt 1 ]
then
echo "$count Internet shortcuts converted"
fi
}

cd "$1"
grep -H ^URL= *.url | awk 'BEGIN{FS=".url:URL="}; {print $1 " " $2}' | isgetlink


(If you use it on a directory that conatins no .url files, then it wil fall over, telling you that there is no such file as *.url. Life's too short to code sophisticated eror handlers for one-off conversion programs!)
TheDoctor
LXF regular
 
Posts: 325
Joined: Mon Jan 02, 2006 9:02 pm

Postby towy71 » Sat Apr 01, 2006 6:21 pm

Surely the secret is not to use any of the microparp stuff ;-)
still looking for that door into summer
User avatar
towy71
Moderator
 
Posts: 4264
Joined: Wed Apr 06, 2005 2:11 pm
Location: wild West Wales

Postby MartyBartfast » Sat Apr 01, 2006 6:43 pm

Rereading the OP I noticed you said :

when it's run it produces, in addition to its intended results, two empty files called 1 and 2.

Does it really/still produce a file named 2? or was that coming from some previous incarnation?
I have been touched by his noodly appendage.
User avatar
MartyBartfast
LXF regular
 
Posts: 816
Joined: Mon Aug 22, 2005 7:25 am
Location: Hants, UK

Postby TheDoctor » Sat Apr 01, 2006 6:47 pm

towy71 wrote:Surely the secret is not to use any of the microparp stuff ;-)


That's a bit like, when giving directions to a stranger who's lost, telling them not to start from here. :)

If, like me, you've got a load of useful links that you've accumulated over the years while using Windows, then migrating involves taking them along.

Sure it might have been better for me to have used Linux in the first place. But I didn't. I used DOS and then Windows. I began collecting links while using Windows 3.1. And that's where I have to start my journey from.
TheDoctor
LXF regular
 
Posts: 325
Joined: Mon Jan 02, 2006 9:02 pm

Postby jhooks » Wed Apr 05, 2006 10:13 am

TheDoctor wrote:
towy71 wrote:Surely the secret is not to use any of the microparp stuff ;-)


That's a bit like, when giving directions to a stranger who's lost, telling them not to start from here. :)


I suppose the other option is stay where you are - using microparp stuff :twisted:

Nice script by the way.
jhooks
 
Posts: 48
Joined: Sat Jan 07, 2006 5:39 pm

Postby halsboss » Sun Feb 04, 2007 6:22 am

A 1-day newbie to Ubuntu query
I have lots of these windows .URL links from my windows hard drive ...

I pasted the 2nd script as test.sh into a folder of them and double clicked on it and selected "Run" from the pop-up menu but nothing happened (no files in the same folder) ... help, anyone ?

Thanks
halsboss
 
Posts: 3
Joined: Sun Feb 04, 2007 6:20 am


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests