Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

FileCopyDir, FileMoveDir ... a newbie problem



  • Please log in to reply
6 replies to this topic
Zvonko
  • Members
  • 173 posts
  • Last active: Jan 24 2016 05:21 PM
  • Joined: 17 Oct 2014

I am trying to solve this problem since hours, and it seems to be a problem which could occupy a newbie like me for days. So I hope that somebody will help ...

 

I am using FileCopyDir and FileMoveDir in a GuiDropFiles part of a script in totally identical context, to copy or to move a selected folder to a target folder, but the result is completely different:

 

With FileMoveDir the selected FOLDER (as folder, with its whole content) is moved to the target folder,

with FileCopyDir only the CONTENT of the selected folder is copied to the target folder.

 

Example:

 

SelectedFolder = C:\SelFolder

contained file: C:\SelFolder\File.txt

 

TargetFolder for CopyDir = C:\Copied

TargetFolder for MoveDir = C:\Moved

 

After executing FileMoveDir

there is a file

C:\Moved\SelFolder\File.txt

 

After executing FileCopyDir

there is a file

C:\Copied\File.txt

 

What am I doing wrong???

 

 

Here my script (the GuiDropFile part):

MyGuiGuiDropFiles:

If Desired = copy
	{
	   Loop, parse, A_GuiEvent, `n
	     {
		ItemIsFol := InStr(FileExist(A_LoopField), "D")
		   If (ItemIsFol > 0)
			FileCopyDir %A_LoopField%, %TargetDir%, 1
		   else
	      		FileCopy %A_LoopField%, %TargetDir%, 1
	     }
	   return
	}
If Desired = move
	{
	   Loop, parse, A_GuiEvent, `n
	     {
		ItemIsFol := InStr(FileExist(A_LoopField), "D")
		   If (ItemIsFol > 0)
			FileMoveDir %A_LoopField%, %TargetDir%, 1
		   else
	      		FileMove %A_LoopField%, %TargetDir%, 1
	     }
	   return
	}
return




Zvonko
  • Members
  • 173 posts
  • Last active: Jan 24 2016 05:21 PM
  • Joined: 17 Oct 2014

Here a script which demonstrates the different behavior of FileCopyDir and FileMoveDir.

 

The result on my computer is as described above.

 

I studied the ahk documentation referring to FileCopyDir and FileMoveDir to understand this difference, but I have not found an explanation. Of course I know that this must be a misunderstanding of me.

TargetCo = C:\Copied
TargetMO = C:\Moved
IfNotExist %TargetCo%
   FileCreateDir, %TargetCo%
IfNotExist %TargetMo%
   FileCreateDir, %TargetMo%
SelFolder = C:\SelFolder
IfNotExist %SelFolder%
   FileCreateDir, %SelFolder%
File = %SelFolder%\File.txt
FileAppend, , %File%

RunWait C:

Gui Cop: -dpiscale
Gui Cop: Add, Text, w420, Select the folder C:\SelFolder and drop it first into this gui.
Gui Cop: Show, x50 y50 w450 h130, [1] Copy to C:\Copied 

Gui Mov: -dpiscale
Gui Mov: Add, Text, w420, Select then folder C:\SelFolder and drop it into this gui.
Gui Mov: Show, x50 y250 w450 h130, [2] Move to C:\Moved

Gui Res: -dpiscale
Gui Res: Add, Text, w420, Check now the content of C:\Copied and C:\Moved.
Gui Res: Show, x50 y450 w450 h130, [3] Check results

WinActivate, [1]

return

CopGuiDropFiles:
	   Loop, parse, A_GuiEvent, `n
	     {
		ItemIsFol := InStr(FileExist(A_LoopField), "D")
		   If (ItemIsFol > 0)
			FileCopyDir %A_LoopField%, %TargetCo%, 1
		   else
	      		FileCopy %A_LoopField%, %TargetCo%, 1
	     }
WinActivate, [2]
return


MovGuiDropFiles:
	   Loop, parse, A_GuiEvent, `n
	     {
		ItemIsFol := InStr(FileExist(A_LoopField), "D")
		   If (ItemIsFol > 0)
			FileMoveDir %A_LoopField%, %TargetMo%, 1
		   else
	      		FileMove %A_LoopField%, %TargetMo%, 1
	     }
WinActivate, [3]
return


CopGuiClose:
MovGuiClose:
ResGuiClose:
FileRemoveDir, %TargetCo%, 1
FileRemoveDir, %TargetMo%, 1
ExitApp
return


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
It's explained in the Flag parameter of FileMoveDir.

Known limitation: If Dest already exists as a folder and it is on the same volume as Source, Source will be moved into it rather than overwriting it. To avoid this, see the next option.



Zvonko
  • Members
  • 173 posts
  • Last active: Jan 24 2016 05:21 PM
  • Joined: 17 Oct 2014

Thank you very much, Lexikos!

 

You never lose patience with all such newbies like me???

 

My misunderstanding was that I even expected the result of FileCopyDir would be analogous to the result od FileCopy:

- FileCopyDir C:\SelFolder C:\Copied, 1

would result in

- C:\Copied\SelFolder                  ... with C:\Copied\SelFolder\File.txt

because

- FileCopy C:\File.doc C:\Copied, 1

results in

- C:\Copied\File.doc

 

So much naivety in a heap!

 

I suppose the solution in my new demo script below is correct (lines with !!!). Or could this even be simplified?

TargetCo = C:\Copied
TargetMO = C:\Moved
IfNotExist %TargetCo%
   FileCreateDir, %TargetCo%
IfNotExist %TargetMo%
   FileCreateDir, %TargetMo%
SelFolder = C:\SelFolder
IfNotExist %SelFolder%
   FileCreateDir, %SelFolder%
File = %SelFolder%\File.txt
FileAppend, , %File%

RunWait C:

Gui Cop: -dpiscale
Gui Cop: Add, Text, w420, Select the folder C:\SelFolder and drop it first into this gui.
Gui Cop: Show, x50 y50 w450 h130, [1] Copy to C:\Copied 

Gui Mov: -dpiscale
Gui Mov: Add, Text, w420, Select then folder C:\SelFolder and drop it into this gui.
Gui Mov: Show, x50 y250 w450 h130, [2] Move to C:\Moved

Gui Res: -dpiscale
Gui Res: Add, Text, w420, Check now the content of C:\Copied and C:\Moved.
Gui Res: Show, x50 y450 w450 h130, [3] Check results

WinActivate, [1]

return

CopGuiDropFiles:
	   Loop, parse, A_GuiEvent, `n
	     {
		ItemIsFol := InStr(FileExist(A_LoopField), "D")
		   If (ItemIsFol > 0)
		      {
			StringGetPos, Pos, A_LoopField, \, R1			; !!!
			StringTrimLeft, FolName, A_LoopField, Pos + 1		; !!!
			FileCopyDir %A_LoopField%, %TargetCo%\%FolName%, 1	; !!!
		      }
		   else
	      		FileCopy %A_LoopField%, %TargetCo%, 1
	     }
WinActivate, [2]
return


MovGuiDropFiles:
	   Loop, parse, A_GuiEvent, `n
	     {
		ItemIsFol := InStr(FileExist(A_LoopField), "D")
		   If (ItemIsFol > 0)
		      {
			StringGetPos, Pos, A_LoopField, \, R1			; !!!
			StringTrimLeft, FolName, A_LoopField, Pos + 1		; !!!
			FileMoveDir %A_LoopField%, %TargetMo%\%FolName%, 2	; !!!
		      }
		   else
	      		FileMove %A_LoopField%, %TargetMo%, 1
	     }
WinActivate, [3]
return


CopGuiClose:
MovGuiClose:
ResGuiClose:
FileRemoveDir, %TargetCo%, 1
FileRemoveDir, %TargetMo%, 1
ExitApp
return


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
✓  Best Answer
You can use SplitPath instead of StringGetPos/StringTrimLeft. Or InStr/SubStr.

MarcAntony
  • Members
  • 1 posts
  • Last active: Nov 05 2015 01:55 AM
  • Joined: 08 Jul 2014

I apologies in advance as I am asking something not related to the topic as I do not know how and where to ask.

I use autohotkey for many hotkeys and string replacement for quit some time but my expertise level is novice only.

I have found that the autohotkey goes to sleep (on one of my three computers) and I need to reload script to be able to use hotkeys. Sometimes Autohotkey exits of its own and I have to run it again. I have not made executable of my script as I make change in my script file very frequently. I have put exception for the autohotkey in the anti virus program. I am not able to understand where to look for answer as the same script and program works fine on my other computers. Thanking you in advance. -MA



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

New questions should be directed to autohotkey.com/boards/

 

And that is an interesting issue.

 

AHK should only exit on its own if it

1) Lacks hotkeys and #Persistent; may exit early by reaching an Exit command

2) Encounters an ExitApp

 

Or, maybe something else I'm forgetting, but those are the two I know for exiting an AHK script.

 

It would be good if you can share on the other forums what your code is. It would be the best if you can provide as short of a script as you can - still a full script - that experiences the issue you're talking about. Additionally, you should report on the version of AHK that you have (MsgBox % A_AhkVersion) and on the Operating System you are running. Especially so if there is a difference in either of those amongst your three computers.