Jump to content

Rearrange multiple filenames.


Recommended Posts

Hello autoit lovers, What I'm trying to do is change the filenames of the text files I have. 3 different functions I wrote work separately, but I want them to be done in a single operation. Thanks in advance for your help and support in this matter. Example: The file name is 25.08.2022.txt. Converting the file names in this form to 20220825.txt.

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

Global $TextList, $LstStr

_Main()

Func _Main()
    _FileLst()
    _FileRnm()
    _FileMove()
EndFunc

Func _FileLst()
    $TextList = _FileListToArray(@ScriptDir, "*.txt")
    If @error = 1 Then
        MsgBox(0, "", "Path was invalid.")
        Exit
    EndIf
    If @error = 4 Then
        MsgBox(0, "", "No file(s) were found.")
        Exit
    EndIf
    _ArrayDisplay($TextList, "Text File List")
EndFunc

Func _FileRnm()
    Local $Length, $ModStr, $LefStr, $MedStr, $RigStr
    $Length = StringLen($TextList)
    $ModStr = StringReplace($TextList, ".", "")
    If $Length == 10 Then
        $LefStr = StringLeft($ModStr, 2)
        $MedStr = StringMid($ModStr, 3, 2)
        $RigStr = StringRight($ModStr, 4)
    Else
        $LefStr = StringLeft($ModStr, 1)
        $MedStr = StringMid($ModStr, 2, 2)
        $RigStr = StringRight($ModStr, 4)
    EndIf
    $LstStr = $RigStr & $MedStr & $LefStr
EndFunc

Func _FileMove()
    FileMove(@ScriptDir & "\" & $LstStr & ".txt", @ScriptDir & "\Modify\", $FC_OVERWRITE + $FC_CREATEPATH)
    ShellExecute(@ScriptDir & "\Modify\")
EndFunc

:ILA2:

Link to comment
Share on other sites

Maybe something like:

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

Global $g_sSourceDir = @ScriptDir & "\Original"
Global $g_sTargetDir = @ScriptDir & "\Modify"

_FileCopyRename($g_sSourceDir, $g_sTargetDir)

Func _FileCopyRename($_sSourceDir = $g_sSourceDir, $_sTargetDir = $g_sTargetDir)
    Local $aTargetFile
    Local $aFileList = _FileListToArray($_sSourceDir, "*.txt", 1, 0)
        If @error Then Exit
        ;~ Add Target Directory Column
        _ArrayColInsert($aFileList, 1)
        For $i = 1 To $aFileList[0][0]
            $aTargetFile = StringSplit($aFileList[$i][0], ".")
            ;~ If Array count equals 4 <Day>, <Month>, <Year>, <extension>
            If $aTargetFile[0] = 4 Then
                $aFileList[$i][1] = $aTargetFile[3] & $aTargetFile[2] & $aTargetFile[1] & "." & $aTargetFile[4]
                FileMove($_sSourceDir & "\" & $aFileList[$i][0], $_sTargetDir & "\" & $aFileList[$i][1], 9)
            EndIf
        Next
EndFunc

 

Link to comment
Share on other sites

@Subz

optimized version of your script without unneccessary _ArrayColInsert():

#include <File.au3>

Global $g_sSourceDir = @ScriptDir & "\Original"
Global $g_sTargetDir = @ScriptDir & "\Modify"

_FileCopyRename($g_sSourceDir, $g_sTargetDir)

Func _FileCopyRename($_sSourceDir = $g_sSourceDir, $_sTargetDir = $g_sTargetDir)
    Local $aTargetFile
    Local $aFileList = _FileListToArray($_sSourceDir, "*.txt", 1, 0)
        If @error Then Exit
        For $i = 1 To $aFileList[0]
            $aTargetFile = StringSplit($aFileList[$i], ".")
            ;~ If Array count equals 4 <Day>, <Month>, <Year>, <extension>
            If $aTargetFile[0] = 4 Then
                $sNewName = $aTargetFile[3] & $aTargetFile[2] & $aTargetFile[1] & "." & $aTargetFile[4]
                FileMove($_sSourceDir & "\" & $aFileList[$i], $_sTargetDir & "\" & $sNewName, 9)
            EndIf
        Next
EndFunc

 

Edited by Zedna
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...