Passing Loop Variable Result to File Copy Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sethjbr
Posts: 11
Joined: 14 Feb 2017, 16:39

Passing Loop Variable Result to File Copy

22 Sep 2017, 09:18

Greetings Autohotkey Community,

I don't think this will take long, but I would like assistance understanding why I can successively pass a variable's answer from a input box to a filecopy command, but when I try doing the same thing with a find loop that was shared by the user "corrupt" https://autohotkey.com/board/topic/2712 ... -its-path/ to a file copy command, the command is not successful and doesn't show any errors.

Would you please assist?

Code that works by passing %UserInput% variable to the file copy command

Code: Select all

InputBox, UserInput, Please enter a file path., , 50, 400
if ErrorLevel
    MsgBox, CANCEL was pressed.
else
    MsgBox, You entered %UserInput%

FileCopy, C:\Users\User\Desktop\copyme.txt, %UserInput%, 1

return
Code that doesn't work using a find loop to locate the file path of a particular file, pass the file path to a variable called %found%, and then attempt to pass that variable result to the FileCopy command.

Code: Select all

directory = C:\users\bryan\Desktop\testdir
search = findme.txt
found = 

loop, %directory%\*.*, 1, 1 ; search the library for the file to get the full path 
{ 
  if A_LoopFileName = %search% 
  {    
   found = %A_LoopFileFullPath%
  } 
}

if found =
{
MsgBox, File not found
}
else
{
MsgBox, Found here: %found%
}

FileCopy, C:\Users\User\Desktop\copyme.txt, %found%, 1

ErrorCount := ErrorLevel

MsgBox, Errors reported: %ErrorCount%

return
Can anyone explain to me why this second selection of code doesn't work?

Thank you, I really appreciate it.
sethjbr
Posts: 11
Joined: 14 Feb 2017, 16:39

Re: Passing Loop Variable Result to File Copy

27 Sep 2017, 11:39

Has anyone tried reproducing this issue, or is this a bug with autohotkey?
User avatar
KuroiLight
Posts: 327
Joined: 12 Apr 2015, 20:24
Contact:

Re: Passing Loop Variable Result to File Copy

27 Sep 2017, 11:46

maybe if A_LoopFileName = %search% -> if A_LoopFileName == %search%

try

Code: Select all

directory := "C:\users\bryan\Desktop\testdir"
search := "findme.txt"
found := ""

Loop, File, %directory%\* {
    if(A_LoopFileName == search) {
        found := %A_LoopFileFullPath%
    }
}

if(found) {
    MsgBox, % "Found Here: " . found
} else {
    MsgBox, % "Not Found"
}

FileCopy, C:\Users\User\Desktop\copyme.txt, %found%, 1
if(ErrorLevel)
    ErrorCount++

MsgBox, % "ErrorCount: " . ErrorCount
Unless you are sure of its usage avoid using = and stick with := for assignment, and == for comparison.
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
[/color]
Guest

Re: Passing Loop Variable Result to File Copy

28 Sep 2017, 13:57

KuroiLight,

Thank you for your assistance. I have looked up proper usage of = and := and == and tested out your code suggestion. When I ran your code, it generated an error about a } being in the wrong place. I can't make the script find the file without using the = sign for assignment. I then spent the next several hours troubleshooting and building some more test code that could be ran on any Windows system, provided the appropriate permissions exist, that demonstrates the issue.

This code below creates a folder under the root of C:\ called File_Copy_Test and then creates a sub-folder called CopyLocation. Then it creates a txt file in File_Copy_Test and generates an input box with the default entry of C:\File_Copy_Test\CopyLocation. Then, this code copies copyme.txt to the C:\File_Copy_Test\CopyLocation location and opens explorer showing the test was successful. Finally, it deletes the directory C:\File_Copy_Test and ends the script.

This code works beautifully. :) Feel free to test it out. :D

Code: Select all

FileCreateDir, C:\File_Copy_Test

FileCreateDir, C:\File_Copy_Test\CopyLocation

FileAppend, This txt file will be moved to test this script.`n, C:\File_Copy_Test\copyme.txt

InputBox, UserInput, File Path Entry, Please input a file path., , 400,100,,,,,C:\File_Copy_Test\CopyLocation

if ErrorLevel
    MsgBox, CANCEL was pressed.
else
    MsgBox, You entered %UserInput%

FileCopy, C:\File_Copy_Test\copyme.txt, %UserInput%, 1

MsgBox, Windows Explorer will now open to %UserInput% and show copyme.txt after being moved to the new location.

explorerpath:= "explorer /e," "C:\File_Copy_Test\CopyLocation"
Run, %explorerpath%

Sleep, 1000

MsgBox, After clicking on OK, the files and directories used for this script will be deleted.

    FileRemoveDir, C:\File_Copy_Test, 1


Sleep, 333

return
However, I am looking to have the file path found by searching for it, and then using that file path to copy a file rather than asking for the file path using an input box. The following code does virtually the same steps as the previous code, only the code that generates the input box is substituted with code that will find the file path. However, with this code, the file copy command doesn't work even though the correct file path is found for the copyme.txt file.

Code: Select all

FileCreateDir, C:\File_Copy_Test

FileCreateDir, C:\File_Copy_Test\CopyLocation

FileAppend, This txt file will be moved to test this script.`n, C:\File_Copy_Test\copyme.txt

directory = C:\File_Copy_Test
copy_directory = C:\File_Copy_Test\CopyLocation
search = copyme.txt
found = 

loop, %directory%\*.*, 1, 1 ; search the library for the file to get the full path 
{ 
  if A_LoopFileName = %search% 
  {    
   found = %A_LoopFileFullPath%
  } 
}

if found =
{
MsgBox, File not found
}
else
{
MsgBox, Found here: %found% and this file will now be copied to %copy_directory%

FileCopy, C:\File_Copy_Test\copyme.txt, %found%, 1

Sleep, 1000

explorerpath:= "explorer /e," "C:\File_Copy_Test\CopyLocation"
Run, %explorerpath%

Sleep, 1000

MsgBox, After clicking on OK, the files and directories used for this script will be deleted.

FileRemoveDir, C:\File_Copy_Test, 1

}

Sleep, 333

return
Does that make sense? Again, please feel free to try both code segments on your system.
User avatar
KuroiLight
Posts: 327
Joined: 12 Apr 2015, 20:24
Contact:

Re: Passing Loop Variable Result to File Copy  Topic is solved

28 Sep 2017, 17:41

Sorry heres the corrected code from my post.

Code: Select all

directory := "C:\users\bryan\Desktop\testdir"
search := "findme.txt"
found := ""

Loop, Files, %directory%\*
{
    if(A_LoopFileName == search) {
        found := %A_LoopFileFullPath%
    }
}

if(found) {
    MsgBox, % "Found Here: " . found
} else {
    MsgBox, % "Not Found"
}

FileCopy, C:\Users\User\Desktop\copyme.txt, %found%, 1
if(ErrorLevel)
    ErrorCount++

MsgBox, % "ErrorCount: " . ErrorCount
But the problem with you 2nd code is that you are FileCopying copyme.txt back to itself,
FileCopy, <src>, <dest>; you had it FileCopy, C:\File_Copy_Test\copyme.txt, %found%, 1 where found == "C:\File_Copy_Test\copyme.txt"

here

Code: Select all

FileCreateDir, % "C:\File_Copy_Test"
FileCreateDir, % "C:\File_Copy_Test\CopyLocation"
FileAppend, % "This txt file will be moved to test this script.`n", % "C:\File_Copy_Test\copyme.txt"

directory := "C:\File_Copy_Test"
copy_directory := "C:\File_Copy_Test\CopyLocation"
search := "copyme.txt"
found := "" 

loop, %directory%\*.*, 1, 1 ; search the library for the file to get the full path 
{ 
  if (A_LoopFileName == search) {
    found := A_LoopFileFullPath
  }
}

if (!found) {
    MsgBox, % "File not found."
} else {
    MsgBox, % "Found here: " . found . " and this file will now be copied to " . copy_directory
    FileCopy, %found%, %copy_directory%, 1
    Sleep, 1000
    explorerpath := "explorer C:\File_Copy_Test\CopyLocation"
    Run, %explorerpath%
    Sleep, 1000
    MsgBox, % "After clicking on OK, the files and directories used for this script will be deleted."
    FileRemoveDir, C:\File_Copy_Test, 1
}

Sleep, 333

return
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
[/color]
sethjbr
Posts: 11
Joined: 14 Feb 2017, 16:39

Re: Passing Loop Variable Result to File Copy

29 Sep 2017, 09:05

KuroiLight,

Thank you for your assistance. Your corrected code works beautifully. Now, I wish I understood your corrections.

1. What is the reason for adding % signs in the first three arguments that create the directories and the files? Is there a autohotkey article that would explain why those are good practice when my first code snippet above in my second code, works fine without those % signs?

Sorry, I am fairly new to understanding the autohotkey code syntax, but I am willing to learn! :)
User avatar
KuroiLight
Posts: 327
Joined: 12 Apr 2015, 20:24
Contact:

Re: Passing Loop Variable Result to File Copy

29 Sep 2017, 11:34

Mostly personal preference
I wouldn't say its good/bad practice I mostly do it for code highlighting and to make intent and strings clearer (for me).
in this example its obvious at glance what is and is not a string

Code: Select all

Msgbox, % "true or false"
where as here

Code: Select all

Msgbox, true or false
both have the same meaning, but 1 is clearer as to your intent.

The only real issue with your code was the FileCopy line.
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
[/color]

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Lamron750 and 242 guests