PDA

View Full Version : Script to copy one directory to other, recursively (ideal for SVN softcopy)



Petr Schreiber
13-10-2010, 16:57
I use the following script to copy one directory used with SVN to other dir without SVN tracking data.
In fact, it simply copies the non hidden files and regenerates the directory structure.

I hope somebody might find this useful, you can customize the directories in the first lines of the script and then execute customized version of the script when needed.



'
' Script to receive SVN files to working directory
' Petr Schreiber, 2010
'

Uses "DT" '---Load DateTime module
Uses "FILE" '---Load File module
Uses "console" '---Load Console module


' #############################################################################
' OPERATION PARAMETERS
' #############################################################################
Dim Simulation As Long = TRUE ' -- TRUE = does not copy any files
Dim UseBruteForce As Long = FALSE ' -- TRUE = copies all files from SVN, FALSE = copies only newer files from SVN



Dim IgnoreExtensions As String = LCase$("[dll][pdb][suo][exe]")
Dim IgnoreSubdirs As String = LCase$("[bin][obj]")

Dim SourceDirectory As String = "D:\MySVNDirectory\MyProject"
Dim TargetDirectory As String = "C:\MyLocalDirectory\MyProject"
' #############################################################################

Dim FailCount As Long
Dim AddCount As Long
Dim FixCount As Long

Dim ErrorLog As String

Function TBMain()

If DIR_Exists(SourceDirectory) = FALSE Then
PrintL "Source directory does not exist, no operation will be performed"
WaitKey
Stop
End If

If DIR_Exists(TargetDirectory) = FALSE Then
PrintL "Target directory does not exist, it will be created for you now..."
DIR_MakeAll(TargetDirectory)
End If

FindDirectories(SourceDirectory)

If FailCount Then
ClipBoard_SetText(ErrorLog)
PrintL "Not all files managed their journey to the other side"
PrintL "List of them has been placed to your clipboard"
Else
PrintL "Operation completed successfully"
If UseBruteForce Then
PrintL FixCount + " files received"
Else
PrintL AddCount + " files added"
PrintL FixCount + " files updated"
End If
End If

If Simulation Then
PrintL
PrintL "[!] This was just simulation run, no file operations performed"
PrintL
End If

WaitKey

End Function

Function FindDirectories(Path As String)

Dim Dirs() As String
Dim DirCount As Long

PerformCopy(Path)

DirCount = DIR_ListArray(Dirs, Path, "*", %FILE_SUBDIR Or %FILE_ADDPATH)

Dim i As Long
For i = 1 To DirCount
If InStr(IgnoreSubdirs, LCase$("["+Parse$(Dirs(i), "\", -1))+"]") = 0 Then
FindDirectories(Dirs(i))
End If
Next

End Function

Function PerformCopy(Path As String)

Dim TargetPath As String = TransformPathToTarget(Path)
Dim SourceFile, TargetFile As String
Dim Files() As String
Dim FileCount As Long
Dim Written As Long


FileCount = DIR_ListArray(Files, Path, "*.*", %FILE_NORMAL Or %FILE_SYSTEM Or %FILE_ADDPATH)

Dim i As Long

If DIR_Exists(TargetPath) = FALSE Then DIR_MakeAll(TargetPath)

For i = 1 To FileCount
SourceFile = Files(i)
TargetFile = TransformPathToTarget(SourceFile)

If InStr(IgnoreExtensions, "["+LCase$(FILE_PathSplit(Files(i), %PATH_EXT))+"]") = 0 Then
If UseBruteForce = FALSE Then
If FILE_Exists(TargetFile) = FALSE Then
If ManagedCopy(SourceFile, TargetFile) Then
PrintL "[FAIL] " + TargetFile
ErrorLog += "Failed to copy: "+ SourceFile + $CRLF
FailCount += 1
Written = TRUE
Else
PrintL "[ ADD] " + TargetFile
AddCount += 1
Written = TRUE
End If

ElseIf GetSourceState(SourceFile, TargetFile) = %newer Then
If ManagedCopy(SourceFile, TargetFile) Then
PrintL "[FAIL] " + TargetFile
ErrorLog += "Failed to copy: "+ SourceFile + $CRLF
FailCount += 1
Written = TRUE
Else
PrintL "[ FIX] " + TargetFile
FixCount += 1
Written = TRUE
End If
ElseIf GetSourceState(SourceFile, TargetFile) = %older Then
PrintL "[ OLD] " + SourceFile
ErrorLog += "The following file in target location is newer than the one in source location: " + TargetFile + $CRLF
FailCount += 1
Written = TRUE

End If
Else
If ManagedCopy(SourceFile, TargetFile) Then
PrintL "[FAIL] " + TargetFile
ErrorLog += TargetFile + $CRLF
FailCount += 1
Else
PrintL "[ OK ] " + TargetFile
FixCount += 1
End If
End If
End If

Next
If Written = TRUE Then PrintL

End Function

Function TransformPathToTarget(Path As String) As String

Return Replace$(Path, SourceDirectory, TargetDirectory)

End Function

Begin Const
%newer
%older
%match
End Const

Function GetSourceState( source As String, target As String ) As Long

Dim HashS As Long = HASH(1, FILE_Load(source))
Dim HashT As Long = HASH(1, FILE_Load(target))

If HashS = HashT Then Return %match

Dim SourceDate As String = FILE_GetDate(source, %DATE_TIME_LAST_FILE_WRITE)
Dim TargetDate As String = FILE_GetDate(target, %DATE_TIME_LAST_FILE_WRITE)

Dim SourceTime As String = FILE_GetTime(source, %DATE_TIME_LAST_FILE_WRITE)
Dim TargetTime As String = FILE_GetTime(target, %DATE_TIME_LAST_FILE_WRITE)

' Different dates
If DT_DateDiff(SourceDate, TargetDate, %DT_DIFF_IN_SECONDS) < 0 Then Return %newer

If DT_DateDiff(SourceDate, TargetDate, %DT_DIFF_IN_SECONDS) > 0 Then Return %older

' Same date, time used
If DT_TimeToMillisec(SourceTime) > DT_TimeToMillisec(TargetTime) Then Return %newer

If DT_TimeToMillisec(SourceTime) < DT_TimeToMillisec(TargetTime) Then Return %older

Return %match

End Function

Function ManagedCopy(sourceFile As String, targetFile As String) As Long
If Simulation Then
Return 0
Else
Return FILE_Copy(sourceFile, targetFile)
End If
End Function



Petr

Petr Schreiber
15-10-2010, 15:36
Other code for transfer from local->SVN directory:



'
' Script to upload files from working directory to SVN
' Petr Schreiber, 2010
'

Uses "DT" '---Load DateTime module
Uses "FILE" '---Load File module
Uses "console" '---Load Console module


' #############################################################################
' OPERATION PARAMETERS
' #############################################################################
Dim Simulation As Long = FALSE

Dim SourceDirectory As String = "C:\MyLocalDirectory\MyProject"
Dim TargetDirectory As String = "D:\MySVNDirectory\MyProject"

Dim IgnoreExtensions As String = LCase$("[dll][pdb][suo][exe]")
Dim IgnoreSubdirs As String = LCase$("[bin][obj]")

Dim FileType As Long = %FILE_NORMAL Or %FILE_SYSTEM Or %FILE_HIDDEN
Dim DirType As Long = %FILE_SYSTEM Or %FILE_HIDDEN
' #############################################################################

Dim FailCount As Long
Dim AddCount As Long
Dim FixCount As Long

Dim ErrorLog As String

Function TBMain()

If DIR_Exists(SourceDirectory) = FALSE Then
PrintL "Source directory does not exist, no operation will be performed"
WaitKey
Stop
End If

If DIR_Exists(TargetDirectory) = FALSE Then
PrintL "Target directory does not exist, it will be created for you now..."
DIR_MakeAll(TargetDirectory)
End If

FindDirectories(SourceDirectory)

If FailCount Then
ClipBoard_SetText(ErrorLog)
PrintL "Not all files managed their journey to the other side"
PrintL "List of them has been placed to your clipboard"
Else
PrintL "Operation completed successfully"



PrintL AddCount + " files added"
PrintL FixCount + " files updated"
End If


If Simulation Then
PrintL
PrintL "[!] This was just simulation run, no file operations performed"
PrintL
End If

WaitKey

End Function

Function FindDirectories(Path As String)

Dim Dirs() As String
Dim DirCount As Long

PerformCopy(Path)

DirCount = DIR_ListArray(Dirs, Path, "*", DirType Or %FILE_SUBDIR Or %FILE_ADDPATH)

Dim i As Long
For i = 1 To DirCount
If InStr(IgnoreSubdirs, LCase$("["+Parse$(Dirs(i), "\", -1))+"]") = 0 Then
FindDirectories(Dirs(i))
End If
Next

End Function

Function PerformCopy(Path As String)

Dim TargetPath As String = TransformPathToTarget(Path)
Dim SourceFile, TargetFile As String
Dim Files() As String
Dim FileCount As Long
Dim Written As Long
FileCount = DIR_ListArray(Files, Path, "*.*", FileType Or %FILE_ADDPATH)



Dim i As Long

If DIR_Exists(TargetPath) = FALSE Then DIR_MakeAll(TargetPath)

For i = 1 To FileCount
SourceFile = Files(i)
TargetFile = TransformPathToTarget(SourceFile)

If InStr(IgnoreExtensions, "["+LCase$(FILE_PathSplit(Files(i), %PATH_EXT))+"]") = 0 Then

If FILE_Exists(TargetFile) = FALSE Then
If ManagedCopy(SourceFile, TargetFile) Then
PrintL "[FAIL] " + TargetFile
ErrorLog += "Failed to copy: "+ SourceFile + $CRLF
FailCount += 1
Written = TRUE
Else
PrintL "[ ADD] " + TargetFile
AddCount += 1
Written = TRUE
End If

ElseIf GetSourceState(SourceFile, TargetFile) = %newer Then
If ManagedCopy(SourceFile, TargetFile) Then
PrintL "[FAIL] " + TargetFile
ErrorLog += "Failed to copy: "+ SourceFile + $CRLF
FailCount += 1
Written = TRUE
Else
PrintL "[ FIX] " + TargetFile
FixCount += 1
Written = TRUE
End If
ElseIf GetSourceState(SourceFile, TargetFile) = %older Then
PrintL "[ OLD] " + SourceFile
ErrorLog += "The following file in target location is newer than the one in source location: " + TargetFile + $CRLF
FailCount += 1
Written = TRUE










End If

End If

Next
If Written = TRUE Then PrintL

End Function

Function TransformPathToTarget(Path As String) As String

Return Replace$(Path, SourceDirectory, TargetDirectory)

End Function

Begin Const
%newer
%older
%match
End Const

Function GetSourceState( source As String, target As String ) As Long

Dim HashS As Long = HASH(1, FILE_Load(source))
Dim HashT As Long = HASH(1, FILE_Load(target))

If HashS = HashT Then Return %match

Dim SourceDate As String = FILE_GetDate(source, %DATE_TIME_LAST_FILE_WRITE)
Dim TargetDate As String = FILE_GetDate(target, %DATE_TIME_LAST_FILE_WRITE)

Dim SourceTime As String = FILE_GetTime(source, %DATE_TIME_LAST_FILE_WRITE)
Dim TargetTime As String = FILE_GetTime(target, %DATE_TIME_LAST_FILE_WRITE)

' Different dates
If DT_DateDiff(SourceDate, TargetDate, %DT_DIFF_IN_SECONDS) < 0 Then Return %newer

If DT_DateDiff(SourceDate, TargetDate, %DT_DIFF_IN_SECONDS) > 0 Then Return %older

' Same date, time used
If DT_TimeToMillisec(SourceTime) > DT_TimeToMillisec(TargetTime) Then Return %newer

If DT_TimeToMillisec(SourceTime) < DT_TimeToMillisec(TargetTime) Then Return %older

Return %match

End Function

Function ManagedCopy(sourceFile As String, targetFile As String) As Long
If Simulation Then
Return 0
Else
Return FILE_Copy(sourceFile, targetFile)
End If
End Function



Petr

Petr Schreiber
26-10-2010, 11:09
Updated both codes.

Now it does not look on time only, but it also checks hash value to prevent false positives.


Petr