Anthony Padgett

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

6 posts in this topic

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"

$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")


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

Share this post

Link to post
Share on other sites

You must concatenate strings and variables, like this :

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


Share this post

Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post

Link to post
Share on other sites

#5 ·  Posted (edited)

I was able to get both of them to work actually. I just retyped the code. Must be a space or something I cant see in there.


Thank you!


Now how do I get this file to open in its associated program like a double click...

Edited by Anthony Padgett

Share this post

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

  • Similar Content

    • Xulong
      By Xulong
      I have a script which is copying one file (the latest file among a bunch of structured folders) from one network device to another network device.
      The weird thing is the file copied successfully to the destination folder right after the FileCopy command ran but the script is hanging for another few minutes until it return with exit code 0.
      Below is my script:
      #include <MsgBoxConstants.au3> #include <File.au3> #include <Array.au3> #include <FileConstants.au3> copyfile() Func copyfile() Local $testLogFile = FileOpen(@ScriptDir & "\CopyMSI", 1) Local $BuildArtifacts = "\\vm-build\BuildArtifacts" Local $dServer = "\\VM-DEV\c$\Users\Administrator\Desktop" Send("#r") WinWaitActive("Run") ControlSend("Run", "", 1001, $BuildArtifacts) ControlClick("Run", "OK", 1) _FileWriteLog($testLogFile, "Opened Builds folder") ;trying to get the latest build folder and grab the latest msi file for copying WinWaitActive("BuildArtifacts") Local $FolderList = _FileListToArray($BuildArtifacts, "Grand-4.*") _ArraySort($FolderList, 1) Local $latestSEbuild = $FolderList[0] Sleep (5000) WinClose("BuildArtifacts") Local $sServer = $BuildArtifacts & '\' & $latestSEbuild & '\G4.msi' ; start copying file from build to destination folder FileCopy($sServer, $dServer, $FC_OVERWRITE) _FileWriteLog($testLogFile, "Copied MSI") EndFunc  
    • PINTO1927
      By PINTO1927
      Hello guys,
      I'm working on this script:
      Case $BTN Global $URL = FileOpenDialog("IMPORT FILE", $DESKTOP, "ALL FORMAT (*)", 4) $DIR_DEST = "C:\DIR-WORK\list\IMPORT_DOC\" DirCreate($DIR_DEST) Local $LINE For $t = 1 To $URL[0] _FileReadToArray($URL[$t], $LINE) For $u = 1 To $LINE[0] FileCopy($URL[$u], $DIR_DEST) Next Next the selected files via OpenFileDialog must be copied to the folder $DIR_DEST.
    • cheeroke
      By cheeroke
      OK, so i run out of ideas how to make this working. Basically my FIleCOpy statement doesn't do it job. I must be missing something. Here is my code:
      #RequireAdmin . . Local $fTasks = "C:\Drivers\Other\Tasks\" Local $fTasksSys = "C:\2\" . . . FileCopy(GUICtrlRead($fTasks) & "Win10_CNC_Setup", $fTasksSys, 8) MsgBox(0, "Boot Setting", "Copied Tasks." & $fTasks & "Win10_CNC_Setup" & @CR & "To " & $fTasksSys) FileDelete($fTasksSys & "ADxLR5_StartUp") MsgBox(0, "Boot Setting", "Deleted tasks.") while it is finally creating folder called "2" it does not want to copy files. Files are .xml but without extension one of the is called: "Win10_CNC_Setup".
      Another thing is I don't understand why this
      $FC_OVERWRITE + $FC_CREATEPATH doesn't work but if i replace it with this
      8 it does create folder.
      Main question is - how to make it copying files?
      Please help.
    • ur
      By ur
      $RootDir = "C:\x86\" $BinariesFolder = "C:\Binaries\x86\0717-15.Binary.r9642_Release_win32_Fresh" $DestinationFolder = $RootDir & "_AppFiles-Telos" $SourceFolder=$BinariesFolder FileCopy($SourceFolder & "\*.*", $DestinationFolder & "\", $FC_OVERWRITE + $FC_CREATEPATH) In the above code, even if I keep $FC_OVERWRITE, the existing files are not getting replaced.
      Any suggestion please.
    • Wicked_Caty
      By Wicked_Caty
      I'm writing a small program for creating a backup. I want to do that by using FileCopy. Everything from drive C should be copied to drive D.
      FileCopy("C:", "D:", $FC_OVERWRITE + $FC_CREATEPATH) That doesn't do anything, though. I played a bit with the paths, but nothing does what I want. Sometimes only the folder of the program itself is copied, sometimes nothing is copied.
      What am I doing wrong? What do I have to do that everything from C goes to D? Thanks!
      Edit: DirCopy does pretty much the same