Jump to content
Sign in to follow this  
mschol

php script -> autoit script

Recommended Posts

mschol

i'm trying to convert a php script to a autoit script:

php:

function getFiles($path, $fileFilter) {
   $dirStack = array($path);
   $files = array();
   
 while (null !== ($dir = array_pop($dirStack)))
 {
  $dh = opendir($dir) or die('Error: Could not open directory: '.$dir);
  while (false !== ($file = readdir($dh)))
  {
   $skipDirs = array('.', '..', 'System Volume Information', 'Temporary Internet Files', 'Temp', 'WINDOWS');
   if (!in_array($file,$skipDirs))
   {
    $fullFile = $dir . $file;
             if (is_file($fullFile))
    {
     if (preg_match($fileFilter, $file))
        {
      $splitFullFile = $dir ."|". $file;
                    $files[] = $splitFullFile;
                 }
             } 
    else if (is_dir($fullFile))
    {
                 $dirStack[] = $fullFile . '/';
             }
          }
       }
  closedir($dh);
   }
   sort($files);
   return $files;
}

so far i have thought of the following code:

Func getFiles($path)
   dim $dirStack,$dir,$dh,$skipDirs,$file,$fullFile,$splitFullFile,$files
   $dirStack = _ArrayCreate($path);
 while $dir = _ArrayPop($dirStack)
  
  $dh = FileFindFirstFile($dir & "*.*")  
  
  while FileFindNextFile($dh);
   $skipDirs = _ArrayCreate('System Volume Information', 'Temporary Internet Files', 'Temp', 'WINDOWS');
   if Not _ArrayBinarySearch($skipDirs,$file) Then
    $fullFile = $dir & $file;
    if Not _isfolder($fullFile) Then
     if StringRegExp($file,"\.(?-iavi|mpg|ogm|mkv)$",0) Then
      $splitFullFile = $dir ."|". $file;
      if IsArray($files) Then
       $files[] = $files = _Arrayadd($files,$splitFullFile);
      Else
       $files = _ArrayCreate($splitFullFile)
      EndIf
     EndIf
    else
                $dirStack[] = $fullFile & '/';
             EndIf
   EndIf
       WEnd
  FileClose($dh);
 WEnd 
 
 Return $files
EndFunc

but i dont get any output from the function

anyone who can help me convert the php script to autoit (and i know about the _filelistToArrayEx() function, its just that i want a native autoit script that doesnt depend on a commandline in order to get file/folder lists)

Edited by mschol

Share this post


Link to post
Share on other sites
therks

*Edit: I also see a major problem. How many times do you read/compare the variable $file? Okay, now how many times do you SET it?

Check the help file on using FileFindNextFile. It has a very good example of how to use the function, cus you can't really use it in a while loop the way you are. PHP has a very handy feature in that you can set a variable, while at the same time checking it, but AutoIt cannot do this.

Also, what exactly do you want this function to do? Just list files into an array and return it? I think you're needlessly over complicating it by trying to force a PHP script into AutoIt. Besides some similar syntax, the two languages are totally different.

What is your $path variable supposed to be?

Cus I'm just seeing a problem with the line $dirStack = _ArrayCreate($path).

The _ArrayCreate function works with multiple variable input, ie:

_ArrayCreate('one', 'two', 'three', 'four', 'etc')

Which gives you:

Array (
   [0] = 'one'
   [1] = 'two'
   [2] = 'three'
   [3] = 'four'
   [4] = 'etc'
)

So this: $dirStack = _ArrayCreate($path)

Gives you this:

Array (
   [0] = $path
)
Edited by Saunders

Share this post


Link to post
Share on other sites
mschol

Use Smoke_N's _FileListToArrayEx http://www.autoitscript.com/forum/index.ph...c=33930&hl=

Don't reinvent the wheel

as i said, i'd like to have a native autoit way to do this...

i assume that *should* be possible because if you , for example, cant run cmd.exe due to restrictions, you should be able to still run the script..

i dont like to be dependent on OS features

Share this post


Link to post
Share on other sites
therks

I reiterate: What exactly do you want this function to do? I'm sure it can't be that hard to figure out, but your existing code confuses me.

Share this post


Link to post
Share on other sites
mschol

I reiterate: What exactly do you want this function to do? I'm sure it can't be that hard to figure out, but your existing code confuses me.

list al files in a given directory, also give back all the files in the subdirectory's

it has a couple of additional things:

1) look for certain extensions.

2) exclude certain directory's (for example , the recycle bin and windows folder)

and give back an array with only the file list (i do not need the amount of items in the array)

i have the php script, which works perfect but i want a more portable way to do this :) (and i thought autoit would be the solution)

but for now i will use the _FilelisttoArrayEx() function, with a couple of own additional checks in order to achieve what i want...

and i still think there is a way to do all of this function (_filelisttoArray()) in a native autoit way.. (so that you dont have to rely on cmd)

because when grouppolicy deny running cmd, the entire script will hang.

Share this post


Link to post
Share on other sites
therks

and i still think there is a way to do all of this function (_filelisttoArray()) in a native autoit way

Oh there definitely is (sorry I've taken so long to reply), I wrote up a function myself to do it for a program I made.

What do you want the results to look like?

You just want file names?

[0] = file.txt

[1] = fun.bat

[2] = hello.wld

etc

Or full path+file?

[0] = c:\file.txt

[1] = c:\windows\fun.bat

etc

Considering you want to check subdirs I'm guessing fullpath+filename.

Share this post


Link to post
Share on other sites
therks

Okay, give this a try.

Func _FileList($sFilePath, $sFileFilter = '', $sFolderExclude = '')
    Local $iFileCounter, $aFileList[10]
    Local $iFolderCounter, $aFolderList[10]
    Local $aSubfolderList[1]
    Local $hFind, $sFindFile
    
    If StringRight($sFilePath, 1) <> '\' Then $sFilePath &= '\'
    
    ConsoleWrite('Indexing ' & $sFilePath & @CRLF)
    $hFind = FileFindFirstFile($sFilePath & '*')
    While 1
        Local $sFindFile = FileFindNextFile($hFind)
        If @error Then ExitLoop
        If StringInStr(FileGetAttrib($sFilePath & $sFindFile), 'd') Then
            If StringRegExp($sFilePath & $sFindFile & '\', $sFolderExclude) Then
                ContinueLoop
            EndIf
            $aFolderList[$iFolderCounter] = $sFilePath & $sFindFile & '\'
            $iFolderCounter += 1
            If $iFolderCounter >= UBound($aFolderList) Then
                ReDim $aFolderList[$iFolderCounter + 10]
            EndIf
        Else
            If StringRegExp($sFindFile, $sFileFilter) Then
                $aFileList[$iFileCounter] = $sFilePath & $sFindFile
                $iFileCounter += 1
                If $iFileCounter >= UBound($aFileList) Then
                    ReDim $aFileList[$iFileCounter + 10]
                EndIf
            EndIf
        EndIf
    WEnd
    FileClose($hFind)

    For $iFolder = 0 To $iFolderCounter - 1
        $aSubfolderList = _FileList($aFolderList[$iFolder], $sFileFilter, $sFolderExclude)
        If IsArray($aSubfolderList) Then
            For $iSubfolderFile = 0 To UBound($aSubfolderList) - 1
                $aFileList[$iFileCounter] = $aSubfolderList[$iSubfolderFile]
                $iFileCounter += 1
                If $iFileCounter >= UBound($aFileList) Then ReDim $aFileList[$iFileCounter + 10]
            Next
        EndIf
    Next
    
    If Not $iFileCounter Then
        Return 0
    Else
        ReDim $aFileList[$iFileCounter]
        Return $aFileList
    EndIf
EndFuncoÝ÷ ÙKí7éªëk

Does that do what you need it to do?

Share this post


Link to post
Share on other sites
SmOke_N

You could just look in randallc's signature and try out his FileListToArrayNew ... I believe he stayed away from the cmd option.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×