Jump to content

Find string/word in a textfile and move the textfile


Go to solution Solved by Luke94,

Recommended Posts

Hi! I want to do the script, which will search through files (.md files, does Autoit work with them btw?) in a fixed folder, search for a certain fixed word (or a string, it doesn't matter for my needs, would like to do what's easier) and if it will find it, the file will be moved to a different fixed folder.

I need to do this script, but I have no idea how to do the scripts :) So can you help me please?

Link to post
Share on other sites
  • Solution

This will do what you want.

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

Global $g_sDir = @ScriptDir ; Directory of *.md files
Global $g_aFiles
Global $g_sOutputDir = @ScriptDir & '\Output\' ; Output directory of *.md files - Must have the '\' on the end otherwise will output to a file, not a folder
Global $g_sFindText = 'text' ; Text to find
Global $g_aMatchedFiles

$g_aFiles = _FileListToArrayRec($g_sDir, '*.md', $FLTAR_FILES, $FLTAR_RECUR, Default, $FLTAR_FULLPATH) ; Find all files in $g_sDir with file extension .md
If @ERROR Then
    ConsoleWrite('ERROR: _FileListToArrayRec (@EXTENDED = ' & @EXTENDED & ')' & @CRLF)
    Exit
EndIf
$g_aMatchedFiles = _SearchFiles()
If UBound($g_aMatchedFiles) > 0 Then ; If we've found some files that contain the text then..
    For $i = 0 To (UBound($g_aMatchedFiles) - 1) Step 1 ; Loop through all the file paths that contain the text
        FileMove($g_aMatchedFiles[$i], $g_sOutputDir, ($FC_OVERWRITE + $FC_CREATEPATH)) ; Move the file to the Output directory
        ConsoleWrite('"' & $g_aMatchedFiles[$i] & '" moved to "' & $g_sOutputDir & '"' & @CRLF)
    Next
Else
    ConsoleWrite('No *.md files found in ' & $g_sDir & ' containing "' & $g_sFindText & '"' & @CRLF)
EndIf

Func _SearchFiles()
    Local $hFile
    Local $sText
    Local $aMatchedFiles[0]
    For $i = 1 To $g_aFiles[0] Step 1 ; Loop through all files found in $g_sDir with file extension .md
        $hFile = FileOpen($g_aFiles[$i], $FO_READ) ; Open the file for reading
        $sText = ''
        If $hFile = -1 Then
            ConsoleWrite('ERROR: Failed to open ' & $g_aFiles[$i] & @CRLF)
            ContinueLoop
        EndIf
        $sText = FileRead($hFile) ; Read the entire file to $sText
        If @ERROR Then
            ConsoleWrite('ERROR: Failed to read ' & $g_aFiles[$i] & @CRLF)
        EndIf
        If StringLen($sText) > 0 Then ; Check the returned text isn't empty
            If StringInStr($sText, $g_sFindText) > 0 Then ; Search the returned text for the text you're looking for
                _ArrayAdd($aMatchedFiles, $g_aFiles[$i]) ; Add the file path to an array so we can later move the files
            EndIf
        EndIf
        FileClose($hFile) ; Close the file
    Next
    Return $aMatchedFiles
EndFunc

I've left details of the script in the comments.

I find learning easier to breakdown the code and figure out myself how it's working, but that's just me.

If you'd like to learn from scratch, look at these functions:

_FileListToArrayRecFileOpenFileRead and FileMove

Link to post
Share on other sites
1 hour ago, Luke94 said:

This will do what you want.

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

Global $g_sDir = @ScriptDir ; Directory of *.md files
Global $g_aFiles
Global $g_sOutputDir = @ScriptDir & '\Output\' ; Output directory of *.md files - Must have the '\' on the end otherwise will output to a file, not a folder
Global $g_sFindText = 'text' ; Text to find
Global $g_aMatchedFiles

$g_aFiles = _FileListToArrayRec($g_sDir, '*.md', $FLTAR_FILES, $FLTAR_RECUR, Default, $FLTAR_FULLPATH) ; Find all files in $g_sDir with file extension .md
If @ERROR Then
    ConsoleWrite('ERROR: _FileListToArrayRec (@EXTENDED = ' & @EXTENDED & ')' & @CRLF)
    Exit
EndIf
$g_aMatchedFiles = _SearchFiles()
If UBound($g_aMatchedFiles) > 0 Then ; If we've found some files that contain the text then..
    For $i = 0 To (UBound($g_aMatchedFiles) - 1) Step 1 ; Loop through all the file paths that contain the text
        FileMove($g_aMatchedFiles[$i], $g_sOutputDir, ($FC_OVERWRITE + $FC_CREATEPATH)) ; Move the file to the Output directory
        ConsoleWrite('"' & $g_aMatchedFiles[$i] & '" moved to "' & $g_sOutputDir & '"' & @CRLF)
    Next
Else
    ConsoleWrite('No *.md files found in ' & $g_sDir & ' containing "' & $g_sFindText & '"' & @CRLF)
EndIf

Func _SearchFiles()
    Local $hFile
    Local $sText
    Local $aMatchedFiles[0]
    For $i = 1 To $g_aFiles[0] Step 1 ; Loop through all files found in $g_sDir with file extension .md
        $hFile = FileOpen($g_aFiles[$i], $FO_READ) ; Open the file for reading
        $sText = ''
        If $hFile = -1 Then
            ConsoleWrite('ERROR: Failed to open ' & $g_aFiles[$i] & @CRLF)
            ContinueLoop
        EndIf
        $sText = FileRead($hFile) ; Read the entire file to $sText
        If @ERROR Then
            ConsoleWrite('ERROR: Failed to read ' & $g_aFiles[$i] & @CRLF)
        EndIf
        If StringLen($sText) > 0 Then ; Check the returned text isn't empty
            If StringInStr($sText, $g_sFindText) > 0 Then ; Search the returned text for the text you're looking for
                _ArrayAdd($aMatchedFiles, $g_aFiles[$i]) ; Add the file path to an array so we can later move the files
            EndIf
        EndIf
        FileClose($hFile) ; Close the file
    Next
    Return $aMatchedFiles
EndFunc

I've left details of the script in the comments.

I find learning easier to breakdown the code and figure out myself how it's working, but that's just me.

If you'd like to learn from scratch, look at these functions:

_FileListToArrayRecFileOpenFileRead and FileMove

Thank you very much for your time, works perfectly fine!
Also thanks for learning recommendations, will check it out.

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...