Jump to content

Help with Filewrite please.


Go to solution Solved by Subz,

Recommended Posts

Hello would someone be able to help me with my code. I am trying to write the clipboards contents to a text file. I have read the documentation but can't seem to egt it to work.


   $file1 = "C:\Users\Home\Desktop\Documents\test.txt"

   Local $Savetext = FileOpen($file1, 1)
   FileWrite($Savetext, ClipGet() & @CRLF)
   FileClose($Savetext)

Link to post
Share on other sites

Have you tried FileOpen($file1, 9) ;~ Creating the file path if it doesn't exist?

Otherwise it should work fine, as long as you've saved data to the clipboard.

Link to post
Share on other sites
  • Developers
7 minutes ago, Subz said:

Otherwise it should work fine, as long as you've saved data to the clipboard.

As long as it is text!

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites
3 minutes ago, Jos said:

As long as it is text!

Why must it be text? Wouldn't anything in the clipboard get copied to the text file?

I tried changing the FileOpen($file1, 8) but does not seem to create the filepath.

Link to post
Share on other sites
Quote

Why must it be text? Wouldn't anything in the clipboard get copied to the text file?

Not all data copied to clipboard is in text format, example copying an image to clipboard.

Quote

I tried changing the FileOpen($file1, 8)

Did you try using flag 9?  Flag 8 would try to open an existing file (read-only mode).

 

Link to post
Share on other sites
  • Developers
23 minutes ago, RyeStick said:

Why must it be text? Wouldn't anything in the clipboard get copied to the text file?

Yes,No!

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites
16 minutes ago, Subz said:

Not all data copied to clipboard is in text format, example copying an image to clipboard.

Did you try using flag 9?  Flag 8 would try to open an existing file (read-only mode).

 

Yes the I tried 9 also, The documentation says it is number 8.

I have added the following

$file1 = "C:\Users\Home\Desktop\Documents\test.txt"



   Local $Savetext = FileOpen($file1, 8)

   ; Prove it exists
   If FileExists($Savetext) Then
    MsgBox($MB_SYSTEMMODAL, "File", "Exists")
   Else
    MsgBox($MB_SYSTEMMODAL, "File", "Does not exist")
   EndIf


   FileWrite($Savetext, ClipGet() & @CRLF)
   FileClose($Savetext)

MsgBox says the file does not exist.

30 minutes ago, JockoDundee said:

what do you mean?

1) no file is created

2) file created with unexpected data?

 

Both. I can't create the file and if I create the file manually and then attempt to amend/write over it with the  clipboards contents then does it not work.

Link to post
Share on other sites
  • Solution

Flag 8 creates the folder path, you need to be combine it with either:

  1. Append text to file
  2. Overwrite existing file

This could also be a result of not having rights to create the folder, in which case you could add #RequireAdmin to the top of your script, here is a basic example which works for me:

;~ Place some text into clipboard
ClipPut("Clipboard example text")
;~ Path to file
Local $sFileName = @ScriptDir & "\Documents\test123.txt"
;~ Create folder path + open in write mode, appending to file
Local $hFileName = FileOpen($sFileName, 9)
    ;~ Write clipboard to the file
    FileWrite($hFileName, ClipGet() & @CRLF)
FileClose($hFileName)
;~ Open the file in your preferred text editor.
ShellExecute($sFileName)

 

Link to post
Share on other sites
25 minutes ago, Subz said:

Flag 8 creates the folder path, you need to be combine it with either:

  1. Append text to file
  2. Overwrite existing file

This could also be a result of not having rights to create the folder, in which case you could add #RequireAdmin to the top of your script, here is a basic example which works for me:

;~ Place some text into clipboard
ClipPut("Clipboard example text")
;~ Path to file
Local $sFileName = @ScriptDir & "\Documents\test123.txt"
;~ Create folder path + open in write mode, appending to file
Local $hFileName = FileOpen($sFileName, 9)
    ;~ Write clipboard to the file
    FileWrite($hFileName, ClipGet() & @CRLF)
FileClose($hFileName)
;~ Open the file in your preferred text editor.
ShellExecute($sFileName)

 

Your example works in creating a filepath, does it copy ClipGet() contents for you? as it opens an emtpy black notepad for me.

Link to post
Share on other sites
26 minutes ago, Subz said:

Yes, I tested the code before posting it.  Did you use the code above or did you modify it?

Wow, I just tried this on a different computer and it works. I have spent hours trying to get this to work. What do you think the problem may be?I am going to try a reinstall and get back to you.

Link to post
Share on other sites
47 minutes ago, RyeStick said:

Wow, I just tried this on a different computer and it works. I have spent hours trying to get this to work. What do you think the problem may be?I am going to try a reinstall and get back to you.

The Reinstall has not fixed the problem? do you think my register could be corrupted or something, how do you think I can fix the problem?

Link to post
Share on other sites
8 hours ago, RyeStick said:

The documentation says it is number 8

No it doesn't. It says "$FO_CREATEPATH (8) = Create directory structure if it doesn't exist (See Remarks)."

You actually need, $FO_APPEND (1) = Write mode (append to end of file) OR  $FO_OVERWRITE (2) = Write mode (erase previous contents)

So, your arg should be 8 + 1 = 9 OR 8 + 2 = 10 depending on whether you want to extend the file or clear it every time.

6 hours ago, RyeStick said:

how do you think I can fix the problem?

Add some debug statements to your test code. Initially just try writing the file without messing around with extracting the clipboard. Once you have the filewrite working then implement the rest of your code. One step at a time.

Phil Seakins

Link to post
Share on other sites
7 hours ago, RyeStick said:

Wow, I just tried this on a different computer and it works. I have spent hours trying to get this to work. What do you think the problem may be?

It depends on whether you use clipboard tools, or clipboard cleaning tools (CCleaner), or password tools that clear the clipboard, or ... and maybe also on the Windows version (e.g. Win 10 can synchronize your clipboard between devices).

Link to post
Share on other sites
  • 2 weeks later...

Obtuse comment on this thread: A lot of effort would have been saved if return codes for each function were tested, or even just written to the console for debugging purposes.

ConsoleWrite, @error, variable values, etc, clauses will help with debugging immensely.

Example, return code for unable to create file, unable to open file in output mode, unable to write from clipboard, etc. You will then be able to see clearly where your code is failing, or even pick up on unanticipated conditions that you can test for and take appropriate action to make your code flexible and robust.

This is just good coding practise. Get used to embedding it, early, is my strong suggestion.

Go on, add the error code results, and see where your code broke, and why. It will be a great learning exercise for all.

Edited by Confuzzled
Link to post
Share on other sites

It's so simple. Just hover your mouse over a variable and type ALT-D. A debug statement will be added to your code automatically.

Where there is no variable eg @error, just type @error on the following line, hover and type ALT-D. Then delete @error.

If you want to know the runtime value of a variable or macro, eg $somestring or @HOUR , that is not nearby, type the variable name or macro on a blank line, hover over it and type ALT-D. Then delete the line containing $somestring or the macro that you just typed.

Edited by pseakins
@error was a bad example for debug display of a variable

Phil Seakins

Link to post
Share on other sites

Just to add further to the discussion, in your code

FileWrite($hFileName, ClipGet() & @CRLF)

don't forget you are actually doing three things (FileWrite, ClipGet and string concatenation), so the error code that is returned may apply to only one of the three functions when you test for it.

In this situation, if this is where your code is inconsistent, you perform each operation separately, using intermediate variables, and test each @ERROR that is returned individually.

Link to post
Share on other sites
13 minutes ago, Confuzzled said:

...so the error code that is returned may apply to only one of the three functions when you test for it...

Actually it would always and only apply to the FileWrite() the way it is written, since ClipGet() is called first.

IMO, and FWIW, inline “string concatenation” never alters @Error. 
 

Code hard, but don’t hard code...

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...