Jump to content

FileCopy will not copy a file when using variables...


Recommended Posts

Hello Experts,

I am trying to create a script that will go onto my network and copy a word document to a local folder then open it. When I hard code the FileCopy, it works perfectly, but when I use variables for file path and file name, only the local folder I need gets created. I cannot seem to get the file to open even when using the hard coded path in function FileOpen code. Here is my code with comments on what is happening.

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

$ProductName = "Microsoft Word 2013"
$AbvLanguage = "ENG"

$Network = "mynetwork"
$Folder = "shared"
$SampleDocs = "Sample Documents"

$DocumentName = "Holiday Schedule.docx"

;Or
$FileLocation = "mynetwork\shared\Sample Documents"

 

SampleDocument ()
Func SampleDocument()


;This commented out hard coded line of code below works and does the copy, create local folder, and paste
;FileCopy ("\\mynetwork\shared\Sample Documents\Microsoft Word 2013\ENG\Holiday Schedule.docx","c:\Sample Documents\",8)

;The opening of FileOpen line of code below has not worked yet, even when the file copy works
FileOpen ("C:\Sample Documents\Holiday Schedule.docx")

 

;Here are the 2 versions of FileCopy code(I only need one, one that works) and 1 version of FileOpen code I want to use with variables that has not work, ever

;FileCopy ("\\$Network\$Folder\$SampleDocs\$ProductName\$AbvLanguage\$DocumentName","c:\Sample Documents\",8)
;FileCopy ("\\$FileLocation\$ProductName\$AbvLanguage\$DocumentName","c:\Sample Documents\",8)
;FileOpen ("c:\$SampleDocs\$DocumentName")

EndFunc

What am I missing here? Is this as simple as you cannot use variables to create file paths in the FileCopy function?

Link to comment
Share on other sites

You must concatenate strings and variables, like this :

FileCopy ("\\" & $Network & "\" & $Folder & "\" & $SampleDocs & "\" & $ProductName & "\" & $AbvLanguage & "\" & $DocumentName,"c:\Sample Documents\",8)

 

Link to comment
Share on other sites

I followed your suggestion but it is telling me its an Unterminated String. I feel like there should be a quote mark after DocumentName but that doesnt fix it either. 

When I run the following;

FileCopy ("\\" & $Network & "\" & $Folder & "\" & $SampleDocs & "\" & $ProductName & "\" & $AbvLanguage & "\" $DocumentName,"c:\Sample Documents",8)

I get the Error: Badly Formated Variable Or Macro

I did use the code where network path was a variable and that worked. Dont know what the difference was but it worked.

FileCopy ("\\" & $FileLocation & "\" & $ProductName & "\" & $AbvLanguage & "\" & $DocumentName,"c:\Sample Documents\",8)

 

Maybe there is a limit to the amount of variables allowed in a string, though I doubt that.

Edited by Anthony Padgett
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

×
×
  • Create New...