Jump to content

Rename files with its first line


gear
 Share

Recommended Posts

;===============================================================================
;
; Function Name:  RenameFileWithFirstLine
; Description:: Rename the files with its first line, if the first line is only spaces or blank, find the next line.
;if there are some spaces between words, only all the spaces except one single space will be removed.
; Parameter(s): $Folder - A folder name, the files within it will be renamed
;              $FileExtention - The exstention of the file to be renamed, default is *.txt
; Requirement(s):  #include <File.au3>,#include <Array.au3>,#include <Misc.au3>
; Author(s):       Gear
;Notes:       If there is(are) files is failed to be renamed, the file name will be wrote to console and the Beep sound will remind program runner.
; Example:     RenameFileWithFirstLine("c:\1")
;
;===============================================================================
;
Func RenameFileWithFirstLine($Folder, $FileExtention = "*.txt")
    $FileList = _FileListToArray($Folder, $FileExtention, 1)
    $FileEx = StringReplace($FileExtention, "*", "")
    If IsArray($FileList) And $FileList[0] >= 1 Then; when there is no file within $Folder, $FileList is not a array.
        For $j = 1 To $FileList[0];so $FileList[0] leads to error
            $CurrentFile = $Folder & "\" & $FileList[$j]
            $fileOpen = FileOpen($CurrentFile, 0)
            $FileContent = FileRead($fileOpen)
            If $fileOpen Then
                $WhichLine = 1
                $NotFound = 1
                $LineContent = FileReadLine($fileOpen, $WhichLine)
                While ($NotFound And @error <> -1 And IsString($LineContent))
                    $TheUnWelcomeCharacterInFileName = '<|>|?|:|*|\|/|"|  |  |  |  ';these are the unacceptable characters in file name
                    $AfterStripName0 = _StringRemove($LineContent, $TheUnWelcomeCharacterInFileName, "|")
                    $AfterStripName1 = _StringRemove($AfterStripName0, "  |  ", "|")
                    $Len0 = StringLen($AfterStripName0)
                    $Len1 = StringLen($AfterStripName1)
                    While $Len1 <> $Len0;it can remove all the white space betwwen strings but still ramians the only one betwwen strings if there is(are) spaces originally.
                        $Len0 = $Len1
                        $AfterStripName1 = _StringRemove($AfterStripName1, "  |  ", "|")
                        $Len1 = StringLen($AfterStripName1)
                    WEnd
                    If ($AfterStripName1 == "" Or $AfterStripName1 == " " Or $AfterStripName1 == " ") Then
                        $WhichLine = $WhichLine + 1
                        $LineContent = FileReadLine($fileOpen, $WhichLine)
                    Else
                        $NotFound = 0
                    EndIf
                WEnd
                $IfClose = FileClose($fileOpen)
                If $NotFound == 0 Then
                    $name = $Folder & "\" & $AfterStripName1 & $FileEx
                    If $name <> $CurrentFile Then
                        $file = FileOpen($name, 10)
                        $ifgood = FileWrite($file, $FileContent)
                        $IfClose = FileClose($file)
                        If ($ifgood And $IfClose) Then
                            $IfDel = FileDelete($CurrentFile)
                        Else
                            Beep(200, 100)
                            ConsoleWrite("Fail to Rename file: " & $CurrentFile & @CRLF)
                        EndIf
                    EndIf
                EndIf
            Else
                Beep(500, 100)
                ConsoleWrite("Fail to open file: " & $CurrentFile & @CRLF)
            EndIf
        Next
    Else
        ConsoleWrite("There is no file within " & $Folder & @CRLF)
    EndIf
    $FolderList = _FileListToArray($Folder, "*", 2);when within folder there is no file and folder, $FolderList is not an array.
    If IsArray($FolderList) And $FolderList[0] >= 1 Then;when within folder there is(are) files but no child folder,
        For $i = 1 To $FolderList[0]; $FolderList is an array but $FolderList[0] is nothing(but not 0), so here must use $FolderList[0] >= 1
            RenameFileWithFirstLine($Folder & "\" & $FolderList[$i], $FileExtention)
        Next
    Else
        ConsoleWrite("There is no child folder within " & $Folder & @CRLF)
    EndIf
EndFunc  ;==>RenameFileWithFirstLine

;===============================================================================
;
; Function Name:   _StringRemove
; Description:: Removes certain characters or phrases from a string
; Parameter(s): $s_string - String
;              $s_remove - Characters to be removed (separated by $s_delim)
;              $s_delim[optional] (delimiter '|' by default)
; Requirement(s):  None
; Return Value(s): On Success - String with removed characters
;              On Failure - 0
; Author(s):       RazerM
; Notes:           Only useful when removing lots of characters
; Example:     MsgBox(0,0,_StringRemove("102.3040506.0700...80900.", "0|."))
;
;===============================================================================
;
Func _StringRemove($s_string, $s_remove, $s_delim = "|")
    $s_remove = StringSplit($s_remove, $s_delim)
    If @error Then Return 0
    For $i = 1 To $s_remove[0]
        $s_string = StringReplace($s_string, $s_remove[$i], " ")
        If @error Then Return 0
    Next
    Return $s_string
EndFunc  ;==>_StringRemove

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