Jump to content

Need to convert entire Directory contents into one string


Recommended Posts

Hey, so heres what i'm trying to do here:

I have a directory; say C:\dir

I want to be able to read all the files in the directory, while retaining the ENTIRE path

#Include <File.au3>
#Include <Array.au3>
$FileList=_FileListToArray("c:\dir", "*.*" , 1)
If (Not IsArray($FileList)) and (@Error=1) Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf

_ArrayDisplay($FileList,"$FileList")

This code just returns the filename, not the c:\dir\ path so i'd like that included

Now, i would like to be able to transform the entire directory listing into one string, with a ";" seperator between every file

So if i have:

file1.jpg

file2.jpg

file3.jpg

in the directory, i want the string to read:

c:\dir\file1.jpg;c:\dir\file2.jpg;c:\dir\file3.jpg

I assume i would have to use some concatenate feature, but im not sure how to do that

Link to comment
Share on other sites

Try this and see if it's what you're looking for.

#include <recfilelisttoarray.au3>
#include <Array.au3>
Global $Path = "C:\Windows"
Global $aFileList = _RecFileListToArray($Path, "*", 1, 0,  0, 2)
_ArrayDisplay($aFileList)
Global $Text = ""
For $I = 1 To $aFileList[0]
    $Text &= $aFileList[$I] & ";"
Next
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Text = ' & $Text & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console

It will display the variable $Text in the console window of scite if rum from there.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Hey brew i got it to work with _arraytostring function. However, it only returns the filenames, i want the full directory to precede each file

I get : file1.jpg;file2.jpg;file3.jpg

I want : c:\dir\file1.jpg;c:\dir\file2.jpg;c:\dir\file3.jpg

Link to comment
Share on other sites

I forgot, you'll need to add the UDF which makes this easier to get what you need.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

....

I assume i would have to use some concatenate feature, but im not sure how to do that

The concatenate feature is using "&" between strings.

Try this.

#include <File.au3>
#include <Array.au3>

Local $sDir = "C:\Documents and Settings\All Users\Desktop"
Local $FileList = _FileListToArray($sDir, "*.*", 1)
If @error Then   ; or  IsArray($FileList) = 0 then ;
    MsgBox(0, "", "No Files\Folders Found.")
    Exit
EndIf

_ArrayDisplay($FileList, "$FileList")

Local $sFileListString = ""
For $i = 1 To UBound($FileList) - 1
    $sFileListString &= $sDir & "\" & $FileList[$i] & ";" ; <-- Concatenation
Next
$sFileListString = StringTrimRight($sFileListString, 1) ; Remove trailing semicolon.

ConsoleWrite($sFileListString & @CRLF)
MsgBox(0, "File List String", $sFileListString)
Link to comment
Share on other sites

Or, if you only need the first level:

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

$dir = "C:\dir"

$FileList=_FileListToArray($dir, "*.*" , 1)
If (Not IsArray($FileList)) and (@Error=1) Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
Else
    $FileListString = _ArrayToString($FileList, ", " & $dir & "\", 1)
EndIf

ConsoleWrite($FileListString & @CRLF)
Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...