How speedup this code?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Terka
Posts: 157
Joined: 05 Nov 2015, 04:59

How speedup this code?

Post by Terka » 11 Dec 2015, 17:06

what it does:
2 drives
o: (original), b: (backup)

at the beginning the o: and b: are exactly the same

have large files (eg. home videos) on o: and im moving them to different directories on o: only.
That means, all files on o: are still backuped on b:, but they are located in wrong folders.

I want to make o: and b: exactly the same again, by MOVING files on b: to proper folders.
Dont want to copy anything from original disk to backup one, because copying takes looong time.
Want only move files inside the b: drive to proper directory, because moving on SAME disk is fast.

Code: Select all

list1:="c:\list1"
list2:="c:\list2"
;lists examples:
;-       ___list1:___                                                                ___list2:___
;- index.php201410200857464782|i:\index.php                                 index.php201410200857464782|j:\index.php
;- project.properties20140815152635141|i:\root\project.properties           project.properties20140815152635141|j:\project.properties
;- project.xml20140815152635322|i:\root\project.xml                         project.xml20140815152635322|j:\root\project.xml
;- soubor.ke.kopirovani.txt201410201105378|i:\soubor.ke.kopirovani.txt      soubor.ke.kopirovani.txt201410201105378|j:\root\soubor.ke.kopirovani.txt



Loop, read, %list1%
{
  i=%A_LoopReadLine%


      Loop, read, %list2%
      {
        j=%A_LoopReadLine%
        ; i is Source,  j is Destination
        StringSplit, arrayS, i,|
        StringSplit, arrayD, j,|
            if ( arrayS1 = arrayD1) { ;if same files
              Source = %arrayD2%
              Dest:= RegExReplace(arrayS2, ".(.+)", "j$1")  ; arrayS2 but another drive
              FileMove, %Source%, %Dest%, 0  ;-(1 = overwrite)
              ;todo: remove line from both lists-will this help?
              break
            }
      }
}

just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How speedup this code?

Post by just me » 12 Dec 2015, 02:01

1. How do you get the list files?
2. Are all file names unique?

Terka
Posts: 157
Joined: 05 Nov 2015, 04:59

Re: How speedup this code?

Post by Terka » 12 Dec 2015, 02:59

Code: Select all

 loop, %inputPath% , 0 , 1
                                                {
                                                  FileAppend, %A_LoopFileName%%A_LoopFileTimeModified%%A_LoopFileSize%|%A_LoopFileFullPath% `n, %file%
                                                }

                                            ; sort list
                                                FileRead, Contents, %file%
                                                if not ErrorLevel  ; Successfully loaded.
                                                {
                                                    Sort, Contents, C  ;case sensitive
                                                    FileDelete, %file%
                                                    FileAppend, %Contents%, %outputFile%
                                                    Contents =  ; Free the memory.
                                                }
the names (lines in list) are not uniq

just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How speedup this code?

Post by just me » 12 Dec 2015, 04:13

I mean the pure file names without the path.

Terka
Posts: 157
Joined: 05 Nov 2015, 04:59

Re: How speedup this code?

Post by Terka » 12 Dec 2015, 14:56

loop, %inputPath% , 0 , 1
{
FileAppend, %A_LoopFileName%%A_LoopFileTimeModified%%A_LoopFileSize%|%A_LoopFileFullPath% `n, %file%
}

just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How speedup this code?

Post by just me » 12 Dec 2015, 16:02

Once again, are the file names in A_LoopFileName unique?

User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: How speedup this code?

Post by AlphaBravo » 13 Dec 2015, 00:31

loop list1 lines plus list2 lines instead of looping list1 lines times list2 lines

Code: Select all

FileRead, List1, c:\list1
FileRead, List2, c:\list2
objS := []

loop, parse, list1, `n, `r
{
	S := StrSplit(A_LoopField, "|")
	objS[S.1] := RegExReplace(S.2, "^.", "j")
}

loop, parse, list2, `n, `r
{
	D := StrSplit(A_LoopField, "|")
	if objS[D.1]
	{
		Source := D.2
		Dest := objS[D.1]
		if (Source <> Dest)
			FileMove, % Source , % Dest, 0
	}
}
return

Terka
Posts: 157
Joined: 05 Nov 2015, 04:59

Re: How speedup this code?

Post by Terka » 13 Dec 2015, 13:01

the names are not uniq

Terka
Posts: 157
Joined: 05 Nov 2015, 04:59

Re: How speedup this code?

Post by Terka » 14 Dec 2015, 16:06

AlphaBravo, your code does not compare all the possibilities as mine does.
tested.

Terka
Posts: 157
Joined: 05 Nov 2015, 04:59

Re: How speedup this code?

Post by Terka » 14 Dec 2015, 18:46

once again - comparing
m*n values
but
if m=n
i can remove n from the "nlist" so the inner cycle does have n-1 values when running it next time
but how to put the algorithm in praxis?

just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How speedup this code?

Post by just me » 15 Dec 2015, 04:57

Well, after rereading your original code I think the main problem is that you perform the FileMove command for all identical files even if their pathes are equal besides the drive.

I suggest the following changes:
  • Ensure that

    Code: Select all

    #NoEnv
    SetBatchLines, -1
    are included at the top of your script to run with maximum speed.
  • Remove the drive from the file path in both lists, because it is constant.
  • To reduce file system access read the whole files only once and replace the file-reading loops with parsing loops.
  • Check whether the pathes are different to prevent needless FileMove action.

Code: Select all

#NoEnv
SetBatchLines, -1

; S: source drive, D: backup drive
driveS := "i:"
driveD := "j:"

listS := "c:\list1"
listD := "c:\list2"

;lists examples:
;-       ___list1:___                                                                ___list2:___
;- index.php201410200857464782|\index.php                                 index.php201410200857464782|\index.php
;- project.properties20140815152635141|\root\project.properties           project.properties20140815152635141|\project.properties
;- project.xml20140815152635322|\root\project.xml                         project.xml20140815152635322|\root\project.xml
;- soubor.ke.kopirovani.txt201410201105378|\soubor.ke.kopirovani.txt      soubor.ke.kopirovani.txt201410201105378|\root\soubor.ke.kopirovani.txt

FileRead, contentS, %listS%
FileRead, contentD, %listD%

Loop, Parse, contentS, `n, `r
{
   StringSplit, arrayS, A_LoopField, |
   Loop, Parse, contentD, `n, `r
   {
      StringSplit, arrayD, A_LoopField, |
      If (arrayS1 = arrayD1) { ; if same files
         If (arrayS2 <> arrayD2) { ; if different pathes
            FileMove, % driveD . arrayD2, % driveD . arrayS2, 0  ;-(1 = overwrite)
            Break
         }
      }
   }
}
*not tested*

Terka
Posts: 157
Joined: 05 Nov 2015, 04:59

Re: How speedup this code?

Post by Terka » 30 Nov 2022, 06:41

Please how can the contentD be shorten after each found record?

Post Reply

Return to “Ask for Help (v1)”