 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Cephei1
Joined: 04 Aug 2008 Posts: 199 Location: UK
|
Posted: Thu Feb 25, 2010 6:15 pm Post subject: [SOLVED] USB SYNC |
|
|
Hi, I know the program I am looking for already exists in the 'Scripts and Functions' bit, but I don't know which one is the one I'm looking for as I find the code for them too complicated for me to understand its function.
I simply want a small script that compares my USB folder (sub folders and files) with the Hard drive folder and if there are modifications made to the USB files then it copies this file and replaces the old copy in the Hard drive.
I don't want the script to replace files in the USB folder if there are modifications made in the Hard drive folder.
This is because I always open my work from USB, and save it to the USB but due too large number of files edited in USB everyday, its hard to manually compare the two folders too see which ones I modified in the USB and copy it to the Hard drive. Hope that makes sense.
Thanks in advance for any help/suggestions
DABSTER
Last edited by Cephei1 on Wed Dec 14, 2011 11:01 am; edited 1 time in total |
|
| Back to top |
|
 |
da_boogie_man Guest
|
Posted: Thu Feb 25, 2010 8:08 pm Post subject: |
|
|
I would think a simple FileCopy would work for this:
| Quote: |
FileCopy, SourcePattern, DestPattern [, Flag]
Parameters
SourcePattern The name of a single file or folder, or a wildcard pattern such as C:\Temp\*.tmp. SourcePattern is assumed to be in %A_WorkingDir% if an absolute path isn't specified.
DestPattern The name or pattern of the destination, which is assumed to be in %A_WorkingDir% if an absolute path isn't specified. To perform a simple copy -- retaining the existing file name(s) -- specify only the folder name as shown in these functionally identical examples:
FileCopy, C:\*.txt, C:\My Folder
FileCopy, C:\*.txt, C:\My Folder\*.*
Flag (optional) this flag determines whether to overwrite files if they already exist:
0 = (default) do not overwrite existing files
1 = overwrite existing files
This parameter can be an expression, even one that evalutes to true or false (since true and false are stored internally as 1 and 0).
|
And you can use this to see which Flash Drive has the required folder:
| Code: | origFolder = TestMe ;<--Name of your PRIMARY folder
copyFolder = c:\blah blah\%origFolder% ;<--Where you keep the Copied Data
SetFormat, Integer, H
DriveGet, DrvList, List, Removable
Loop, Parse, DrvList
{ DriveGet, DrvSerial, Serial, %A_Loopfield%:
StringTrimLeft, DrvSerial, DrvSerial,2
StringUpper, DrvSerial, DrvSerial
IfExist, %a_LoopField%:\%origFolder% ;<-- Looks for your folder
FileCopy, %a_LoopField%:\%origFolder%, %copyFolder%, 2 ;<-- Copies & Overwrites to wherever you're storing the copy.
}
|
<<Got part for checking the flash drives from:
http://www.autohotkey.com/forum/post-137122.html#137122
DBM |
|
| Back to top |
|
 |
da_boogie_man Guest
|
Posted: Thu Feb 25, 2010 8:11 pm Post subject: |
|
|
Oops. You would actually only need to use part of that:
| Code: | origFolder = TestMe ;<--Name of your PRIMARY folder
copyFolder = c:\blah blah\%origFolder% ;<--Where you keep the Copied Data
SetFormat, Integer, H
DriveGet, DrvList, List, Removable
Loop, Parse, DrvList
IfExist, %a_LoopField%:\%origFolder% ;<-- Looks for your folder
FileCopy, %a_LoopField%:\%origFolder%, %copyFolder%, 2 ;<-- Copies & Overwrites to wherever you're storing the copy.
|
DBM |
|
| Back to top |
|
 |
Cephei1
Joined: 04 Aug 2008 Posts: 199 Location: UK
|
Posted: Thu Feb 25, 2010 9:16 pm Post subject: |
|
|
Thanks DBM for your reply, I wanted something more on the lines of, Only replace the files that are changed, add the files that do not exist in the Harddisk folder.
Here is what i came up with so far. Bear with me on the code. Its confusing.
| Code: |
;_________| USB FOLDER, RECIEVES LIST OF FILES AND THEIR MODIFICATION DATES
FileList =
Loop, E:\XYZ\*.*
FileList = %FileList%%A_LoopFileTimeModified%`t%A_LoopFileName%`n
Loop, parse, FileList, `n
{
If A_LoopField =
Continue
StringSplit, FileItem, A_LoopField, %A_Tab% ; SPLITS THE DATE AND NAME INTO TWO VARIABLES
USBFILEMOD%A_Index% = %FileItem1% ; SAVES USB FILE NAME IN THE MEMORY
USBFILENAME%A_Index% = %FileItem2% ; SAVES USB FILE MODIFICATION DATE IN THE MEMORY
FileNO=%A_Index% ; USED LATER AS NUMBER OF LOOPS WHEN COMPARING MODIFICATION DATES
}
;_________| HARDDRIVE FOLDER, RECIEVES LIST OF FILES AND THEIR MODIFICATION DATES
FileList2 =
Loop, D:\XYZ\College\XYZ\*.*
FileList2 = %FileList2%%A_LoopFileTimeModified%`t%A_LoopFileName%`n
Loop, parse, FileList2, `n
{
If A_LoopField =
Continue
StringSplit, FileItem, A_LoopField, %A_Tab% ; SPLITS THE DATE AND NAME INTO TWO VARIABLES
HOMEFILEMOD%A_Index%=%FileItem1% ; SAVES HOME FILE NAME IN THE MEMORY
HOMEFILENAME%A_Index%=%FileItem2% ; SAVES HOME FILE MODIFICATION DATE IN THE MEMORY
}
;_________| COMPARING THE TWO MODIFICATION DATES AND REPLACING OLDER FILE IN HARDRIVE ACCORDINGLY
Loop, %FileNO%
{
USBMOD = % USBFILEMOD%A_Index% ; CHANGE THE NAME TO ADD %A_Index% INTO THE VARIABLE
HOMEMOD = % HOMEFILEMOD%A_Index% ; CHANGE THE NAME TO ADD %A_Index% INTO THE VARIABLE
MsgBox, USB:`t%USBMOD%`nHome:`t%HOMEMOD% ; CONFIRMS THE TWO DIFFERENT MODIFICATION DATES, ONLY USED DURING TESTING PROCESS. WILL BE REMOVED ONCE COMPLETE.
If USBMOD > HOMEMOD ; THIS IS THE PART THATS NOT WORKING, I TRIED "if var > value" I TRIED "IfGreater, var, value" NONE OF THESE WORK!
{
NAME= % USBFILENAME%A_Index% ; THIS IS ONLY USED DURING TESTING PROCESS. WILL BE REMOVED ONCE COMPLETE.
MsgBox, 4, File: %NAME% Has been modified. Want to replace on Harddrive?
IfMsgBox, Yes
{
FileCopy, E:\XYZ\%NAME% , D:\XYZ\College\XYZ\, 1
}
Else
{
}
}
Else
{
NAME= % USBFILENAME%A_Index% ; THIS IS ONLY USED DURING TESTING PROCESS. WILL BE REMOVED ONCE COMPLETE.
MsgBox, File: %NAME% `nHas not been modified ; THIS IS ONLY USED DURING TESTING PROCESS. WILL BE REMOVED ONCE COMPLETE.
}
} |
The part i am currently stuck on is the comparison, i tried both of these "IfGreater, var, value (same: if var > value)" no of which work.
Any other comparison methods?
Thanks
DABSTER |
|
| Back to top |
|
 |
da_boogie_man Guest
|
Posted: Thu Feb 25, 2010 10:17 pm Post subject: |
|
|
File size:
FileGetSize, OutputVar, Filename, Units
If they're different copy 'em
The docs have a LOT of stuff on file information and changing/whatever.
DBM |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Fri Feb 26, 2010 8:03 am Post subject: |
|
|
Place and run the following script from the USB drive:
| Code: | #SingleInstance, Force
SetBatchLines -1
Logfile := A_Temp "\USBSYNC.log", Target := "C:\USB_BACKUP"
Loop % SubStr( A_ScriptFullpath,1,3 ) "\*.*", 0, 1
{ If ( ( Update := FileExist( Trg := Target "\" SubStr( A_LoopFileLongPath,4 ) ) ) = ""
or InStr( FileExist( A_LoopFileLongPath ), "A" ) )
{
SplitPath, Trg,, Folder
If ! InStr( FileExist(Folder), "D" )
FileCreateDir, %Folder%
FileCopy, %A_LoopFileLongPath%, %Trg%, 1
Log .= ( Update ? "*" : "+" ) " " Trg "`n"
FileSetAttrib, -A, %A_LoopfileLongPath%
} }
IfEqual,Log,, SetEnv,Log,!!! Nothing to Update !!!`n
FileDelete, %Logfile%
FileAppend, Log Created: %A_Now%`n`n%Log%`nNote: ( "+" = New / "*" = Updated ), %Logfile%
Run Notepad.exe "%Logfile%",, Max |
|
|
| Back to top |
|
 |
Paradox
Joined: 05 Feb 2010 Posts: 22
|
Posted: Fri Feb 26, 2010 8:34 am Post subject: |
|
|
| I know the point of this forum is to help with autohotkey scripts, but, I'd also like to point out that windows comes with a very handy utility meant for this very thing called briefcase.... |
|
| Back to top |
|
 |
Zahari Guest
|
Posted: Wed Sep 07, 2011 4:20 pm Post subject: A little thing |
|
|
| Code: |
#SingleInstance force
SetBatchLines -1
Source=C:\Users\zachari\Autohotkey
Target=\\ZACHARI-PC\Videos\AHKBKup
Progress, W550 C00 FM8 WM100 M, ,Execution de la sauvgarde..., Execution de la sauvgarde...
Progress, ,,Creating folder tree.
TotalNumOfFiles=0
Loop, %Source%\*.*, , 1
{
TotalNumOfFiles++
}
Loop, %Source%\*.*,2 , 1 ;Create source folder tree to dest
{
StringReplace, DestMirorFile, A_LoopFileFullPath, %Source%, %Target%
IfNotExist %DestMirorFile%
{
FileCreateDir ,%DestMirorFile%
}
}
Loop, %Source%\*.*, , 1
{
CurrentProgress:=((A_Index*100)/TotalNumOfFiles)
StringReplace, DestMirorFile, A_LoopFileFullPath, %Source%, %Target%
IfNotExist %DestMirorFile%
{
FileCopy ,%A_LoopFileFullPath%,%DestMirorFile%
Progress, %CurrentProgress%,,%A_LoopFileFullPath% Not exist Created
continue
}
IfExist %DestMirorFile%
{
FileGetTime, TimeDest, %DestMirorFile%
EnvSub, TimeDest, %A_LoopFileTimeModified%, seconds
if TimeDest < 0
{
FileCopy ,%A_LoopFileFullPath%,%DestMirorFile%,1
Progress, %CurrentProgress%,,%A_LoopFileFullPath% Newer Copied
continue
}
else
{
Progress, %CurrentProgress%,,%A_LoopFileFullPath% Is Older ignored
continue
}
}
}
|
this copy new files from target to dest and replace older w newers |
|
| Back to top |
|
 |
berban_
Joined: 16 Mar 2011 Posts: 150 Location: Worcester, Massachusetts
|
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|