AutoHotkey Community

It is currently May 27th, 2012, 4:26 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: [SOLVED] USB SYNC
PostPosted: February 25th, 2010, 7:15 pm 
Offline
User avatar

Joined: August 4th, 2008, 1:22 pm
Posts: 244
Location: UK
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 December 14th, 2011, 12:01 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 25th, 2010, 9:08 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 25th, 2010, 9:11 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 25th, 2010, 10:16 pm 
Offline
User avatar

Joined: August 4th, 2008, 1:22 pm
Posts: 244
Location: UK
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 25th, 2010, 11:17 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 26th, 2010, 9:03 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 26th, 2010, 9:34 am 
Offline

Joined: February 5th, 2010, 8:20 pm
Posts: 22
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....


Report this post
Top
 Profile  
Reply with quote  
 Post subject: A little thing
PostPosted: September 7th, 2011, 5:20 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 8th, 2011, 10:33 am 
Offline

Joined: March 16th, 2011, 6:12 pm
Posts: 172
Location: Worcester, Massachusetts
I use a handy little utility called DSynchronize http://download.cnet.com/DSynchronize/3 ... 69625.html

Props to Nimda for suggesting it to me :)

_________________
★★★ Email me at berban at aim full stop com ★★★


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: [SOLVED] USB SYNC
PostPosted: May 3rd, 2012, 7:20 pm 
If you are on Windows, doesn't xcopy do this rather easily?


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Mickers, rbrtryn, Yahoo [Bot] and 67 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group