Jump to content

Sort Excisting .txt File By Name


Recommended Posts

Is it possible to sort a excisting txt file name by name.

For an example:

example.txt

Abcd 1

Dcba 2

Bacd 3

Cabd 4

Cbda 5

Dbac 6

Adcb 7

I think it must be something with filereadline, but i don't know in what kind of context.

Could someone help pls?

Link to comment
Share on other sites

  • Moderators

Is it possible to sort a excisting txt file name by name.

For an example:

example.txt

Abcd 1

Dcba 2

Bacd 3

Cabd 4

Cbda 5

Dbac 6

Adcb 7

I think it must be something with filereadline, but i don't know in what kind of context.

Could someone help pls?

2 Functions, both in the help file: _FileListToArray()/_ArraySort()
#Include <File.au3>
#Include <Array.au3>
$FileList=_FileListToArray(@DesktopDir)
If (Not IsArray($FileList)) and (@Error=1) Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf
_ArrayDisplay($FileList,"$FileList")
_ArraySort($FileList)
_ArrayDisplay($FileList, 'List Descending')

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.

Link to comment
Share on other sites

2 Functions, both in the help file: _FileListToArray()/_ArraySort()

#Include <File.au3>
#Include <Array.au3>
$FileList=_FileListToArray(@DesktopDir)
If (Not IsArray($FileList)) and (@Error=1) Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf
_ArrayDisplay($FileList,"$FileList")
_ArraySort($FileList)
_ArrayDisplay($FileList, 'List Descending')
Ah man and i searched the help file :)

Thanks, I hope i get it done :mellow:

Link to comment
Share on other sites

How can i skip the " " record?

This is my code:

#include <Array.au3>
#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("C:\company\Test.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
For $x = 1 to $aRecords[0]
    _ArraySort($aRecords,0,1)
    Msgbox(0,'Record:' & $x, $aRecords[$x])
Next

This is my text file:

a
c
e
b
d
f
i
h
k
j
g

How can i skip the last entry wich is blank. I tried al possible ways, but i am missing something.

Edited by Iznogoud
Link to comment
Share on other sites

  • Moderators

How can i skip the " " record?

This is my code:

#include <Array.au3>
#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("C:\company\Test.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
For $x = 1 to $aRecords[0]
    _ArraySort($aRecords,0,1)
    Msgbox(0,'Record:' & $x, $aRecords[$x])
Next

This is my text file:

a
c
e
b
d
f
i
h
k
j
g

How can i skip the last entry wich is blank. I tried al possible ways, but i am missing something.

You could always recreate the array before the array sort or after it to get rid of the empty strings, or modify ArraySort() itself.

Edit:

Forgot the example of recreate:

#include <array.au3>
Local $Example[13] = ['a','c','e','b','d','f','i','h','k','j','g', ' ', ' ']

_ArraySort($Example)
Local $NewArray = ''

For $i = 1 To UBound($Example) - 1
    If StringStripWS($Example[$i], 7) <> '' Then $NewArray &= $Example[$i] & Chr(01)
Next
    
$NewArray = StringSplit(StringTrimRight($NewArray, 1), Chr(01))
_ArrayDisplay($NewArray, 'Stripped Empty Elements')
Edited by SmOke_N

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.

Link to comment
Share on other sites

_ArraySort($aRecords,0,1); this should be first

For $x = 1 to $aRecords[0] -1; will stop the loop 1 before the end
 
    Msgbox(0,'Record:' & $x, $aRecords[$x])
Next

8)

If you do it like this you still see the blank field first, but the last character wich should be K you don't.

So the array must start at the 2nd part and not the first wich is blank. I am trying to accomplish this with the example in the help file, but i still don't got it to work :)

You could always recreate the array before the array sort or after it to get rid of the empty strings, or modify ArraySort() itself.

Edit:

Forgot the example of recreate:

#include <array.au3>
Local $Example[13] = ['a','c','e','b','d','f','i','h','k','j','g', ' ', ' ']

_ArraySort($Example)
Local $NewArray = ''

For $i = 1 To UBound($Example) - 1
    If StringStripWS($Example[$i], 7) <> '' Then $NewArray &= $Example[$i] & Chr(01)
Next
    
$NewArray = StringSplit(StringTrimRight($NewArray, 1), Chr(01))
_ArrayDisplay($NewArray, 'Stripped Empty Elements')
Doesn't your code need DIM $Example?

It is not yet clear what this does, but i get an error if i try to run you code to see what happens.

Edited by Iznogoud
Link to comment
Share on other sites

  • Moderators

If you do it like this you still see the blank field first, but the last character wich should be K you don't.

So the array must start at the 2nd part and not the first wich is blank. I am trying to accomplish this with the example in the help file, but i still don't got it to work :)

Doesn't your code need DIM $Example?

It is not yet clear what this does, but i get an error if i try to run you code to see what happens.

You don't need Dim, but you do need Beta.

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.

Link to comment
Share on other sites

Ok, i am trying your code and i adjusted it so it fits in mine.

#include <Array.au3>
#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("C:\company\Test.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
_ArraySort($aRecords)
Local $NewArray = ''
For $i = 1 To UBound($aRecords) - 1
    If StringStripWS($aRecords[$i], 7) <> '' Then $NewArray &= $aRecords[$i] & Chr(01)
Next

$NewArray = StringSplit(StringTrimRight($NewArray, 1), Chr(01))
_ArrayDisplay($NewArray, 'Stripped Empty Elements')

I am getting this result:

Posted Image

Still the last one doesn't belong there or am i wrong?

Edited by Iznogoud
Link to comment
Share on other sites

Ok, i am trying your code and i adjusted it so it fits in mine.

#include <Array.au3>
#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("C:\Mareco\Test.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
_ArraySort($aRecords)
Local $NewArray = ''
For $i = 1 To UBound($aRecords) - 1
    If StringStripWS($aRecords[$i], 7) <> '' Then $NewArray &= $aRecords[$i] & Chr(01)
Next

$NewArray = StringSplit(StringTrimRight($NewArray, 1), Chr(01))
_ArrayDisplay($NewArray, 'Stripped Empty Elements')

I am getting this result:

Posted Image

Still the last one doesn't belong there or am i wrong?

; Change this
_ArraySort($aRecords)
; To this
_ArraySort($aRecords, 0, 1)

:)

Link to comment
Share on other sites

Oke, thanks all the good help i adjusted the code to this:

#include <Array.au3>
#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("C:\company\Test.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
_ArraySort($aRecords, 0, 1)
Local $NewArray = ''
For $i = 1 To UBound($aRecords) - 1
    If StringStripWS($aRecords[$i], 7) <> '' Then $NewArray &= $aRecords[$i] & Chr(01)
Next

$NewArray = StringSplit(StringTrimRight($NewArray, 1), Chr(01))
For $x = 1 to $NewArray[0]
    Msgbox(0,'Record:' & $x, $NewArray[$x])
Next

And yes it does work :)

Now i finally can let AutoIT do the work instead of me sorting it by hand or excel or something wich takes to much time.

Only if i add a new customer its not yet vissible in the GUI. Is there a command wich refresh the GUI or is there a way that the customer wich has been added automatically is selected?

Edited by Iznogoud
Link to comment
Share on other sites

Ok i have adjusted the code some more, so i can input it in one of my bigger scripts. But i got one more question.

This is my code:

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

$tempfile = FileOpen("C:\company\tempfile.txt", 1)
Dim $aRecords
If Not _FileReadToArray("C:\company\Test.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
_ArraySort($aRecords)
Local $NewArray = ''
For $i = 1 To UBound($aRecords) - 1
    If StringStripWS($aRecords[$i], 7) <> '' Then $NewArray &= $aRecords[$i] & Chr(01)
Next

$NewArray = StringSplit(StringTrimRight($NewArray, 1), Chr(01))
For $x = 1 to $NewArray[0]
    FileWriteLine($tempfile,  $NewArray[$x] & @CRLF)
Next

FileMove("C:\company\Tempfile.txt", "C:\company\Test.txt")

Why doesn't FileMove, wich i use to rename the file, work?

If i run this code it won't rename it to Test.txt, but if run Filemove alone after this script is done, it does work.

I tried fileclose etc. in different places, but i can't get it to work. Do you guys know what i am doing wrong?

Link to comment
Share on other sites

Ok i have adjusted the code some more, so i can input it in one of my bigger scripts. But i got one more question.

This is my code:

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

$tempfile = FileOpen("C:\company\tempfile.txt", 1)
Dim $aRecords
If Not _FileReadToArray("C:\company\Test.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
_ArraySort($aRecords)
Local $NewArray = ''
For $i = 1 To UBound($aRecords) - 1
    If StringStripWS($aRecords[$i], 7) <> '' Then $NewArray &= $aRecords[$i] & Chr(01)
Next

$NewArray = StringSplit(StringTrimRight($NewArray, 1), Chr(01))
For $x = 1 to $NewArray[0]
    FileWriteLine($tempfile,  $NewArray[$x] & @CRLF)
Next

FileMove("C:\company\Tempfile.txt", "C:\company\Test.txt")

Why doesn't FileMove, wich i use to rename the file, work?

If i run this code it won't rename it to Test.txt, but if run Filemove alone after this script is done, it does work.

I tried fileclose etc. in different places, but i can't get it to work. Do you guys know what i am doing wrong?

Might want to close it 1st

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

  • Moderators

Might want to close it 1st

Might want to make sure that if it doesn't exists it's created too, and if it does exist that it overwrites the current one:
FileMove("C:\company\Tempfile.txt", "C:\company\Test.txt", 9)
Note the '9'.

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.

Link to comment
Share on other sites

Might want to make sure that if it doesn't exists it's created too, and if it does exist that it overwrites the current one:

FileMove("C:\company\Tempfile.txt", "C:\company\Test.txt", 9)
Note the '9'.
I tried the number 1 wich said in the helpfile it will overwrite.

"1 = overwrite existing files"

But one way or the other, it doesn't work.

And i tried closing the files with FileClose before FileMove, but that doesn't work either.

Edited by Iznogoud
Link to comment
Share on other sites

  • Moderators

I tried the number 1 wich said in the helpfile it will overwrite.

"1 = overwrite existing files"

But one way or the other, it doesn't work.

Delete any existing 'Test.txt' files that are there... and try this where filemove() is:
If FileExists("C:\company\Tempfile.txt") Then
    Local $TimerTest = 0
    Local $Timer = TimerInit()
    Do
        FileMove("C:\company\Tempfile.txt", "C:\company\Test.txt", 8)
        If TimerDiff($Timer) / 1000 >= 10 Then $TimerTest = 1; Keep trying to move and rename the file for 10 seconds
        Sleep(10)
    Until FileExists("C:\company\Test.txt") Or $TimerTest = 1
    If $TimerTest == 1 Then
        MsgBox(0, 'Timed Out', 'Timer Timed Out, File Not Moved')
    Else
        MsgBox(0, 'Created', 'File was moved and renamed')
        Run('notepad.exe C:\company\Test.txt')
    EndIf
Else
    MsgBox(0, 'Error', 'The original file you are trying to move does not exists')
EndIf
Edit:

Added a Run() option there if the file was created, will open the .txt file up in Notepad.

Edited by SmOke_N

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.

Link to comment
Share on other sites

I get this error: The original file you are trying to move does not exists

If i look at the directory the tempfile is created but it is 0 Kb untill the script is finished the kb will pop to 31 kb for an example. So i think there is starting the problem.

You said i must delete the Test file but there is the information wich i want to sort the data into.

I dont know if this can be in an other way like.

Instead writing it to a tempfile to write it to the original file wich was read into a array?

Then you don't need to rename/move/edit the file in any way.

-EDIT-

I just tested it in the way i said before with this code:

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

Dim $aRecords
If Not _FileReadToArray("C:\company\Test.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
_ArraySort($aRecords)
Local $NewArray = ''
For $i = 1 To UBound($aRecords) - 1
    If StringStripWS($aRecords[$i], 7) <> '' Then $NewArray &= $aRecords[$i] & Chr(01)
Next

FileDelete("C:\company\Test.txt")
$testfile = FileOpen("C:\company\Test.txt", 1)

$NewArray = StringSplit(StringTrimRight($NewArray, 1), Chr(01))
For $x = 1 to $NewArray[0]
    FileWriteLine($testfile,  $NewArray[$x] & @CRLF)
Next

FileClose($testfile)

So yes it does work, as far as i understand it. The information containing in the Test.txt is read into an array (like in memory or something) then it deletes the Test.txt file and recreate it with the new sorted information from the array.

Is this a good way or do i can get some problems with this kind of a solution?

Edited by Iznogoud
Link to comment
Share on other sites

  • Moderators

It looks to me that you had the names of the files backwards...

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.

Link to comment
Share on other sites

It looks to me that you had the names of the files backwards...

What do you mean? Files backwards? That i was confused with the filenames?

I tried the code standalone and it does work, but if i put it into my main script it doesn't.

It looks like the file isn't getting closed somehow. Untill the script is finished you see the size change.

I can compile 2 scripts and call one script from the other, but i think there must be an other way to get one script.

Is there a way so i can see wich files are open and wich files need to be closed when i run the script?

Link to comment
Share on other sites

  • Moderators

What do you mean? Files backwards? That i was confused with the filenames?

I tried the code standalone and it does work, but if i put it into my main script it doesn't.

It looks like the file isn't getting closed somehow. Untill the script is finished you see the size change.

I can compile 2 scripts and call one script from the other, but i think there must be an other way to get one script.

Is there a way so i can see wich files are open and wich files need to be closed when i run the script?

Well a good practice is to close the file when your function is done with it... wouldn't you agree... then once it is closed, then you can proceed with whatever it is that your going to do.

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.

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...