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 / Power automate desktop...
Power Automate
Answered

Power automate desktop using a var from pythn script

(0) ShareShare
ReportReport
Posted on by 39

Hi,

 

I'm trying to create a PAD flow where I'm moving files to folders.
The foldres should by in a format of YYYYmmm.
The file name have a long name and part of the name is a date (example for a file name:"AC01DA7SBC02__23.01.13-03.43.03_00001.csv"
I want to take the year and month from the file name and check if there is a folder with this name.

To do so I have used a python script as bellow, and I'm checking if the folder exists If it doesn't exist I want to create it.
The problem is that when I'm trying to do it I get a massage that the path is using "Illegal characters in path".
I have tried to add steps which are looking for invisible characters but it didn't help.
When I tried to hard-code the output of the python (2023Jan) it worked.

Can anyone help me?

My Python code:
import re
from datetime import datetime

# Set the file name
file_name = "%FileName%"

# Extract the month from the file name using regular expressions
match = re.search(r"_(\d{2})\.(\d{2})\.", file_name)
year = match.group(1)
month = match.group(2)

# Convert the input string to a datetime object
dt = datetime.strptime(year + month, "%%y%%m")

# Format the datetime object as a string in the desired format and replace illegal characters
foldername = dt.strftime("%%Y%%b").replace('/', '-').replace('\\', '_').strip()


# Print the result
print(foldername)

My flow:

Moshema_1-1676561676962.png

 


My flow failure:

Moshema_0-1676561643525.png

 


Error massage:
Illegal characters in path.: Microsoft.Flow.RPA.Desktop.Modules.SDK.ActionException: Can't create folder 2023Jan
into C:\SFTPROOT\SDR_In\Archieved\. ---> System.ArgumentException: Illegal characters in path.
at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
at System.IO.Path.Combine(String path1, String path2)
at Microsoft.Flow.RPA.Desktop.Modules.Folder.Actions.FolderActions.CreateFolder(Variant newFolderPath, Variant newFolderName, Variant& newFolder)
--- End of inner exception stack trace ---
at Microsoft.Flow.RPA.Desktop.Modules.Folder.Actions.CreateFolder.Execute(ActionContext context)
at Microsoft.Flow.RPA.Desktop.Robin.Engine.Execution.ActionRunner.Run(IActionStatement statement, Dictionary`2 inputArguments, Dictionary`2 outputArguments)



  • Verified answer
    ZS440 Profile Picture
    262 on at

    Hi. @Moshema 

    As you can see when you Escape Text PythonScriptOutput, there are line feed codes.

    ZS440_1-1676639760387.png

     

    ZS440_0-1676639503625.png

    ZS440_2-1676639782362.png

    Can you create a folder by setting %PythonScriptOutput.Trimmed%?

     

    ZS440_3-1676639824222.png

    However, PythonScriptOutput has problems such as not being able to convert to numbers even if it is trimmed, so it might be better to implement it using only the basic actions of Power Automate for desktop.

    ZS440_4-1676642683453.png

     

     

     

  • Kaif_Siddique Profile Picture
    2,108 Super User 2024 Season 1 on at

    Another solution.

    This will work if naming convention is standard. Customize the flow as per your need(pass the file into set variable)

     

    Kaif_Siddique_0-1676661097435.png

    Copy it and paste to flow:

    SET strVal TO $'''AC01DA7SBC02__23.01.13-03.43.03_00001.csv'''
    Text.SplitText.SplitWithDelimiter Text: strVal CustomDelimiter: $'''__''' IsRegEx: False Result=> TextList
    Text.SplitText.SplitWithDelimiter Text: TextList[1] CustomDelimiter: $'''-''' IsRegEx: False Result=> TextList1
    Text.ConvertTextToDateTime.ToDateTimeCustomFormat Text: TextList1[0] CustomFormat: $'''yy.MM.dd''' DateTime=> TextAsDateTime
    Text.ConvertDateTimeToText.FromCustomDateTime DateTime: TextAsDateTime CustomFormat: $'''yyyyMMM''' Result=> FormattedDateTime

     

    If I have answered your question, please mark my post as Solved.

    If you like my response, please give it a Thumbs Up.

     

    Regards

    Kaif

  • Moshema Profile Picture
    39 on at

    Thanks! .trimmed did the job

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
11manish Profile Picture

11manish 237 Super User 2026 Season 2

#2
David_MA Profile Picture

David_MA 227 Super User 2026 Season 2

#3
Mohsin Ali Profile Picture

Mohsin Ali 194

Last 30 days Overall leaderboard