Sign in to follow this  
Followers 0
kor

How to add full path to _FileListToArray

12 posts in this topic

#Include <File.au3>
#Include <Array.au3>

$sPath = @ScriptDir
$FileList=_FileListToArray($sPath, '*', 2)

$company = InputBox("test", "Company Name?", "", " M")
$destination = InputBox("test1", "Destination Drive Letter?", "", " M1")

If @Error=1 Then
    MsgBox (0,"Error","No Files\Folders Found.")
    Exit
EndIf

$iElement = "0"
_ArrayDelete($FileList, $iElement)

$File = $company & ".bat"
_FileWriteFromArray($File, $FileList)

; Display results
Run("notepad.exe " & $File)

I've gotten this far, but I want to add the full path to the directories in the file. Also, if I could somehow add another string to the beginning of each line, and at the end of each line that would be awesome too, but I haven't gotten that far yet :(

I know I need to do some kind of file read loop kinda thing but I'm not really sure how to move forward.

Share this post


Link to post
Share on other sites



add this

$lineprefix = ""
$linesufix = ""
for $i = 0 to UBound($FileList)-1
    $FileList($i) = $lineprefix & $sPath & "\" & $FileList($i) & $linesufix
Next

after the _arraydelete(). Set pre- and sufix to whatever you need...

Share this post


Link to post
Share on other sites

If it's just folders that you are after then try this function. It returns the full path with a trailing backslash and it's (optionally) recursive.

;===============================================================================
; Function Name:    _Fldr_ListToArray()
; Description:      Recursivly find the folders in a given path (returns full path for each)
; Syntax:           _Fldr_ListToArray($s_Path[, $b_Recurse[, $b_Hidden[, $b_System]]])
; Parameter(s):     $s_Path - The base folder
;                   $s_Ignore - Pipe (|) separated list of folders to ignore.  The * wildcard is accepted.
;                   $b_Recurse - If True (default) search is recursive
;                   $b_Hidden - If False (default) "Hidden" folders are NOT included
;                   $b_System If True (default) "System" folders are included.
; Requirements:
; Return Value(s):  Success - A 0 based array of the folders with paths
;                   Failure - Sets @Error to 1
; Author(s):        George (GEOSoft) Gedye
; Notes:
;Example(s):
#cs
    ;; folders in C:\, non-recursive Excluding the Windows folder and including Hidden and System folders
    $aArray = _Fldr_ListToArray("c:", "Windows", True)
    _ArrayDisplay($aArray)
#ce
;===============================================================================

Func _Fldr_ListToArray($s_Path, $s_Ignore = "", $b_Recurse = True, $b_Hidden = False, $b_System = True)
    If StringRight($s_Path, 1) <> "\" Then $s_Path &= "\"
    $s_Path = '"' & $s_Path & '"'
    Local $s_Working = @WorkingDir, $s_Env = ""
    FileChangeDir(@TempDir)
    Local $sFile = "dir.txt"
    If FileExists($sFile) Then FileDelete($sFile)
    If $b_Recurse Then $s_Env &= " /s"
    $s_Env &= " /b /o:n"
    Local $s_Attrib = " /a:d"
    If NOT $b_Hidden Then $s_Attrib &= "-h"
    If NOT $b_System Then $s_Attrib &= "-s"
    $s_Env &= $s_Attrib
    EnvSet("DIRCMD", $s_Env)
    $hPID = Run(@ComSpec & ' /c dir ' & $s_Path  & " > " & $sFile , "", @SW_HIDE, 0x02)
    While ProcessExists($hPID)
        If @error Then ExitLoop
    WEnd
    Local $s_Str = StringStripWS(FileRead($sFile), 2)
    FileDelete($sFile)
    EnvSet("DIRCMD", "")
    FileChangeDir($s_Working)
    If $s_Str Then
        If $s_Ignore Then
            $aIgnore = StringSplit($s_Ignore, "|", 2)
            For $i = 0 To UBound($aIgnore) -1
                $aIgnore[$i] = StringRegExpReplace($aIgnore[$i], "\.*\*+", "\.\*\?")
                $s_Str = StringRegExpReplace($s_Str, "(?i)(?m:^).+\\" & $aIgnore[$i] & "\\.*(?:\z|\v)+", "")
            Next
        EndIf
        $a_Fldrs = StringRegExp($s_Str, "(?i)(?m:^)(.+)(?:\v|$)+", 3)
        If Not @error Then
            For $i = 0 To UBound($a_Fldrs) -1
                $a_Fldrs[$i] &= "\"
            Next
            Return $a_Fldrs
        EndIf
    EndIf
    Return SetError(1)
EndFunc   ;;<===>_Fldr_ListToArray

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

add this

$lineprefix = ""
$linesufix = ""
for $i = 0 to UBound($FileList)-1
    $FileList($i) = $lineprefix & $sPath & "\" & $FileList($i) & $linesufix
Next

after the _arraydelete(). Set pre- and sufix to whatever you need...

>"C:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\filelist.au3"

C:\filelist.au3 (21) : ==> Expected a "=" operator in assignment statement.:

$FileList($i) = $lineprefix & $sPath & "\" & $FileList($i) & $linesufix

$FileList^ ERROR

>Exit code: 1 Time: 2.119

Share this post


Link to post
Share on other sites

Here is one too:(If i understand correct?)

#include <File.au3>
#include <Array.au3>
Dim $avarray

;$company = InputBox("test", "Company Name?", "", " M")
$destination = InputBox("Enter Drive Letter","Enter Drive Letter","")

RunWait("cmd.exe /c " & "dir /B " & $destination & " >"&@TempDir&"\tempfile.dat",@ScriptDir,@SW_HIDE)
_FileReadToArray(@TempDir&"\tempfile.dat",$avarray)
For $x=0 To $avarray[0]
    MsgBox(64,"",FileGetLongName($destination&$avarray[$x]))
    Next

[size="5"] [/size]

Share this post


Link to post
Share on other sites

Kafu, I got the script to run by adding Step at the end of your For To loop

for $i = 0 to UBound($FileList) Step -1

which allows the script to run, but it is not actually adding the paths, prefix or suffix to the lines.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Hi,

it looks like a typo:

$FileList ($i) should be $FileList [$i], shouldn't it, as it adresses an array element?

And the Step - 1 doesn't make any sense as you run from 0 to 50 (e.g) Step -1 ???

;-))

Stefan

Edited by 99ojo

Share this post


Link to post
Share on other sites

#Include <File.au3>
#Include <Array.au3>

$Path = @ScriptDir
$Folders=_FileListToArray($Path, '*', 2)
Dim $avarray

$company = InputBox("", "Company Name?", "", " M")
$destination = InputBox("", "Destination Drive Letter?", "", " M1")
$File = $company & ".bat"

If @Error=1 Then
    MsgBox (0,"Error","No Files\Folders Found.")
    Exit
EndIf

$count = "0"
_ArrayDelete($Folders, $count)
_ArrayDisplay($Folders)

For $i = 0 to UBound($Folders) Step -1
    $Folders[$i] = "test\" & $Folders($i)
Next

_FileWriteFromArray($File, $Folders)

; Display results
Run("notepad.exe " & $File)

I've changed the code a little, and changed the names of the VAR's to make them less confusing to me. Everything is working except the actual modification of the array lines. It spits out the list of folders, but it isn't adding the "test" in front of them. (or at all)

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Hi,

you haven't read carefully: :(

For $i = 0 to UBound($Folders) Step -1

should be

For $i = 0 to UBound($Folders) - 1

Then it should run as expected.

:)

Stefan

Edited by 99ojo

Share this post


Link to post
Share on other sites

Man, I'm so stupid. I was about to reply and say I've already tried that and it didnt work, but I didn't have a space between ($Folders) -1, mine was ($Folders)-1

thank you so much!

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Final Code

#Include <File.au3>
#Include <Array.au3>

$Path = @ScriptDir
$Folders=_FileListToArray($Path, '*', 2)

$company = InputBox("Robocopy Maker", "Company Name?", "", " M")
$destinationdrive = InputBox("Robocopy Maker", "Destination Drive Letter?", "", " M1")
$File = $company & ".bat"

; Remove drive letter from Path
$workingpath = StringTrimLeft($Path, 1)
; Combine to form new destination directory
$destinationpath = $destinationdrive & $workingpath

; Error checking
If @Error=1 Then
    MsgBox (0,"Error","No Files\Folders Found.")
    Exit
EndIf

; Trim folder count from array
$count = "0"
_ArrayDelete($Folders, $count)
;_ArrayDisplay($Folders)

; Pre and Post commands
$pre = "start robocopy "
$post = " /E /DCOPY:T"
For $i = 0 to UBound($Folders) -1
    $Folders[$i] = $pre & $Path & $Folders[$i] & " " & $destinationpath & $Folders[$i] & $post
Next

_FileWriteFromArray($File, $Folders)

; Display results
Run("notepad.exe " & $File)
Edited by kor

Share this post


Link to post
Share on other sites

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  
Followers 0