Sign in to follow this  
Followers 0
KickStarter15

Need help with _ReplaceStringInFile()

14 posts in this topic

#1 ·  Posted

Hi Experts,

I have this issue with my current function _ReplaceStringInFile() which is not all line were replace with "" (empty).

Here's my code:

$FP = 0
    Local $ForProcLogFile = FileOpen(@ScriptDir & "\ForProcessFiles.log", 0)
    For $FP = 1 To _FileCountLines(@ScriptDir & "\ForProcessFiles.log")
      $retrivF = StringTrimRight(FileReadLine($ForProcLogFile, $FP),4) & ".pdf"
      If FileExists($dFileRead & $retrivF) Then ; $dFileRead is a from FileReadLine()
        FileMove($dFileRead & $retrivF, $bFileRead & $retrivF, 9); $bFileRead is a from FileReadLine()
         If FileExists($bFileRead & $retrivF) Then
            GUICtrlSetData($Label3, "Moving " & $retrivF & " to out folder...")
            Sleep (2000)
;           MsgBox(0,"",FileReadLine($ForProcLogFile, $FP))
            _ReplaceStringInFile(@ScriptDir & "\ForProcessFiles.log", FileReadLine($ForProcLogFile, $FP), "")
;~          StringReplace(@ScriptDir & "\ForProcessFiles.log", FileReadLine($ForProcLogFile, $FP), "") ; not working either..
         EndIf
      EndIf
    Next
    FileClose($ForProcLogFile)

In ForProcessFiles.log I have:

Sample1_New.pdf
Sample1_Old.pdf
Sample2_New.pdf
Sample2_Old.pdf
Sample3_New.pdf
Sample3_Old.pdf

and so on....

I also tried using _DeleteArrayElementWithStringInstr() but it seems that only the Sample*_Old.pdf were removed/replaced in log file but Sample*_New.pdf remains (see below). Please need your advise on proper handling for this case.

_FileReadToArray ( @ScriptDir & '\ForProcessFiles.log', $_Array )
$_Array = _DeleteArrayElementWithStringInstr ( $_Array, FileReadLine($ForProcLogFile, $FP))
_FileWriteFromArray ( @ScriptDir & '\ForProcessFiles.log', $_Array, 1 )

Sample1_New.pdf
Sample2_New.pdf
Sample3_New.pdf

Thanks in advance, Experts.

 

KS15

Share this post


Link to post
Share on other sites



#2 ·  Posted

Hello @KickStarter15,

I'm sorry but i dont understand :D

Could you please try to explain what you are trying to achieve instead of explaining how you are doing it? ;)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

@Neutro,

I'm currently creating a program that will loop thru .log file which is "ForProcessFiles.log" and in that .log file, I have the below list of files to be process, once the program found the file match from my out folder, then it will automatically get that file from out folder and must be cleared from the .log.

Below is my .log file contains:

Sample1_New.pdf
Sample1_Old.pdf
Sample2_New.pdf
Sample2_Old.pdf
Sample3_New.pdf
Sample3_Old.pdf

Here's an example. In out folder, I have this file found "D:\Programs\MyAutoIt\out\Sample1.New.pdf" and "D:\Programs\MyAutoIt\out\Sample1.Old.pdf" and it matches from my .log created. So, the program will get that file from out folder and then remove the filename that matches in my .log file and so on.

I have attached my sample log file for your perusal.

ForProcessFiles.log

My problem here is that when both Old and New pdf file was found and transferred already, in the log file it only remove the Old.pdf filename and retain the New.pdf filename. Below is the after getting the file from out folder.

Sample1_New.pdf
Sample2_New.pdf
Sample3_New.pdf
 

As you can see, all New.pdf was retained after running the program.

Edited by KickStarter15

Share this post


Link to post
Share on other sites

#4 ·  Posted

Untested but should work

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

$sFile = @ScriptDir & '\ForProcessFiles.log'
$aFiles = FileReadToArray($sFile)

For $i = UBound($aFiles)-1 to 0 step -1
   If FileExists("D:\Programs\MyAutoIt\out\" & $aFiles[$i]) Then
      ; do something
      _ArrayDelete($aFiles, $i)
Next
;_ArrayDisplay($aFiles)

_FileWriteFromArray($sFile, $aFiles)

 

1 person likes this

Share this post


Link to post
Share on other sites

#5 ·  Posted

@mikell,

Sorry for the late response, it was a weekend to our office. Anyways, tried already and it's not removing the filename in the log file but the file already exists in the folder.

I did not changed the code I just copied and pasted it in separate just to test.

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Ah. In ForProcessFiles.log the file was mentioned "Sample1_New.pdf" (with underscore) while you were checking "Sample1.New.pdf" (with a dot)
I thought that it was a typo. If not then of course it should be something like this

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

$sFile = @ScriptDir & '\ForProcessFiles.log'
$aFiles = FileReadToArray($sFile)

For $i = UBound($aFiles)-1 to 0 step -1
   $tmp = StringReplace($aFiles[$i], "_", ".", -1) ; replace the last underscore by a dot
   If FileExists("D:\Programs\MyAutoIt\out\" & $tmp) Then
      ; do something, ex. FileMove($tmp, ...) etc
      _ArrayDelete($aFiles, $i)
   EndIf   
Next
;_ArrayDisplay($aFiles)

_FileWriteFromArray($sFile, $aFiles)

 

Edited by mikell

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

:'(

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

Global $iOutputDir = "D:\Programs\MyAutoIt\out"
Global $sFileLog = @ScriptDir & '\ForProcessFiles.log'
Global $aFileList = FileReadToArray($sFileLog)
If @error Then Exit MsgBox(Null, Null, "Check log file: " & $sFileLog)
;~ _ArrayDisplay($aFileList)

For $i = UBound($aFileList) - 1 To 0 Step -2
    If FileExists($iOutputDir & "\" & $aFileList[$i]) Then
        ConsoleWrite("- FileMove: " & $aFileList[$i] & @CRLF)
        ConsoleWrite("+ TO > " & $aFileList[$i - 1] & @CRLF)
        If FileMove($aFileList[$i], $aFileList[$i - 1], 1) Then
            ConsoleWrite($i & " Move OK! " & @CRLF)
            _ArrayDelete($aFileList, $i)
            _ArrayDelete($aFileList, $i - 1)
        Else
            ConsoleWrite("! Move Error ! " & @CRLF)
        EndIf
    Else
        ConsoleWrite("! File not Exists: " & $aFileList[$i] & @CRLF)
    EndIf
Next
;~ _ArrayDisplay($aFileList)

_FileWriteFromArray($sFileLog, $aFileList)
;~ ShellExecute($sFileLog)

 

Edited by VIP
FileMove

Regards,
 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

@VIP

It's not removing the filename in log file and same in file path where the file was found. It will just ConsoleWrite "- FileMove: " and "+ TO >" but nothing is happening.

15 hours ago, VIP said:

:'(

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

Global $iOutputDir = "D:\Programs\MyAutoIt\out"
Global $sFileLog = @ScriptDir & '\ForProcessFiles.log'
Global $aFileList = FileReadToArray($sFileLog)
If @error Then Exit MsgBox(Null, Null, "Check log file: " & $sFileLog)
;~ _ArrayDisplay($aFileList)

For $i = UBound($aFileList) - 1 To 0 Step -2
    If FileExists($iOutputDir & "\" & $aFileList[$i]) Then
        ConsoleWrite("- FileMove: " & $aFileList[$i] & @CRLF)
        ConsoleWrite("+ TO > " & $aFileList[$i - 1] & @CRLF)
        If FileMove($aFileList[$i], $aFileList[$i - 1], 1) Then
            ConsoleWrite($i & " Move OK! " & @CRLF)
            _ArrayDelete($aFileList, $i)
            _ArrayDelete($aFileList, $i - 1)
        Else
            ConsoleWrite("! Move Error ! " & @CRLF)
        EndIf
    Else
        ConsoleWrite("! File not Exists: " & $aFileList[$i] & @CRLF)
    EndIf
Next
;~ _ArrayDisplay($aFileList)

_FileWriteFromArray($sFileLog, $aFileList)
;~ ShellExecute($sFileLog)

 

@mikell,

It is now working basically, but when it comes to the last line, it will not removed but the file was already FileMove from the path. I have this scenario that when processing these files not all were thrown out to "Out" folder sometimes there is/are late file that was fed to "Out" folder.

What I mean is for example:

There are 10 pdf files being processed and in the log of course there were 10 listed as well. Now, in return, there were only 8 pdf file was fed to out folder so basically in our log, there are two lines left right? the "Sample5_Old.pdf" and the "Sample5_New.pdf". The last two lines was not removed in the .log but the file from the out folder which is "D:\Programs\MyAutoIt\out\" was move using FileMove() to the feeder for output.

My log file before has:

Sample1_New.pdf
Sample1_Old.pdf
Sample2_New.pdf
Sample2_Old.pdf
Sample3_New.pdf
Sample3_Old.pdf
Sample4_New.pdf
Sample4_Old.pdf
Sample5_New.pdf
Sample5_Old.pdf

After:

Sample5_New.pdf
Sample5_Old.pdf

This means that "Sample5_New.pdf" and "Sample5_Old.pdf" is still waiting to exist in out folder.

And when exists, then filename in log file will not be removed/deleted. I think it was the last line in log file that has problem.:sweating:

Edited by KickStarter15

Share this post


Link to post
Share on other sites

#9 ·  Posted

Hmm it seems that the issue comes from _FileWriteFromArray (returns @error=5 if the array is empty)

Let's try another way - I changed the paths for the test

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

$sFile = @ScriptDir & '\ForProcessFiles.log'
$aFiles = FileReadToArray($sFile)
$content = FileRead($sFile)

For $i = 0 to UBound($aFiles)-1
   If FileExists(@ScriptDir & "\" & $aFiles[$i]) Then
      FileMove(@ScriptDir & "\" & $aFiles[$i], @ScriptDir & "\out\" & $aFiles[$i]) 
      $content = StringRegExpReplace($content, $aFiles[$i] & '\R?', "")
   EndIf   
Next
$hFile = FileOpen($sFile, 2)
FileWrite($hFile, $content)
FileClose($hFile)

 

1 person likes this

Share this post


Link to post
Share on other sites

#10 ·  Posted

@mikell, It worked perfectly, thanks:lol:. Now all I need to do is insert this to my code and see what happened.:sweating:

I just have one question::> Can you explain to me what is "\R?" from this function "StringRegExpReplace()" is for? quite confused a little bit. It seems new to me.:D

Share this post


Link to post
Share on other sites

#11 ·  Posted

Every time a file is found, its name is removed from the text of the log. You might do this using a simple : StringReplace($content, $aFiles[$i], "")
If you want to fire also the following @crlf  you could do : StringReplace($content, $aFiles[$i] & @crlf, "")
BUT the last line has no crlf, damn, what can I do to avoid an error ?
And the answer is : use a regular expression which allows you to make this crlf optional  :)
So in StringRegExpReplace($content, $aFiles[$i] & '\R?', ""),  "\R" means "newline sequence",  "?" means "optional", and then the whole expression means "replace the file and the possible newline"

 

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Wow:lmao:, thanks @mikell. That explains everything, do you have any link where I can study more about that? if you only have but if there's none it's fine no worries:sweating:. Thank you so much mikell "BIG BIG HELP".^_^ cheers!

Edited by KickStarter15

Share this post


Link to post
Share on other sites

#13 ·  Posted

There is plenty of links all over the web. I personally like this one and this one - among others
But as the regex syntax look a lot like klingon I firmly suggest to start at the very beginning, that is : the Autoit helpfile ( StringRegExp page and examples) and the countless examples of regular expressions in the forum threads  :)

Share this post


Link to post
Share on other sites

#14 ·  Posted

Very appreciated mikell, thank you so much for your help and support.:lol:

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