Jump to content


Photo

Directory File Help Thing :P


  • Please log in to reply
13 replies to this topic

#1 cjburkey01

cjburkey01

    Member

  • Members
  • PipPip
  • 14 posts

Posted 21 February 2014 - 05:44 PM

Hello people!

I have this bit of code:

<?php
function drawArray(DirectoryIterator $directory)
{
    $result=array();
    foreach($directory as $object)
    {
        if($object->isDir()&&!$object->isDot())
        {
            $result[$object->getFilename()]=drawArray(new DirectoryIterator($object->getPathname()));
        }
        else if($object->isFile())
        {
            $result[]=$object->getFilename();
        }
    }
    return $result;
}
$array=drawArray(new DirectoryIterator('mods'));
$info = implode("\n",$array);
file_put_contents("mods.txt", $info);
 
$array2 = drawArray(new DirectoryIterator('config'));
$info2 = implode("\n",$array2);
file_put_contents("config.txt", $info2);
 
function remove($Filename, $key) {
$fc=file($Filename);
$f=fopen($Filename,"w");
foreach($fc as $line)
{
if (!strstr($line,$key))
fputs($f,$line);
}
fclose($f);
}
 
remove("mods.txt", "Array");
remove("mods.txt", ".DS_Store");
remove("config.txt", "Array");
remove("config.txt", ".DS_Store");
?>

It works, but I have directories in the folder it's searching, how would I make it print the directories to the file in this formate:

directory/fileInIt.cfg
?

#2 WebStyles

WebStyles

    Advanced Member

  • Members
  • PipPipPip
  • 1,215 posts
  • LocationEurope

Posted 21 February 2014 - 06:19 PM

I know this is not the answer to your question, but it seems you have other issues to deal with first. You're reading through the directories, adding everything into an array, converting that array to a string, creating a file(s), writing the contents of that string to the file(s), then opening that file again, reading each line and removing things you didn't want and writing back the ones you do want... Do you see a problem here?

 

Remember: Simpler is better.


Edited by WebStyles, 21 February 2014 - 06:20 PM.

Need a Web Developer? Check out ClickWorks.org | I work on a PAY-WHAT-YOU-WANT basis (because the world needs a change)

 

"If it looks like a duck, and quacks like a duck, we have at least to consider the possibility that we have a small aquatic bird of the family anatidae on our hands!"

-- Douglas Adams


#3 cjburkey01

cjburkey01

    Member

  • Members
  • PipPip
  • 14 posts

Posted 21 February 2014 - 06:25 PM

I know this is not the answer to your question, but it seems you have other issues to deal with first. You're reading through the directories, adding everything into an array, converting that array to a string, creating a file(s), writing the contents of that string to the file(s), then opening that file again, reading each line and removing things you didn't want and writing back the ones you do want... Do you see a problem here?

 

Remember: Simpler is better.

Yes, I now see that problem, so would be more efficient to create the file, convert, insert, delete, then close, instead of reopening and everything?



#4 WebStyles

WebStyles

    Advanced Member

  • Members
  • PipPipPip
  • 1,215 posts
  • LocationEurope

Posted 21 February 2014 - 06:28 PM

would be more efficient to remove unwanted stuff from the array BEFORE even writing to the file.


Need a Web Developer? Check out ClickWorks.org | I work on a PAY-WHAT-YOU-WANT basis (because the world needs a change)

 

"If it looks like a duck, and quacks like a duck, we have at least to consider the possibility that we have a small aquatic bird of the family anatidae on our hands!"

-- Douglas Adams


#5 WebStyles

WebStyles

    Advanced Member

  • Members
  • PipPipPip
  • 1,215 posts
  • LocationEurope

Posted 21 February 2014 - 06:34 PM

can I ask exactly why you're doing that? (you're obviously trying to map the directories) but is is a one-time thing you need to do, or is it a script that will run for every user or something like that? 

 

(I'm asking because if it's a one-time thing you can simply type: find mods > mods.txt at the command prompt (I'm assuming it's a linux box) and it will map out all the files for you.)


Need a Web Developer? Check out ClickWorks.org | I work on a PAY-WHAT-YOU-WANT basis (because the world needs a change)

 

"If it looks like a duck, and quacks like a duck, we have at least to consider the possibility that we have a small aquatic bird of the family anatidae on our hands!"

-- Douglas Adams


#6 cjburkey01

cjburkey01

    Member

  • Members
  • PipPip
  • 14 posts

Posted 21 February 2014 - 06:52 PM

can I ask exactly why you're doing that? (you're obviously trying to map the directories) but is is a one-time thing you need to do, or is it a script that will run for every user or something like that? 

 

(I'm asking because if it's a one-time thing you can simply type: find mods > mods.txt at the command prompt (I'm assuming it's a linux box) and it will map out all the files for you.)

It's a one time thing, well, unless I update the file list.  I'm using a java program that reads the mods.txt, gets the url, and dwnloads each file(More proof-of-concept than functional :P)so, I wanted to run that php everytime I add a new file, (And I didn't want to list the files on my own) :P



#7 WebStyles

WebStyles

    Advanced Member

  • Members
  • PipPipPip
  • 1,215 posts
  • LocationEurope

Posted 21 February 2014 - 06:57 PM

try this (it reads through the directories, and writes everything to the text files without having to create an array.)

 

 

<?php
function drawArray(DirectoryIterator $directory, $file ){
  $path = '';
  foreach ( $directory as $object ){
    if ($object->isDir() && !$object->isDot()){
      $path .= $object->getFilename() .'/'; 
      $result[$object->getFilename()] = drawArray( new DirectoryIterator( $object->getPathname() ), $file );
    }else if($object->isFile() && $object != '.DS_Store'){
      $line = $path . $object->getFilename()."\n";
      file_put_contents($file,$line,FILE_APPEND);
    }
  }
}
drawArray( new DirectoryIterator( 'mods' ), 'mods.txt' );
drawArray( new DirectoryIterator( 'config' ), 'config.txt' );
?>

(it appends each line to the files as it reads through the directory, so you should delete the previous files first (or empty them, I'm sure you can figure out how to add that) ... Just realized there's still a little bug in there, but you get the idea.

 

Hope it helps :)


Edited by WebStyles, 21 February 2014 - 07:01 PM.

Need a Web Developer? Check out ClickWorks.org | I work on a PAY-WHAT-YOU-WANT basis (because the world needs a change)

 

"If it looks like a duck, and quacks like a duck, we have at least to consider the possibility that we have a small aquatic bird of the family anatidae on our hands!"

-- Douglas Adams


#8 cjburkey01

cjburkey01

    Member

  • Members
  • PipPip
  • 14 posts

Posted 21 February 2014 - 08:16 PM

try this (it reads through the directories, and writes everything to the text files without having to create an array.)

 

 

<?php
function drawArray(DirectoryIterator $directory, $file ){
  $path = '';
  foreach ( $directory as $object ){
    if ($object->isDir() && !$object->isDot()){
      $path .= $object->getFilename() .'/'; 
      $result[$object->getFilename()] = drawArray( new DirectoryIterator( $object->getPathname() ), $file );
    }else if($object->isFile() && $object != '.DS_Store'){
      $line = $path . $object->getFilename()."\n";
      file_put_contents($file,$line,FILE_APPEND);
    }
  }
}
drawArray( new DirectoryIterator( 'mods' ), 'mods.txt' );
drawArray( new DirectoryIterator( 'config' ), 'config.txt' );
?>

(it appends each line to the files as it reads through the directory, so you should delete the previous files first (or empty them, I'm sure you can figure out how to add that) ... Just realized there's still a little bug in there, but you get the idea.

 

Hope it helps :)

So, I wouldn't need the delete function. Right?



#9 WebStyles

WebStyles

    Advanced Member

  • Members
  • PipPipPip
  • 1,215 posts
  • LocationEurope

Posted 21 February 2014 - 08:26 PM

What would you want to delete?


Need a Web Developer? Check out ClickWorks.org | I work on a PAY-WHAT-YOU-WANT basis (because the world needs a change)

 

"If it looks like a duck, and quacks like a duck, we have at least to consider the possibility that we have a small aquatic bird of the family anatidae on our hands!"

-- Douglas Adams


#10 cjburkey01

cjburkey01

    Member

  • Members
  • PipPip
  • 14 posts

Posted 21 February 2014 - 11:11 PM

What would you want to delete?

Exactly ;)



#11 cjburkey01

cjburkey01

    Member

  • Members
  • PipPip
  • 14 posts

Posted 21 March 2014 - 07:17 PM

Um, when I use this file, it works for everything, but if the file is nested once in a directory, it does not put that directory in the file list.  So, you could have a file like this:

config/cofh/core.cfg

But it only shows as this:

core.cfg

But if it is further, it work:

/config/cofh/core/file/thing.cfg

would list like

core/file/thing/core.cfg

I use this code:

<?php
function drawArray(DirectoryIterator $directory, $file ){
$path = '';
foreach ( $directory as $object ){
if ($object->isDir() && !$object->isDot()){
$path .= $object->getFilename() .'/'; 
$result[$object->getFilename()] = drawArray( new DirectoryIterator( $object->getPathname() ), $file );
}else if($object->isFile() && $object != '.DS_Store'){
$line = $path . $object->getFilename()."\n";
file_put_contents($file,$line,FILE_APPEND);
}
}
}
drawArray( new DirectoryIterator( 'mods' ), 'mods.txt' );
drawArray( new DirectoryIterator( 'config' ), 'config.txt' );
?>

Edited by cjburkey01, 21 March 2014 - 07:17 PM.


#12 cjburkey01

cjburkey01

    Member

  • Members
  • PipPip
  • 14 posts

Posted 22 March 2014 - 11:42 AM

Are we allowed to bump?  I forget :P



#13 cjburkey01

cjburkey01

    Member

  • Members
  • PipPip
  • 14 posts

Posted 22 March 2014 - 04:03 PM

Are we allowed to bump?  I forget :P

Seriously, are we?



#14 cjburkey01

cjburkey01

    Member

  • Members
  • PipPip
  • 14 posts

Posted 22 March 2014 - 05:27 PM

I'll bump then.

 

BUMP

BUMP

BUMP

BUMP

BUMP

BUMP

BUMP






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com