473,379 Members | 1,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

if file exist

how can i check if file exist in current directory (in javascript)?

--
pozdr;)
zhisol
Jul 20 '05 #1
12 38917
In article <Xn************************@195.136.250.206>, zh****@o2.pl
enlightened us with...
how can i check if file exist in current directory (in javascript)?


This isn't really possible with normal JS in a normal security
environment.

Did you mean on the server or on the client?
Is this for a web page or an intranet application, CD-ROM, etc...?
-------------------------------------------------
~kaeli~
Hey, if you got it flaunt it! If you don't, stare
at someone who does. Just don't lick the TV screen,
it leaves streaks.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #2
kaeli <in********************@NOSPAMatt.net> wrote in
news:MP************************@nntp.lucent.com:
This isn't really possible with normal JS in a normal security
environment.

Did you mean on the server or on the client?
Is this for a web page or an intranet application, CD-ROM, etc...?


it's for web page. i mean exactly that:

if("cover.jpg" exist)
document.write('<img src='cover.jpg'>')
else
//display nothing

and i don't know how can i realise "if("cover.jpg" exist)"

for example i have address: http://mysite.com/show.html
and if in root on server is a file "cover.jpg", make this visable,
otherwise not.

normally in html if "cover.jpg" doesn't exist - client see empty box, but
i want that he'll see nothing

ps: sorry for my english:)
--
pozdr;)
zhisol
Jul 20 '05 #3
In article <Xn************************@195.136.250.206>, zh****@o2.pl
says...
how can i check if file exist in current directory (in javascript)?


Do you have server-side JavaScript or ASP with JavaScript facilities?
If you do, have a look at http://www.4guysfromrolla.com/
--
Hywel I do not eat quiche
http://hyweljenkins.co.uk/
http://hyweljenkins.co.uk/mfaq.php
Jul 20 '05 #4
zhisol <zh****@o2.pl> writes:
if("cover.jpg" exist)
document.write('<img src='cover.jpg'>')
else
//display nothing
This will not work, since checking whether it exists will be
asynchroneous. You will not be able to wait for the result before
writing the image tag.
and i don't know how can i realise "if("cover.jpg" exist)"
What you can do is write the image tag with a src pointing to an empty
image, and then change the src if cover.jpg exists.

function setIfExists(id,src) {
var img = new Image();
img.onload = function () {
document.images[id].src = src;
};
img.onerror = function () {
// do something if images doesn't exist.
}
img.src = src;
}

for example i have address: http://mysite.com/show.html
and if in root on server is a file "cover.jpg", make this visable,
otherwise not.

normally in html if "cover.jpg" doesn't exist - client see empty box, but
i want that he'll see nothing


You can also start the image out hidden (either visibility:hidden or
display:none) and change it to something visible if the image exists.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
Lasse Reichstein Nielsen wrote:
if("cover.jpg" exist)
document.write('<img src='cover.jpg'>')
This will not work, since checking whether it exists will be
asynchroneous. You will not be able to wait for the result before
writing the image tag.


Huh? As it happens, I was going to ask almost exactly the same
question. Your answer makes no sense to me. I mean, in vbscript I can
read files, directories, etc. But it doesn't work in JavaScript? Wow.
You don't mean client side vs. server side, do you?

I need a way to discover the path of the running script's file (on the
server), read the directory said script resides in, and manipulate the
files there in a fairly simple way. This is all server side, of course.

In vbscript I can do something like this:

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
pathXlat = Request.ServerVariables("PATH_TRANSLATED")
pathXlatRoot = Left( pathXlat, InStrRev( pathXlat, "\" ) )
Response.Write "My Path: " & pathXlatRoot & "index.html <br>"

and the path shows up just fine. I can also step through the files or
folders in my subdirectory like so:

Set objFolder = objFSO.GetFolder( pathXlatRoot )
Set colFolders = objFolder.SubFolders
For Each objSubFolder in colFolders
If FileExists( objSubFolder, "a_name" ) Then
call do_something()
End If
Next

No problemo. Anyone know the javascript equivalent? I'm fairly new,
the above was painstakingly hacked out, and I've only got VB references
handy. Web searches aren't really helping.
Jul 20 '05 #6
Mark Space <ma**********@hotmail.com> writes:
Huh? As it happens, I was going to ask almost exactly the same
question. Your answer makes no sense to me. I mean, in vbscript I
can read files, directories, etc. But it doesn't work in JavaScript?
Wow. You don't mean client side vs. server side, do you?
Yes. As I read the question (which can be incorrect, it is not very
precisely stated), the original poster wants to check whether an image
exists in the root of the server, and if so, insert an img tag with
document.write. You can only use document.write on the client, so
the check must be performed on the client and check the existence of
a file on the server. For that, my answer is correct.

If you are talking server side scripting, which is probably ASP then,
I would recommend an ASP-specific group, since it is not really about
Javascript.
In vbscript I can do something like this:

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
pathXlat = Request.ServerVariables("PATH_TRANSLATED")
pathXlatRoot = Left( pathXlat, InStrRev( pathXlat, "\" ) )
Response.Write "My Path: " & pathXlatRoot & "index.html <br>" and the path shows up just fine. I can also step through the files or
folders in my subdirectory like so:

Set objFolder = objFSO.GetFolder( pathXlatRoot )
Set colFolders = objFolder.SubFolders
For Each objSubFolder in colFolders
If FileExists( objSubFolder, "a_name" ) Then
call do_something()
End If
Next

No problemo. Anyone know the javascript equivalent?


I'll assume that the ASP objects work the same. If that is true, then
the following should work in JScript:

var objFSO = Server.CreateObject("Scripting.FileSystemObject");
var pathXlat = Request.ServerVariables("PATH_TRANSLATED");
var pathXlatRoot = pathXlat.substring(0,pathXlat.lastIndexOf("/")+1);
Response.Write("My Path: " + pathXlatRoot + "index.html <br>");

and

var objFolder = objFSO.GetFolder(pathXlatRoot);
var colFolders = objFolder.subFolders;
for (var index in colFolders) {
var objSubFolder = colFolders[index];
if (FileExists(objSubFolder, "a_name")) {
do_something();
}
}

This would be the equivelent JScript code.
Maybe the FileExists function isn't global in JScript. I would expect
it to be a method of the file system object.
Maybe the "for(...in...)" will include too many properties, and
it would be better to do
for (var index = 0; index < colFolders.length ; i++) {
That depends on how the colFolders object is designed. If it is an array,
the simple version should work.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7
zhisol wrote:
[...snip...]

it's for web page. i mean exactly that:

if("cover.jpg" exist)
document.write('<img src='cover.jpg'>')
else
//display nothing

and i don't know how can i realise "if("cover.jpg" exist)"

The HTTPRequest object could be useful to see if file is there or not.
Combine this with the idea of starting out showing an "empty" image or
maybe an invisible image.

Send a HEAD request for the "cover.jpg" file. In the handler that is
triggered when the response comes back, check for 200 response code &
then make the image visible at that point.
See:
http://jibbering.com/2002/4/httprequest.html
HTH
Stephen
for example i have address: http://mysite.com/show.html
and if in root on server is a file "cover.jpg", make this visable,
otherwise not.

normally in html if "cover.jpg" doesn't exist - client see empty box, but
i want that he'll see nothing

ps: sorry for my english:)


Jul 20 '05 #8
Lasse Reichstein Nielsen wrote:
Mark Space <ma**********@hotmail.com> writes:
You can only use document.write on the client, so
the check must be performed on the client and check the existence of
a file on the server. For that, my answer is correct.
Oh, right. It's been a while since I've done this, I missed the
difference between document.write and response.write.

If you are talking server side scripting, which is probably ASP then,
I would recommend an ASP-specific group, since it is not really about
Javascript.
That was actually my next question. Which is better for server side
scripting, VBScript or Javascript? It seemed to me that Javascript
actually had more standards that recognized it, and I wondered if it was
more popular than MS only VBScript. I've only done VBScript so far.
(CGI like Perl or PHP really aren't an option for this project.)
That depends on how the colFolders object is designed. If it is an array,
the simple version should work.
Just FYI, colFolders is a "collection", which is a built in data type in
VBScript. It's kinda like an associative array. It associates
key/value data pairs.

/L


Um, shouldn't that be "</L>"? ;)

Jul 20 '05 #9
Hi Mark,

Mark Space wrote:
Lasse Reichstein Nielsen wrote:

If you are talking server side scripting, which is probably ASP then,
I would recommend an ASP-specific group, since it is not really about
Javascript.

That was actually my next question. Which is better for server side
scripting, VBScript or Javascript? It seemed to me that Javascript
actually had more standards that recognized it, and I wondered if it was
more popular than MS only VBScript. I've only done VBScript so far.
(CGI like Perl or PHP really aren't an option for this project.)


VBScript is probably more used than JavaScript on ASP. I don't know the
actual figures though. Most examples you will find on the Web for ASP
development are also written in VBScript.

Which doesn't matter much, actually, since the API is exactly the same
for VBScript and JScript (just like the API in .NET is exactly the same
if you programe in VB.NET or C#, or any other supported language).

For this reason, I rather recommend using JScript on ASP. It has the
great advantage to allow you to use the same language, same syntax for
server-side and client-side scripting. Besides, and though it's a
controversial point, the JScript syntax is C-like, and IMHO makes more
sense than the VB syntax. If you know JScript, you will feel comfortable
in C, C++, Java, C#. If you know VB, you'll feel comfortable in... VB ;-)

Of course it's a personal choice in the end. Technically, there are no
differences. It's more a question of gut feeling, and of what languages
you already know, and plan to use in the future.

HTH,

Laurent
(currently involved in a .NET project in VB.NET and C#, and constantly
reminded that C# is nicer ;-)
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #10
Stephen <ss*****@austin.rr.com> wrote in
news:oQ*******************@twister.austin.rr.com:
Send a HEAD request for the "cover.jpg" file. In the handler that is
triggered when the response comes back, check for 200 response code &
then make the image visible at that point.
<script language="JavaScript">
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("HEAD", "cover.jpg", false);
xmlHttp.send();
document.write(xmlHttp.statusText);
</script>

is that correct to see return code on the browser screen?
i see nothing :(
http://jibbering.com/2002/4/httprequest.html

it seems, this web page doesn't exist

--
pozdr;)
zhisol
Jul 20 '05 #11
zhisol wrote:
Stephen <ss*****@austin.rr.com> wrote in
news:oQ*******************@twister.austin.rr.com:

Send a HEAD request for the "cover.jpg" file. In the handler that is
triggered when the response comes back, check for 200 response code &
then make the image visible at that point.

<script language="JavaScript">
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("HEAD", "cover.jpg", false);
xmlHttp.send();
document.write(xmlHttp.statusText);
</script>

is that correct to see return code on the browser screen?
i see nothing :(


Should work. Note:

xmlHttp.status should give, e.g., "200"
xmlHttp.statusText should give the associated text, e.g., "OK"

This works for me in my environment, just as you coded it above (except
for substituting an image name I know is on my system).

Server must be configured to allow HEAD requests. Most probably are, but
you'll have to make sure in your case.

You probably also want to check for other possible response codes that
indicate the requested entity is present, e.g., 304.

Regards,
Stephen
http://jibbering.com/2002/4/httprequest.html


it seems, this web page doesn't exist


Jul 20 '05 #12
zhisol wrote:
Stephen <ss*****@austin.rr.com> wrote in
news:oQ*******************@twister.austin.rr.com:

Send a HEAD request for the "cover.jpg" file. In the handler that is
triggered when the response comes back, check for 200 response code &
then make the image visible at that point.

<script language="JavaScript">
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("HEAD", "cover.jpg", false);
xmlHttp.send();
document.write(xmlHttp.statusText);
</script>

is that correct to see return code on the browser screen?
i see nothing :(


Should work. Note:

xmlHttp.status should give, e.g., "200"
xmlHttp.statusText should give the associated text, e.g., "OK"

This works for me in my environment, just as you coded it above (except
for substituting an image name I know is on my system).

Server must be configured to allow HEAD requests. Most probably are, but
you'll have to make sure in your case.

You probably also want to check for other possible response codes that
indicate the requested entity is present, e.g., 304.

Regards,
Stephen

P.S.: need document.close() after the document.write(...)?? Or try
alert instead of document.write()...?
S.
http://jibbering.com/2002/4/httprequest.html


it seems, this web page doesn't exist

Jul 20 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: lamar_air | last post by:
I have a python script and i want to delete a file on my computer c:\....\...\.. .txt what's the script to do this? I know how to write to files but i also need to know how to delete a file.
2
by: Chris Fink | last post by:
I am using the System.IO.File class to determine if a file exists on a network share. The File.Exists method keeps returning false, even though the file does exist. The MSDN documentation...
4
by: Mike | last post by:
Hi, I am looking for function in .Net library that let me know if exist any file if I specified template. Eg: I specify "*.txt" and if any file (1.txt, 2.txt, .. ) exists then I can get True...
1
by: Tim Failes | last post by:
This seems a trival question, but I cannot get it to work properly... Essentially my question is, how can I create a text file, and guarantee it is given the current date/time as the Creation Time?...
52
by: paytam | last post by:
Hi all Can anyone tell me how can I check that a file exist or no.I mean when you use this commands FILE *fp; if(!fp) //Could not open the file doen't show why it can not open it,may be the...
3
by: tshad | last post by:
I have a function that downloads a file to the users computer and it works fine. The problem is that I then want the program to rename the file (file.move) to the same name plus todays date. ...
26
by: Army1987 | last post by:
Is this a good way to check wheter a file already exists? #include <stdio.h> #include <stdlib.h> int ask(const char *prompt); typedef char filename; int main(int argc, char *argv) { FILE...
7
by: sprash | last post by:
Newbie question: I'm trying to determine if a file physically exists regardless of the permissions on it Using File.Exists() returns false if it physically exists but the process does not...
3
by: brook | last post by:
hey all - im new to php and having trouble writing a simple code which should create a file. here is the most simplified version: <?php $content = "my content"; $path = "test.txt";...
65
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.