AutoIt – How to Copy a File’s Contents and Paste Using the Clipboard

In this tutorial I show how to quickly copy and paste large amounts of text into fields of a form after retrieving the data from a database table. This is a recent problem that I wished to resolve since simply sending the variable’s contents into a field takes time if you use the Send() and the contents of the variable contains a large amount of text. Just for your information, to use the Send() with a variable is used as follows:

send($variable, 1)

You need the second parameter (i.e. flag) ‘1’ in the function. The flag ‘1’ means data is sent raw. The default is ‘0’ which is defined as “Text contains special characters like + and ! to indicate SHIFT and ALT key-presses”.

AutoIt: ClipPut(), Placing Contents to Clipboard

However, this really is not the most efficient method to send large bodies of text to, say, notepad or to a form’s fields. Once you retrieve your data and place this data in a variable then all you need to do is the following:

Func printOutput2()

Local $fTest
$fTest = ClipPut($outputArrayRS[0][2]) ;get value of table's field by index number
Run("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("^v")
EndFunc

The ClipPut function places the text within your clipboard. Then all you need to do is Send() the keys Control+v as:

send("^v")

AutoIt: ClipGet(), Get Contents of Clipboard

I attempted to use the ClipGet function; however, this didn’t work for me. ClipGet function does return the value when usiing the MsgBox function. So with MsgBox() this works:

Func printOutput2()

Local $fTest
$fTest = ClipPut($outputArrayRS[0][2]) ;get value of table's field by index number
MsgBox(0, "test2", ClipGet())

Now to execute the function all you need to type is:

printOutput2())

AutoIt: Copy File as in Explorer

If you wish to copy a file as you would in Explorer then use the FileCopy function:

FileCopy("C:file_to_copy.txt", "D:mydirfile_to_copy.txt")

The format to use this function is:

FileCopy ( "source", "dest" [, flag] )

There are a few flags that you can use with the FileCopy function:

[optional] this flag is a combination or one of the following:

0 = (default) do not overwrite existing files

1 = overwrite existing files

8 = Create destination directory structure if it doesn’t exist.

If you wish to combine the flags then add the values together. In other words, if you wish to overwrite an exiting file in the destination and check if the destination directory exists or not and if it does not then create the directory, then would use ‘9’ as the flag.

So the flag would be used as:

FileCopy("C:file_to_copy.txt", "D:mydirfile_to_copy.txt", 9)

You can use wildcards in copying files such as ‘*’ which denotes zero or more characters and ‘?’ which denotes zero or one character. So if you wished to copy all files in the temp directory (i.e. C:temp) and then copy them to another directory you would use the following code:

FileCopy("C:temp*.*", "D:mydir")

Note that you cannot have have more than one wildcard in the filename and extension. So this will NOT work:

FileCopy("C:tempd*g?.*", "D:mydir")

You would need to use regular expressions to accomplish this more complex task.

Another note that applies specifically the extension is matches will be made for the first 3 characters of the extension. In other words, this:

FileCopy("C:temp*.txt", "D:mydir")

will copy all files with the extension ‘txt’ as well any extension as ‘txt*’. So extension ‘txt1’, ‘txt2’, ‘txt3’ will also by copied.

Leave a Reply