FileCopy not working Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Quisquose
Posts: 74
Joined: 24 Jun 2016, 04:16

FileCopy not working

20 Oct 2019, 09:28

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.
Quisquose
Posts: 74
Joined: 24 Jun 2016, 04:16

Re: FileCopy not working

20 Oct 2019, 11:09

Arrggh! .... even a straightforward FileDelete does not work.

FileDelete, "C:\test.txt"

How can something so simple go wrong? What am I missing?
Spark
Posts: 80
Joined: 04 Jan 2017, 02:22

Re: FileCopy not working

20 Oct 2019, 11:19

Try Without "

Code: Select all

FileDelete, C:\test.txt
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: FileCopy not working  Topic is solved

20 Oct 2019, 12:07

Put a % sign before your strings:

Code: Select all

FileDelete, % "C:\test.txt"
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Quisquose
Posts: 74
Joined: 24 Jun 2016, 04:16

Re: FileCopy not working

22 Oct 2019, 00:18

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.
User avatar
boiler
Posts: 16915
Joined: 21 Dec 2014, 02:44

Re: FileCopy not working

22 Oct 2019, 00:22

That's because they are using legacy syntax rather than expression syntax, so you would use it without quotes like Spark showed you.
Quisquose
Posts: 74
Joined: 24 Jun 2016, 04:16

Re: FileCopy not working

22 Oct 2019, 00:41

Even with spaces in the path?

Obviously the test example I gave is not the real path
User avatar
boiler
Posts: 16915
Joined: 21 Dec 2014, 02:44

Re: FileCopy not working

22 Oct 2019, 00:48

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
Quisquose
Posts: 74
Joined: 24 Jun 2016, 04:16

Re: FileCopy not working

22 Oct 2019, 00:58

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
User avatar
boiler
Posts: 16915
Joined: 21 Dec 2014, 02:44

Re: FileCopy not working

22 Oct 2019, 01:03

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
Quisquose
Posts: 74
Joined: 24 Jun 2016, 04:16

Re: FileCopy not working

22 Oct 2019, 01:19

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.
User avatar
boiler
Posts: 16915
Joined: 21 Dec 2014, 02:44

Re: FileCopy not working

22 Oct 2019, 01:22

You're welcome. Stick with it. It will eventually become intuitive.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5, RussF and 390 guests