Write to Textfile activity use dynamic file name

Hello all,

I am trying to write to a text file with dynamic filename…date appended to filename name
“C:\Users\abc\Documents\UiPath\sample\logs"+“logs_”+DateTime.Now.ToString(“dd-MM-yyy hh:mm”)+”.txt"
using write file activity , above filename name
This gives error ,

Please advice , how can i resolve this issue

Hi …@Divya123 - Do you want to create a file under the Logs Folder right?

C:\Users\abc\Documents\UiPath\sample\logs\” - \ is missing…Could you please try?

oh sorry , \ is present , sorry it was a typo in the post

@Divya123 - Ok…could you please show the error screenshot?

@Divya123 - Here you go…I think i found the problem… DateTime.Now.ToString(“dd-MM-yyy hh:mm”)

“:” ==> is not supported character for the filename/foldername…so it will thrown an error…Please try like below

datetime.Now.ToString(“dd-MM-yyyy hh_mm”)

image

1 Like

Please find Screenshot below


image

RemoteException wrapping System.AggregateException: One or more errors occurred. —> RemoteException wrapping System.NotSupportedException: The given path’s format is not supported.
at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
at System.IO.FileStream.Init(String path,
FileMode mode,
FileAccess access,
Int32 rights,
Boolean useRights,
FileShare share,
Int32 bufferSize,
FileOptions options,
SECURITY_ATTRIBUTES secAttrs,
String msgPath,
Boolean bFromProxy,
Boolean useLongPath,
Boolean checkHost)
at System.IO.FileStream…ctor(String path,
FileMode mode,
FileAccess access,
FileShare share,
Int32 bufferSize,
FileOptions options,
String msgPath,
Boolean bFromProxy,
Boolean useLongPath,
Boolean checkHost)
at System.IO.StreamWriter.CreateFile(String path,
Boolean append,
Boolean checkHost)
at System.IO.StreamWriter…ctor(String path,
Boolean append,
Encoding encoding,
Int32 bufferSize,
Boolean checkHost)
at System.IO.File.InternalWriteAllText(String path,
String contents,
Encoding encoding,
Boolean checkHost)
at System.IO.File.WriteAllText(String path,
String contents)
at UiPath.Core.Activities.WriteTextFile.<>c__DisplayClass13_0.b__0()
at UiPath.Core.Activities.Retry.<>c__DisplayClass1_0.b__0()
at UiPath.Core.Activities.Retry.Do[ResultType](Func`1 action,
Int32 timeoutMS,
Int32 retryCount)
— End of inner exception stack trace —

Server stack trace:
at UiPath.Core.Activities.Retry.Do[ResultType](Func`1 action,
Int32 timeoutMS,
Int32 retryCount)
at UiPath.Core.Activities.Retry.Do(Action action,
Int32 timeoutMS,
Int32 retryCount)
at UiPath.Core.Activities.WriteTextFile.ExecuteAsync(String fileName,
String text,
String encodingStr)
at UiPath.Core.Activities.WriteTextFile.<>c__DisplayClass12_0.b__0()
at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md,
Object args,
Object server,
Object& outArgs)
at System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage msg,
IMessageSink replySink)

Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.EndInvokeHelper(Message reqMsg,
Boolean bProxyCase)
at System.Runtime.Remoting.Proxies.RemotingProxy.Invoke(Object NotUsed,
MessageData& msgData)
at System.Action.EndInvoke(IAsyncResult result)
at UiPath.Core.Activities.WriteTextFile.EndExecute(AsyncCodeActivityContext context,
IAsyncResult result)
at System.Activities.AsyncCodeActivity.System.Activities.IAsyncCodeActivity.FinishExecution(AsyncCodeActivityContext context,
IAsyncResult result)
at System.Activities.AsyncCodeActivity.CompleteAsyncCodeActivityData.CompleteAsyncCodeActivityWorkItem.Execute(ActivityExecutor executor,
BookmarkManager bookmarkManager)

@Divya123 - As suspected…please change as shown above…also you are missing a ‘y’ it should be yyyy…

Please refer this link for more datatime.now options…

Thanks prasanth,i did try still gives error
image

Let me try other formats mentioned in the link

@Divya123 - You are still having “:” . I had asked to change it to "datetime.Now.ToString(“dd-MM-yyyy hh_mm”) ==> like this…

Those format will work if you writing it to excel or text file…not for the filename or foldername since there is a character “:” .

oh i just saw your above response now…missed that reply…Thanks Prashanth…let me try it out

1 Like

Thanks Prashanth , it worked fine now.

I have another question , if need to append logs to this file created , how can pick the same file , as the minute would have changed when the next append line comes…any suggestion other than excluding the hh_mm from file…what if i need to created custom logs for every run in a day
Please advice.

@Divya123 - Instead of Write Line to Text file…Try using Append Line activity…

But before that , you have to remove the “hh_mm” from it…or else it will not write to the same file…

Once you appending enough lines to that particular log, you can read that file and add the time component and save it…

1 Like

ok thats nice…let me try it out …thanks Prashanth,appreciate your response.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.