Jump to content

StringRegExp (getting directory name from txt and sort all files)


Recommended Posts

Hi all!

I have txt file, something like this.

2010_08_20100818-141838-1.eml INBOX

2010_09_20100930-150737-1.eml SENT

2010_09_20100927-180146-1.eml INBOX

2010_10_20101004-154712-1.eml AAA

2010_10_20101026-113848-1.eml BBB

I need to sort all this files by folders (%filename%.eml %foldername%).

Please, help with it.

Link to comment
Share on other sites

Try this.

#include <file.au3>
#include <Array.au3>

Dim $aRecords

; Read text file to array
If Not _FileReadToArray("FileFolderList.txt", $aRecords) Then
    MsgBox(4096, "Error", " Error reading log to Array  error:" & @error)
    Exit
EndIf
_ArrayDisplay($aRecords)

; Create a 2D array with the first column custom made to sort on.
Local $aTemp[UBound($aRecords)][2]
For $x = 1 To $aRecords[0]
    $aTemp[$x][1] = $aRecords[$x]
    
    ; Each line re-order to Folder, File name, and File extension.  Should the sort order be 
    ;  Folder, File extension, then File name, use  "$3$2$1" as the "replace" parameter, in next line.
    $aTemp[$x][0] = StringRegExpReplace($aRecords[$x], "(.+)(\.?.*)\h+(.+)", "$3$1$2")
Next

_ArraySort($aTemp, 0, 1, 0, 0) ; Sort on first column.

; Write new sorted array to file.
Local $aNewOrder[UBound($aRecords)]
For $x = 1 To UBound($aNewOrder) - 1
    $aNewOrder[$x] = $aTemp[$x][1]
Next
_FileWriteFromArray("$sNewTextFile.txt", $aNewOrder, 1, 0)

ShellExecute("$sNewTextFile.txt")
Link to comment
Share on other sites

Thanks! But it doesn't work how I need. Maybe I didn't write correctly the thing I need.

I have text file it has many lines.

%filename1% %folder name%

%filename2% %folder name%

%filename3% %folder name%

#include <file.au3>
#include <Array.au3>

Dim $aRecords

; Read text file to array
If Not _FileReadToArray("name.txt", $aRecords) Then
    MsgBox(4096, "Error", " Error reading log to Array  error:" & @error)
    Exit
EndIf

; Create a 2D array with the first column custom made to sort on.
Local $aTemp[UBound($aRecords)][2]
For $x = 1 To $aRecords[0]
    $aTemp[$x][1] = $aRecords[$x]

After that script should sort files to folders according this txt file.

Line from my txt:

2011_06_20110608-095006-calendar-notification.eml INBOX

2011_06_20110607-080249-mail-noreply.eml Sent

First file has to be moved to INBOX folder, second to Sent folder.

Link to comment
Share on other sites

Silly me thought you wanted to sort the text within a file.

So if you want to move files to folders according the txt file, then you should probably use the FileMove() function. Its use with an example is in the AutoIt help file.

Link to comment
Share on other sites

Line from my txt:

2011_06_20110608-095006-calendar-notification.eml INBOX

2011_06_20110607-080249-mail-noreply.eml Sent

First file has to be moved to INBOX folder, second to Sent folder.

If I understood it, this should do the job:

#include <File.au3>

Global $acontent
$file = @ScriptDir & "\your.txt"
_FileReadToArray($file, $acontent)

For $i = 1 To $acontent[0]
    Local $tempfile = StringRegExp($acontent[$i], "^(.+)\s.+", 1)
    Local $tempdir = StringRegExp($acontent[$i], "^.+\s(.+)", 1)
    FileMove($tempfile[0], $tempdir[0] & "\", 9)
Next
Link to comment
Share on other sites

Probably yes! But

MsgBox(1, "1", $tempdir)

MsgBox(1, "1", $tempfile)

show nothing.

$tempdir and $tempfile are arrays so:

MsgBox(1, "1", $tempdir[0])
MsgBox(1, "1", $tempfile[0])

Edit: I see you edited previous message. Glad to see it is working fine.

Edited by sahsanu
Link to comment
Share on other sites

Hi sahsanu!

One little problem yet.

I have also masks such:

HD\ 2\ ME HD

How I should change $tempdir = StringRegExp($acontent[$i] to exclude special symbols because it is impossible to create such directories.

Thanks in advance.

Link to comment
Share on other sites

I have also masks such:

HD\ 2\ ME HD

How I should change $tempdir = StringRegExp($acontent[$i] to exclude special symbols because it is impossible to create such directories.

Add this line just after "For $i = 1 To $acontent[0]".

$acontent[$i] = StringRegExpReplace($acontent[$i],'[\<|\>|\:|\"|\/|\\|\||\?|\*]',"")

Edit: I wrote before but I mean after :huh2:

Edited by sahsanu
Link to comment
Share on other sites

If your directories have spaces then you will have some issues creating the dirs. Try below modified script:

#include <File.au3>

Global $acontent
$file = @ScriptDir & "\your.txt"
_FileReadToArray($file, $acontent)

For $i = 1 To $acontent[0]
    $acontent[$i] = StringRegExpReplace($acontent[$i],'[\<|\>|\:|\"|\/|\\|\||\?|\*]',"")
    Local $tempfile = StringRegExp($acontent[$i], "^(.+?)\s.+", 1)
    Local $tempdir = StringRegExp($acontent[$i], "^.+?\s(.+)", 1)
    FileMove($tempfile[0], $tempdir[0] & "\", 9)
Next
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...