Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Mysterious files when converting Windows Internet Shortcuts

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
TheDoctor
LXF regular


Joined: Mon Jan 02, 2006 9:02 pm
Posts: 325

PostPosted: Sat Apr 01, 2006 12:56 pm    Post subject: Mysterious files when converting Windows Internet Shortcuts Reply with quote

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:

Quote:

#!/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?
Back to top
View user's profile Send private message
MartyBartfast
LXF regular


Joined: Mon Aug 22, 2005 8:25 am
Posts: 814
Location: Hants, UK

PostPosted: Sat Apr 01, 2006 2:03 pm    Post subject: RE: Mysterious files when converting Windows Internet Shortc Reply with quote

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 Confused
Back to top
View user's profile Send private message
TheDoctor
LXF regular


Joined: Mon Jan 02, 2006 9:02 pm
Posts: 325

PostPosted: Sat Apr 01, 2006 5:22 pm    Post subject: Reply with quote

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. Smile

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.
Back to top
View user's profile Send private message
TheDoctor
LXF regular


Joined: Mon Jan 02, 2006 9:02 pm
Posts: 325

PostPosted: Sat Apr 01, 2006 7:11 pm    Post subject: Reply with quote

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:

Quote:

#!/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!)
Back to top
View user's profile Send private message
towy71
Moderator


Joined: Wed Apr 06, 2005 3:11 pm
Posts: 4257
Location: wild West Wales

PostPosted: Sat Apr 01, 2006 7:21 pm    Post subject: Reply with quote

Surely the secret is not to use any of the microparp stuff Wink
_________________
still looking for that door into summer
Back to top
View user's profile Send private message
MartyBartfast
LXF regular


Joined: Mon Aug 22, 2005 8:25 am
Posts: 814
Location: Hants, UK

PostPosted: Sat Apr 01, 2006 7:43 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
TheDoctor
LXF regular


Joined: Mon Jan 02, 2006 9:02 pm
Posts: 325

PostPosted: Sat Apr 01, 2006 7:47 pm    Post subject: Reply with quote

towy71 wrote:
Surely the secret is not to use any of the microparp stuff Wink


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

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.
Back to top
View user's profile Send private message
jhooks



Joined: Sat Jan 07, 2006 5:39 pm
Posts: 48

PostPosted: Wed Apr 05, 2006 11:13 am    Post subject: Reply with quote

TheDoctor wrote:
towy71 wrote:
Surely the secret is not to use any of the microparp stuff Wink


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


I suppose the other option is stay where you are - using microparp stuff Twisted Evil

Nice script by the way.
Back to top
View user's profile Send private message
halsboss



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

PostPosted: Sun Feb 04, 2007 6:22 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast