| 1 |
; |
|---|
| 2 |
; level |
|---|
| 3 |
; Language: English |
|---|
| 4 |
; Platform: Win XP / Vista tested |
|---|
| 5 |
; Author: S.van Dragt <sander.vandragt@gmail.com> |
|---|
| 6 |
; |
|---|
| 7 |
; Script Function: |
|---|
| 8 |
; Commandline script flatttening a series of folders into a final folder |
|---|
| 9 |
; |
|---|
| 10 |
version = 1.07.04 |
|---|
| 11 |
|
|---|
| 12 |
#NoEnv |
|---|
| 13 |
SendMode Input |
|---|
| 14 |
#NoTrayIcon |
|---|
| 15 |
#ErrorStdOut |
|---|
| 16 |
SetBatchLines, -1 ; Make the operation run at maximum speed. |
|---|
| 17 |
subfolders = 0 |
|---|
| 18 |
individual = n |
|---|
| 19 |
overwrite = n |
|---|
| 20 |
overwriteolderonly = n |
|---|
| 21 |
TotalFilesCopied = 0 |
|---|
| 22 |
|
|---|
| 23 |
FileAppend, Level %version% by Sander van Dragt <sander.vandragt@gmail.com>`n, * |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
; read in parameters |
|---|
| 27 |
; if parameters is not a switch then it's a path |
|---|
| 28 |
If 0 < 2 |
|---|
| 29 |
{ |
|---|
| 30 |
MsgBox,0,Level %version% by Sander van Dragt <sander.vandragt@gmail.com>,Level copies files from several sources into a flat folder. Only works from a command prompt.`n`nUse it to for example to copy all your music from 3 sources into one long list of files, or make sure you have the latest version of all your pictures in one place. `n`nSyntax: level `[-s`,-oo`,-o`] `<source`> `[more source`] `<destination`> |cat`n`t-s = level subfolders`n`t-o = overwrite existing files `(-o or -oo`)`n`t-oo = overwrite older existing files only `(-oo or -o`)`n`t|cat = output to command prompt (as opposed to stdout)`n`nExample:`nlevel -o -s c:\temp\1\*.* c:\temp\2\*.* d:\temp\output |cat`n`nLevel requires cat.exe from the UnxUtils project on SourceForge.net (http://sourceforge.net/projects/unxutils/) to display anything on the commandline. |
|---|
| 31 |
} |
|---|
| 32 |
else |
|---|
| 33 |
{ |
|---|
| 34 |
Loop, %0% ; For each parameter: |
|---|
| 35 |
{ |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
param := %A_Index% |
|---|
| 40 |
if param = -s |
|---|
| 41 |
{ |
|---|
| 42 |
subfolders = 1 |
|---|
| 43 |
FileAppend, SWITCH: subfolders leveled`n, * |
|---|
| 44 |
} |
|---|
| 45 |
else if param = -i |
|---|
| 46 |
{ |
|---|
| 47 |
individual = y |
|---|
| 48 |
FileAppend, SWITCH: individual prompting`n, * |
|---|
| 49 |
} |
|---|
| 50 |
else if param = -oo |
|---|
| 51 |
{ |
|---|
| 52 |
overwriteolderonly = y |
|---|
| 53 |
FileAppend, SWITCH: overwrite older files`n, * |
|---|
| 54 |
} |
|---|
| 55 |
else if param = -o |
|---|
| 56 |
{ |
|---|
| 57 |
overwrite = y |
|---|
| 58 |
FileAppend, SWITCH: overwrite existing files`n, * |
|---|
| 59 |
} |
|---|
| 60 |
else |
|---|
| 61 |
{ |
|---|
| 62 |
;get the path and add it in |
|---|
| 63 |
Loop %param% |
|---|
| 64 |
LongPath = %A_LoopFileLongPath% |
|---|
| 65 |
FolderCount += 1 |
|---|
| 66 |
Folder%FolderCount% := %A_Index% |
|---|
| 67 |
} |
|---|
| 68 |
} |
|---|
| 69 |
CopyDest := Folder%FolderCount% |
|---|
| 70 |
FileAppend, DESTINATION: %CopyDest%`n, * |
|---|
| 71 |
IfNotExist, %CopyDest% |
|---|
| 72 |
FileCreateDir, %CopyDest% |
|---|
| 73 |
|
|---|
| 74 |
Loop %FolderCount% |
|---|
| 75 |
{ |
|---|
| 76 |
; The following line uses the := operator to retrieve an array element: |
|---|
| 77 |
if A_Index < %FolderCount% |
|---|
| 78 |
{ |
|---|
| 79 |
CopySourcePattern := Folder%A_Index% ; A_Index is a built-in variable. |
|---|
| 80 |
GoSub, CopyIfNewer |
|---|
| 81 |
} |
|---|
| 82 |
} |
|---|
| 83 |
} |
|---|
| 84 |
ExitApp |
|---|
| 85 |
|
|---|
| 86 |
; Example #5: Copy only the source files that are newer than their counterparts |
|---|
| 87 |
; in the destination: |
|---|
| 88 |
CopyIfNewer: |
|---|
| 89 |
NewFilesCopied = 0 |
|---|
| 90 |
FileAppend, `nSOURCE: %CopySourcePattern%`n, * |
|---|
| 91 |
; Caller has set the variables CopySourcePattern and CopyDest for us. |
|---|
| 92 |
|
|---|
| 93 |
Loop, %CopySourcePattern%,0,%subfolders% |
|---|
| 94 |
{ |
|---|
| 95 |
Sleep, 10 |
|---|
| 96 |
copy_it = n |
|---|
| 97 |
StringReplace, FileNoSpaces, A_LoopFileName, %A_Space%,, All ;Take out spaces |
|---|
| 98 |
|
|---|
| 99 |
IfNotExist, %CopyDest%\%FileNoSpaces% ; Always copy if target file doesn't yet exist. |
|---|
| 100 |
copy_it = y |
|---|
| 101 |
else |
|---|
| 102 |
{ |
|---|
| 103 |
; File exists |
|---|
| 104 |
if overwrite = y |
|---|
| 105 |
{ |
|---|
| 106 |
if overwriteolderonly = n |
|---|
| 107 |
{ |
|---|
| 108 |
copy_it = y |
|---|
| 109 |
} |
|---|
| 110 |
else |
|---|
| 111 |
{ |
|---|
| 112 |
FileGetTime, time, %CopyDest%\%FileNoSpaces% |
|---|
| 113 |
EnvSub, time, %A_LoopFileTimeModified%, seconds ; Subtract the source file's time from the destination's. |
|---|
| 114 |
if time < 0 ; Source file is newer than destination file. |
|---|
| 115 |
copy_it = y |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
if copy_it = y |
|---|
| 121 |
{ |
|---|
| 122 |
NewFilesCopied += 1 |
|---|
| 123 |
FileCopy, %A_LoopFileFullPath%, %CopyDest%\%FileNoSpaces%, 1 ; Copy with overwrite=yes |
|---|
| 124 |
if ErrorLevel |
|---|
| 125 |
MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%FileNoSpaces%". |
|---|
| 126 |
FileAppend, %A_LoopFileFullPath%`n --> %CopyDest%\%FileNoSpaces%`n, * |
|---|
| 127 |
} |
|---|
| 128 |
} |
|---|
| 129 |
TotalFilesCopied += NewFilesCopied |
|---|
| 130 |
FileAppend, DONE: %NewFilesCopied%/%TotalFilesCopied% files copied`n, * |
|---|
| 131 |
Return |
|---|
| 132 |
|
|---|
| 133 |
GuiClose: |
|---|
| 134 |
ExitApp |
|---|