/    Sign up×
Community /Pin to ProfileBookmark

FileSystemObject: relative path trouble

Helo. I am writing a program for personal use with Javascript, that is it does not run from the internet, and in this program I have to work with files. For this I use the ActiveX FileSystemObject. The problem is that when I try to write the path of my file relative, like “data.txt”, or “.data.txt”, I get an error. By extensive investigation I found out that in these cases the browser tries to find the file on the Desktop… When I write absolute paths, however, everything is all right.

Any ideas what I may be doing wrong?

I would appreciate it is you sent an example…

Thanks a lot,

Martas

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@gphNov 26.2005 — I'm not sure if this is what you're after. This is what I use to locate an HTA and from there the files it requires.
<i>
</i>var app_path=location.href.toLocalPath().folderOf();

String.prototype.toLocalPath=function(){
return unescape(this.slice(this.search(/[A-Z]:/[^/]/i)).replace(///g,'\'))
};

String.prototype.folderOf=function(no_slash){
return this.slice(0,this.lastIndexOf('\',this.length-(/\$/.test(this)?2:0))+(no_slash?0:1))
};


There is a [url=http://msdn.microsoft.com/workshop/author/hta/reference/properties/commandline.asp]commandLine[/url] property but I've found it to be unreliable.
Copy linkTweet thisAlerts:
@martasauthorNov 26.2005 — umm.... to tell the truth, I did not really understand your code... *ashamed*

I think you got me wrong, though. whatever, here's my code:

fso = new ActiveXObject("Scripting.FileSystemObject");

filename = "data.txt";

file = fso.OpenTextFile(filename, ForReading, false);

rawFile = file.readAll();

file.Close();

//the above one does not work.

//but if I write absolute path in filename:

fso = new ActiveXObject("Scripting.FileSystemObject");

filename = "C:blablaimportantinfodata.txt";

file = fso.OpenTextFile(filename, ForReading, false);

rawFile = file.readAll();

file.Close();

//that works fine
Copy linkTweet thisAlerts:
@gphNov 26.2005 — Do you know the location of data.txt relative to the hta or htm file?
Copy linkTweet thisAlerts:
@martasauthorNov 27.2005 — Of course I do. As I said, everything works if I use absolute paths....
Copy linkTweet thisAlerts:
@gphNov 27.2005 — Of course I do. As I said, everything works if I use absolute paths....[/QUOTE]

That's what I figured. Therefore, what I posted [B][I]is[/I][/B] what you need.

does this help you understand ?
<i>
</i>String.prototype.toLocalPath=function(){
return unescape(this.slice(this.search(/[A-Z]:/[^/]/i)).replace(///g,'\'))
};

String.prototype.folderOf=function(no_slash){
return this.slice(0,this.lastIndexOf('\',this.length-(/\$/.test(this)?2:0))+(no_slash?0:1))
};

var app_path=location.href.toLocalPath().folderOf();

alert(app_path+'data.txt')
Copy linkTweet thisAlerts:
@martasauthorNov 28.2005 — ok thanksalot. will try that...
×

Success!

Help @martas spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...