Khyzha

Clear blank spaces in file and check if starts with "7"

7 posts in this topic

#1 ·  Posted

Hello people!

I read about AutoIT while searching for a way to do the following task, but i don't quite know how to do it. Can you help me? The little program i'm making reads a .txt file that consists of lines with just numbers, but there can't be none blank spaces in between the numbers nor at the start or end of it. Also i can't have blank lines. Finally (and this is just a plus if you can help me i'm greatful but it's not crucial) i would like to check if the row of numbers start with "7", and if it doesn't the line would be marked somehow, maybe by inserint some "------" after the number. So far i've got this:

#include <File.au3>

$file = FileOpenDialog ("Choose file",@DesktopDir & "\","Text files (*.txt)")
$text = FileRead($file)
$text = StringRegExpReplace(StringRegExpReplace($sText, "(\v)+", @CRLF), "(^\v*)|(\v*\Z)", "")
$2file = FileOpen("Count.txt", 2)
$1file = FileOpen("Count.txt", 1)
FileWrite($2file, $text)
$i = 1
While $i <= _FileCountLines($1file)
    $line = FileReadLine($1file,$i)
    _FileWriteToLine($2file,$i,StringStripWS($line,8))
    $i++
WEnd
FileClose($2file)
FileClose($1file)

Here is an example of before and after of how i need it to be:

Wrong one:

79  09386007913
  7909386111337
7909386222194  


7 909 29 9990302
790929993 7390  

 7890182354540


7909173571078
7909299152663
7909386106395
7909386183839

Right one:

7909386007913
7909386111337
7909386222194
7909299990302
7909299937390
7890182354540
7909173571078
7909299152663
7909386106395
7909386183839

 

Share this post


Link to post
Share on other sites



#2 ·  Posted

Try something like:

#include <Array.au3>

Local $sFileName = @ScriptDir & "\Filename.txt"
Local $aFileName = FileReadToArray($sFileName)
_ArrayDisplay($aFileName)
For $i = UBound($aFileName) - 1 To 0 Step - 1
    If StringStripWS($aFileName[$i], 8) = "" Then
        _ArrayDelete($aFileName, $i)
        ContinueLoop
    EndIf
    $aFileName[$i] = StringStripWS($aFileName[$i], 8)
    If StringLeft($aFileName[$i], 1) <> "7" Then $aFileName[$i] = "----" & $aFileName[$i]
Next
_ArrayDisplay($aFileName)

 

1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted

Another way:

$s = "79  09386007913" & @CRLF & _
"  7909386111337" & @CRLF & _
"7909386222194  " & @CRLF & _
"" & @CRLF & _
"" & @CRLF & _
"7 909 29 9990302" & @CRLF & _
"790929993 7390  " & @CRLF & _
"" & @CRLF & _
" 7890182354540" & @CRLF & _
"" & @CRLF & _
"" & @CRLF & _
"7909173571078" & @CRLF & _
"7909299152663" & @CRLF & _
"7909386106395" & @CRLF & _
"7909386183839"

ConsoleWrite(StringRegExpReplace(StringRegExpReplace($s,"\h",""),"\v+",@CRLF) & @CRLF)

 

1 person likes this

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

#4 ·  Posted

3 hours ago, Subz said:

Try something like:

#include <Array.au3>

Local $sFileName = @ScriptDir & "\Filename.txt"
Local $aFileName = FileReadToArray($sFileName)
_ArrayDisplay($aFileName)
For $i = UBound($aFileName) - 1 To 0 Step - 1
    If StringStripWS($aFileName[$i], 8) = "" Then
        _ArrayDelete($aFileName, $i)
        ContinueLoop
    EndIf
    $aFileName[$i] = StringStripWS($aFileName[$i], 8)
    If StringLeft($aFileName[$i], 1) <> "7" Then $aFileName[$i] = "----" & $aFileName[$i]
Next
_ArrayDisplay($aFileName)

 

I don't quite understand the array system but it seems like it's the best option, but unfortunaly with you code i can't run the script


Line 7  (File "C:\Users\mathe\Desktop\Teste.au3"):

    If StringStripWS($aFileName[$i], 8) = "" Then
^ ERROR
Error: Unable to parse line.
 
Any ideas?
 
3 hours ago, jdelaney said:

Another way:

$s = "79  09386007913" & @CRLF & _
"  7909386111337" & @CRLF & _
"7909386222194  " & @CRLF & _
"" & @CRLF & _
"" & @CRLF & _
"7 909 29 9990302" & @CRLF & _
"790929993 7390  " & @CRLF & _
"" & @CRLF & _
" 7890182354540" & @CRLF & _
"" & @CRLF & _
"" & @CRLF & _
"7909173571078" & @CRLF & _
"7909299152663" & @CRLF & _
"7909386106395" & @CRLF & _
"7909386183839"

ConsoleWrite(StringRegExpReplace(StringRegExpReplace($s,"\h",""),"\v+",@CRLF) & @CRLF)

 

Don't think that's gonna work because the numbers are different everytime.

 

Thanks for replying guys, i'm really having a hard time with this. Nothing seems to work completely. I can get half of the job done, but i'm always missing something.

Share this post


Link to post
Share on other sites

#5 ·  Posted

Can you post your Filename.txt file, I used your example above and it worked fine.  Can you also specify which version of AutoIt you have?

1 person likes this

Share this post


Link to post
Share on other sites

#6 ·  Posted

Hello again!

I actually have done it by mixing some of the code i had with the ones posted here! The script that worked for me is:

#include <Array.au3>

Local $rFile = FileOpenDialog ("Selecione o arquivo de contagem",@DesktopDir & "\","Text files (*.txt)")
Local $aFile = FileReadToArray($rFile)
Local $wFile = FileOpen(@ScriptDir & "\CONTAGEM_RESULTADO.txt",2)
ProgressOn ("Por favor aguarde", "Por favor aguarde")
For $i = 0 To UBound($aFile) -1 Step +1
    If $aFile[$i] <> "" Then
        FileWriteLine($wFile,StringStripWS($aFile[$i],8))
        ProgressSet(($i/(UBound($aFile)-1))*100)
    EndIf
Next
$waFile = FileReadToArray(@ScriptDir & "\CONTAGEM_RESULTADO.txt")
Local $tFile = FileOpen(@ScriptDir & "\CONTAGEM_RESULTADO.txt",2)
For $i = 0 to UBound($waFile) -1 Step +1
    If StringLeft($waFile[$i],1) <> "7" Then
        FileWriteLine($tFile,StringStripWS($waFile[$i],8) & "-Código de barras não inicia com 7")
    ElseIf StringLen($waFile[$i]) <> 13 Then
        If StringLen($waFile[$i]) > 13 Then
            FileWriteLine($tFile,StringStripWS($waFile[$i],8) & "-Código de barras com mais de 13 dígitos")
        Else
            FileWriteLine($tFile,StringStripWS($waFile[$i],8) & "-Código de barras com menos de 13 dígitos")
        EndIf
    Else
        FileWriteLine($tFile,StringStripWS($waFile[$i],8))
    EndIf
Next
ProgressOff()

Don't mind the language, it's Portuguese. I'd like to thank everyone that helped me!

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

@Khyzha not sure if this will help you more:

I did compiled it this way. Kind of something funny but it worked.:D

#include <File.au3>

Global $ifile = @ScriptDir & "\Filename.txt"
$countline = _FileCountLines($ifile)
For $i = 1 to $countline
    $line = FileReadLine($ifile, $i)
    $sString = StringReplace($line, " ", "")
    _FileWriteToLine($ifile,$i,$sString,True)
   For $a = 1 to $countline
    $aline = FileReadLine($ifile, $a)
    $sString = StringReplace($aline, " ", "")
    _FileWriteToLine($ifile,$a,$sString,True)
    $aString = StringLeft($aline, 1)
    For $o = 1 to $aString
      If $aString <> "7" Then
         $insert = FileReadLine($ifile, $aString)
        _FileWriteToLine($ifile, $aString, $insert&"-",True)
     EndIf
    Next
   Next
Next

From text file:

79  09386007913
  7909386111337
7909386222194  


7 909 29 9990302
790929993 7390  

 7890182354540


909173571078 ; Not 7 in start
7909299152663
909386106395 ; Not 7 in start
7909386183839

Output is this:

7909386007913
7909386111337
7909386222194
7909299990302
7909299937390
7890182354540
909173571078---- ; inserted with "----"
7909299152663
909386106395---- ; inserted with "----"
7909386183839

Anyway, since you have found your path then good luck!^_^

 

KS15

 

Edited by KickStarter15
Edited with correction:
1 person likes this

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