Page 1 of 1

FileCopy not working

Posted: 20 Oct 2019, 09:28
by Quisquose
I'm trying to do a very simple file copy, but nothing is happening.

I want to copy a file (that has no filename extension) to the same folder as the original but with a new name.
In other words: copy Bookmarks (bak) to Bookmarks (so that there are now 2 copies of the file instead of one).

Code: Select all

FileCopy,"C:\Program Files\Chrome\User Data\Default\Bookmarks (bak)", "C:\Program Files\Chrome\User Data\Default\Bookmarks"
I don't know why such a simple command doesn't work.

If I throw in run, calc.exe on the next line (as a test) Calculator does open, so the script is being run ok, it's just the FileCopy command itself that has some kind of problem, but I don't know what it is.

Re: FileCopy not working

Posted: 20 Oct 2019, 11:09
by Quisquose
Arrggh! .... even a straightforward FileDelete does not work.

FileDelete, "C:\test.txt"

How can something so simple go wrong? What am I missing?

Re: FileCopy not working

Posted: 20 Oct 2019, 11:19
by Spark
Try Without "

Code: Select all

FileDelete, C:\test.txt

Re: FileCopy not working  Topic is solved

Posted: 20 Oct 2019, 12:07
by Odlanir
Put a % sign before your strings:

Code: Select all

FileDelete, % "C:\test.txt"

Re: FileCopy not working

Posted: 22 Oct 2019, 00:18
by Quisquose
Thanks Odlanir.

I was pulling my hair out trying to understand why nothing would work. It was driving me nuts.

The commands all just failed silently with no error message, so I had no idea where to even start looking for a solution.

Putting % before strings is not mentioned in the help file under the sections for Move, Copy, Delete etc. In fact none of the examples they show have % included.

Re: FileCopy not working

Posted: 22 Oct 2019, 00:22
by boiler
That's because they are using legacy syntax rather than expression syntax, so you would use it without quotes like Spark showed you.

Re: FileCopy not working

Posted: 22 Oct 2019, 00:41
by Quisquose
Even with spaces in the path?

Obviously the test example I gave is not the real path

Re: FileCopy not working

Posted: 22 Oct 2019, 00:48
by boiler
Yes, even with spaces. A parameter in legacy syntax format will consider everything up to the comma or end of the line (generally) to be part of the string of text for that parameter. You don't use quotes in or around it unless the quotes are actually intended to be part of the string. It's not like in a command window where you need to put quotes around it for it to recognize that it's a continuous string.

What Odlanir showed you is using a single % to force expression syntax. It can make it easier to learn AHK by always sticking with expression syntax even though you have to "force" it in command parameter situations. Parameters in function calls are always in expression format, btw -- just to confuse you. :D

Re: FileCopy not working

Posted: 22 Oct 2019, 00:58
by Quisquose
Thank you for the explanation.

What about in instances where I want to use something like: A_WinDir or A_AppData in the path?

FileDelete, % "A_AppData\Prog\file.txt" does not work
nor does:
FileDelete, "A_AppData\Prog\file.txt"
nor does:
FileDelete, A_AppData\Prog\file.txt

Re: FileCopy not working

Posted: 22 Oct 2019, 01:03
by boiler
You need to put only the literal string part inside the quotes and concatenate it with the variable like this:
FileDelete, % A_AppData "\Prog\file.txt"
or
FileDelete, % A_AppData . "\Prog\file.txt"

In legacy format:
FileDelete, %A_AppData%\Prog\file.txt

Re: FileCopy not working

Posted: 22 Oct 2019, 01:19
by Quisquose
Thanks again. It is now working.

I just looked up Legacy Syntax vs Expression in the AutoHotkey Help file and ....... OH ..... MY ..... GOD! :crazy:

What a minefield!

I'm glad that I had your shorthand explanation above.

Re: FileCopy not working

Posted: 22 Oct 2019, 01:22
by boiler
You're welcome. Stick with it. It will eventually become intuitive.