web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Extracting Date inform...
Power Automate
Unanswered

Extracting Date information to be used for creating folders.

(0) ShareShare
ReportReport
Posted on by 22

Hello all I am new to power automate desktop.  and I need to create a very robust flow where I need to copy files from a computer.  Based on the month and year create the folders accordingly and move the files to those folders.  I am having trouble trying to figure out how to get the date information to parse out the month and year.

 

Please Point me in the right direction!

  • CU16071609-1 Profile Picture
    6,255 Moderator on at

    @eyarea51 

     

    Could you please share some info how your file name looks like?

     

    Share some screenshot if possible how your files are looks like in your folder

     


    Thanks,
    Deenuji Loganathan 👩‍💻
    Automation Evangelist 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀

  • eyarea51 Profile Picture
    22 on at

    The File name would be WIN_20240626_11_35_30_Pro.jpg

  • NathanAlvares24 Profile Picture
    1,714 Moderator on at

    Hi @eyarea51 !

     

    You can try this flow.

    NathanAlvares24_7-1719423153566.png

     

    NathanAlvares24_6-1719423132226.png

     

    Code (just copy paste the code into your flow):

     

    SET FolderPath TO $'''C:\\Users\\UserName\\Desktop\\Testing files'''
    Folder.GetFiles Folder: FolderPath FileFilter: $'''WIN*''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Files=> Files
    LOOP FOREACH File IN Files
     SET FileName TO File.NameWithoutExtension
     Text.SplitText.SplitWithDelimiter Text: FileName CustomDelimiter: $'''_''' IsRegEx: False Result=> TextFileList
     Text.ConvertTextToDateTime.ToDateTimeCustomFormat Text: TextFileList[1] CustomFormat: $'''yyyyMMdd''' DateTime=> DateFormatted
     Text.ConvertDateTimeToText.FromCustomDateTime DateTime: DateFormatted CustomFormat: $'''yyyy''' Result=> strYear
     Text.ConvertDateTimeToText.FromCustomDateTime DateTime: DateFormatted CustomFormat: $'''MMM''' Result=> strMonth
     IF (Folder.IfFolderExists.Exists Path: $'''%FolderPath%\\%strYear%''') THEN
     IF (Folder.IfFolderExists.DoesNotExist Path: $'''%FolderPath%\\%strYear%\\%strMonth%''') THEN
     Folder.Create FolderPath: $'''%FolderPath%\\%strYear%''' FolderName: strMonth Folder=> NewFolder
     END
     ELSE
     Folder.Create FolderPath: FolderPath FolderName: strYear Folder=> NewFolder
     Folder.Create FolderPath: $'''%FolderPath%\\%strYear%''' FolderName: strMonth Folder=> NewFolder
     END
     File.Move Files: File Destination: $'''%FolderPath%\\%strYear%\\%strMonth%''' IfFileExists: File.IfExists.DoNothing MovedFiles=> MovedFiles1
    END

     

     

    Before:

    NathanAlvares24_2-1719421193634.png

     

    After:

    NathanAlvares24_0-1719423037951.png

    NathanAlvares24_1-1719423048421.png

    NathanAlvares24_2-1719423062475.png

     

    NathanAlvares24_3-1719423077711.png

    NathanAlvares24_4-1719423084834.png

     

     

     

    So all you need to do is just copy your files into the Main Folder. Use Get files in folder action with filter as "WIN*" to get all files starting with that irrespective of extension. And it will do the work.

     

    I hope this helps.

  • CU16071609-1 Profile Picture
    6,255 Moderator on at

    @eyarea51 

     

    Please follow the below approach:

    Deenuji_1-1719422139819.png

    Deenuji_2-1719422162993.png

     

    Before:

    Deenuji_0-1719422475445.png

     

     

    After:

    Deenuji_1-1719422642011.png

     

     

    Code:

     

    Folder.GetFiles Folder: $'''C:\\Boot\\Images''' FileFilter: $'''*''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Files=> Files
    SET Root_Folder TO $'''C:\\Boot\\Images'''
    LOOP FOREACH CurrentItem IN Files
     SET FileName TO CurrentItem.Name
     @@copilotGeneratedAction: 'False'
    Scripting.RunPowershellScript.RunPowershellScript Script: $'''# Filename
    $filename = \"%FileName%\"
    
    # Regex pattern to match and capture the year and month
    $pattern = \'WIN_(\\d{4})(\\d{2})\\d{2}_\\d{2}_\\d{2}_\\d{2}\'
    
    # Use regex to match and extract the year and month
    if ($filename -match $pattern) {
     $year = $matches[1]
     $monthNumber = $matches[2]
    
     # Convert month number to month name
     $monthName = [CultureInfo]::CurrentCulture.DateTimeFormat.GetMonthName([int]$monthNumber)
     
     # Output the extracted year and month name
     Write-Output \"$year\"
     Write-Output \"$monthName\"
    } else {
     Write-Output \"Filename format does not match the expected pattern.\"
    }''' ScriptOutput=> PowershellOutput
     IF NotContains(PowershellOutput, $'''Filename format''', True) THEN
     Text.SplitText.Split Text: PowershellOutput StandardDelimiter: Text.StandardDelimiter.NewLine DelimiterTimes: 1 Result=> TextList
     SET Str_Year TO TextList[0]
     SET Str_Month TO TextList[1]
     IF (Folder.IfFolderExists.DoesNotExist Path: $'''%Root_Folder%\\%Str_Year%''') THEN
     Folder.Create FolderPath: Root_Folder FolderName: Str_Year Folder=> NewFolder
     END
     IF (Folder.IfFolderExists.DoesNotExist Path: $'''%Root_Folder%\\%Str_Year%\\%Str_Month%''') THEN
     Folder.Create FolderPath: $'''%Root_Folder%\\%Str_Year%''' FolderName: Str_Month Folder=> NewFolder
     END
     File.Move Files: CurrentItem.FullName Destination: $'''%Root_Folder%\\%Str_Year%\\%Str_Month%''' IfFileExists: File.IfExists.DoNothing MovedFiles=> MovedFiles
     END
    END

     

     

    Please adjust the configuration of the folders specified in the variable to suit your requirements.

     

    How to copy/paste the above code into your power automate desktop?

    Deenuji_3-1719422227234.gif

     


    Thanks,
    Deenuji Loganathan 👩‍💻
    Automation Evangelist 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀

  • eyarea51 Profile Picture
    22 on at

    Thank you for the information.

     

    I do like to create the flow to help me learn.  

     

    I did run into an issue as I am creating and watching the data.

     

    the DateFormated is not getting the rite data, the convert text to datetime does not seem to gett he right part of the file name.  see my screenshot

     

    eyarea51_0-1719427285374.png

     

  • eyarea51 Profile Picture
    22 on at

    I have Found My Mistake in the line.

     

    Now I need to efficiently rename and move or Move and rename the file.

     

     

  • NathanAlvares24 Profile Picture
    1,714 Moderator on at

    Hi @eyarea51 !

     

    What does your "TextFileList" look like?

    Mine is this:

    NathanAlvares24_0-1719429078431.png

    Hence I used TextFileList[1] since that has the format of date as yyyyMMdd

    NathanAlvares24_1-1719429219562.png

    And I used the one marked in red as this is the required information for extraction.

     

    Can you show screenshot what your "TextFileList" shows like? I can help in the extraction of the date.

  • NathanAlvares24 Profile Picture
    1,714 Moderator on at

    Hi @eyarea51 !

     

    Do you need help in the part where you need to move and rename i.e. after the files have been moved to their locations? Any particular format it requires? I would gladly assist. If yes, do provide some more info in that case.

  • eyarea51 Profile Picture
    22 on at

    I added another get files folder after the rename so the move would work.    here is the code is theis the best way to do this?  

     

    FUNCTION Main_copy GLOBAL
    SET FolderPath TO $'''\\\\XXXXXXXX\\Users\\XXXXX\\Pictures\\Camera Roll'''
    SET FolderPath2 TO $'''C:\\Workspace'''
    Folder.GetFiles Folder: FolderPath FileFilter: $'''WIN*''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Files=> Files
    LOOP FOREACH File IN Files
    SET FileName TO File.NameWithoutExtension
    Text.SplitText.SplitWithDelimiter Text: FileName CustomDelimiter: $'''_''' IsRegEx: False Result=> TextFileList
    Text.ConvertTextToDateTime.ToDateTimeCustomFormat Text: TextFileList[1] CustomFormat: $'''yyyyMMdd''' DateTime=> DateFormatted
    Text.ConvertDateTimeToText.FromCustomDateTime DateTime: DateFormatted CustomFormat: $'''yyyy''' Result=> strYear
    Text.ConvertDateTimeToText.FromCustomDateTime DateTime: DateFormatted CustomFormat: $'''MMM''' Result=> strMonth
    IF (Folder.IfFolderExists.Exists Path: $'''%FolderPath2%\\%strYear%''') THEN
    IF (Folder.IfFolderExists.DoesNotExist Path: $'''%FolderPath2%\\%strYear%\\%strMonth%''') THEN
    Folder.Create FolderPath: $'''%FolderPath2%\\%strYear%''' FolderName: strMonth Folder=> NewFolder
    END
    ELSE
    Folder.Create FolderPath: FolderPath2 FolderName: strYear Folder=> NewFolder
    Folder.Create FolderPath: $'''%FolderPath2%\\%strYear%''' FolderName: strMonth Folder=> NewFolder
    END
    File.RenameFiles.RenameReplaceText Files: File TextToReplace: $'''WIN''' ReplaceWith: $'''REG1''' IfFileExists: File.IfExists.DoNothing RenamedFiles=> RenamedFiles
    Folder.GetFiles Folder: FolderPath FileFilter: $'''REG1*''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Files=> Files2
    File.Move Files: Files2 Destination: $'''%FolderPath2%\\%strYear%\\%strMonth%''' IfFileExists: File.IfExists.DoNothing MovedFiles=> MovedFiles1
    END
    END FUNCTION

  • CU16071609-1 Profile Picture
    6,255 Moderator on at

    @eyarea51 

     

    I have proposed a similar method to what you described above, but my process involves using a PowerShell script to output the year and month, then it creates a folder for them and moves the files into it.

     


    Thanks,
    Deenuji Loganathan 👩‍💻
    Automation Evangelist 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Automate

#1
Mohsin Ali Profile Picture

Mohsin Ali 430

#2
11manish Profile Picture

11manish 341 Super User 2026 Season 2

#3
M Bilal Khan Profile Picture

M Bilal Khan 282

Last 30 days Overall leaderboard