Jump to content

Can't create file with strings and macros in filename.


Bagel
 Share

Recommended Posts

I know I'm probably missing something simple and obvious here but I can't see what it is. I didn't see anything in the help file that could help me with this and I've seen similar examples working in other topics so I can't explain it.

 

#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <WinAPISysWin.au3>
#include <WindowsConstants.au3>
#include <Inet.au3>
#Include <File.au3>
#include <FileConstants.au3>
#include <WinAPIFiles.au3>
#include <TrayConstants.au3>
#include <Date.au3>

   $FileName = "C:\" & @YEAR & "-" & @MON & "-" & @MDAY & "-" & "TEST" & "-" & _DateTimeFormat(_NowCalc(), 5) & ".txt"

   ConsoleWrite ( $FileName )

   $Variable = 6
   $File = FileOpen($FileName, 1)
   FileWriteLine($File, "This is a test to see if I can write lines and variables to a text file... " & $Variable & @CRLF )
   FileClose($File)

 

Link to comment
Share on other sites

I tried #RequireAdmin but also tried another directory on my desktop. The following works fine but using the original FileName with the date and times fails silently:

#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <WinAPISysWin.au3>
#include <WindowsConstants.au3>
#include <Inet.au3>
#Include <File.au3>
#include <FileConstants.au3>
#include <WinAPIFiles.au3>
#include <TrayConstants.au3>
#include <Date.au3>
#RequireAdmin

   ;$FileName = "C:\" & @YEAR & "-" & @MON & "-" & @MDAY & "-" & "TEST" & "-" & _DateTimeFormat(_NowCalc(), 5) & ".txt"
   $FileName = "C:\TEST4.txt"

   ConsoleWrite ( $FileName )

   $Variable = 6
   $File = FileOpen($FileName, 1)
   FileWriteLine($File, "This is a test to see if I can write lines and variables to a text file... " & $Variable & @CRLF )
   FileClose($File)

I was able to create this on the desktop as well without RequireAdmin but can't see why the compound name fails. And that's what I need to work.

Link to comment
Share on other sites

3 hours ago, Bagel said:

but can't see why the compound name fails.

Did you see what @Nine wrote?

When you use _DateTimeFormat() and _NowCalc() functions, you are using a character that Windows doesn't allow in a filename.

Could you please post what is returned if you try this?

ConsoleWrite(_Now() & @CRLF)

:)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

OH! Because of the format I selected for the date a while ago I didn't explicitly see that it was adding colons into the file name, saw the colon in the drive directory and noticed that it was working with one of my tests and didn't think anything more of it. Didn't help that FileOpen was failing silently. This is what I get for not stopping after getting burned out working on this project. Thanks! The following code works perfectly:

 

#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <WinAPISysWin.au3>
#include <WindowsConstants.au3>
#include <Inet.au3>
#Include <File.au3>
#include <FileConstants.au3>
#include <WinAPIFiles.au3>
#include <TrayConstants.au3>
#include <Date.au3>
#RequireAdmin

   $FileName = "C:\" & @YEAR & "-" & @MON & "-" & @MDAY & "-" & "TEST" & "-" & StringReplace( _DateTimeFormat(_NowCalc(), 5) , ":", "-" ) & ".txt"
   $DateTest = StringReplace( _DateTimeFormat(_NowCalc(), 5) , ":", "-" )

   ConsoleWrite ( $FileName & @CRLF )

   $Variable = 6
   $File = FileOpen($FileName, 1)
   FileWriteLine($File, "This is a test to see if I can write lines and variables to a text file... " & $Variable & @CRLF )

   FileClose($File)

 

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...