How to rename a file when it arrives in a specific folder?

306 Views Asked by At

I'm using Playwright to record test videos and save them to a specific folder. You aren't able to rename these video files when they're made in Playwright, but I need file names to be date and time of recording. In AutoIt how to rename a file to the FileGetTime() return once it's added to the Playwright videos folder?

I tried FileFindFirstFile(! "2023") to find files that haven't been renamed already. Then FileGetTime() to get the string I want to rename it as. Then a FileMove() to rename file to the time while keeping it in same folder.

1

There are 1 best solutions below

0
user4157124 On

"I tried FileFindFirstFile(! "2023") to find files …"

Consider using _FileListToArray() instead.

"… files that haven't been renamed already."

Use StringRegExp($sFileName, '^\d{14}\.', $STR_REGEXPMATCH) (and/or $FC_NOOVERWRITE on FileMove()) instead.

"In AutoIt how to rename a file to the FileGetTime() return …"

Example:

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

Global Const $g_sFolder = 'C:\location\'

FileRenameDateTimeCreated($g_sFolder)
ShellExecute($g_sFolder)

Func FileRenameDateTimeCreated(Const $sFolder, Const $sFilter = '')
    Local Const $aFile = _FileListToArray($sFolder, $sFilter, $FLTA_FILES, False)
    Local       $sName = ''

    For $i1 = 1 To $aFile[0]

        If StringRegExp($aFile[$i1], '^\d{14}\.', $STR_REGEXPMATCH) Then _
        ContinueLoop

        $sName = FileGetTime($sFolder & $aFile[$i1], $FT_CREATED, $FT_STRING) _
               & StringRight($aFile[$i1], StringInStr($aFile[$i1], '.', 0, -1))

        FileMove($sFolder & $aFile[$i1], $sFolder & $sName, $FC_NOOVERWRITE)

    Next

EndFunc