1

I have a folder on the hard drive with files named

  1. myfileone_163637
  2. myfiletwo_14153535
  3. myfilethree_3739284

The numbers at the end of the filenames are random.

How can I check with JavaScript if the file containing "myfilethree" in its name is present?

Thank you in advance.

3 Answers3

3

if you have the file names you can use something like:

if(theFileName.indexOf('myfilethree') > -1)
{
    do your magic here because "myfilethree" is present in the name!
}

However, I am not aware of any way to read a directory index in javascript.

Kris
  • 40,604
  • 9
  • 72
  • 101
  • He may be using node.js, which has a module for filesystem interaction. – Sirko Oct 25 '12 at 12:24
  • @Sirko node.js supports `indexOf` by all means :) – VisioN Oct 25 '12 at 12:25
  • @VisioN The comment was just meant for the last sentence, which doubted a directory index function for JavaScript. Gave +1, anyway. – Sirko Oct 25 '12 at 12:29
  • I know node has lots of IO stuff but node does not equal javascript, nor is it mentioned in the question. – Kris Oct 25 '12 at 13:08
1

There are two parts to this: getting a list of files in a directory, and then searching them for a substring. I'll start with the sceond part, because that's easiest. Simply use indexOf to check for the desired string. If the result of indexOf is not equal to -1, you have a match:

if (filename.indexOf("myfilex") != -1) {
    // whatever you want to do
}

As to actually retrieving a list of files, you will need to use Windows Script Host and JScript. See this question for details on that.

Community
  • 1
  • 1
Ashley Strout
  • 6,107
  • 5
  • 25
  • 45
0
var str = 'myfilethree';
if (str.toLowerCase().indexOf("myfilethree") >= 0)
{
    alert('File Found');
}

Try This:

brightboy2004
  • 258
  • 1
  • 3