5 people like it.

backup files

backup files , construct directory structure and then copy them one by one, ignore some sub directory in ignore list

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
open System
open System.IO

type FileUnit = 
    | Empty 
    | FileElem of string * FileAttributes
    | DirectoryElem of string * FileAttributes * FileUnit
    | FDList of seq<FileUnit>


let rec getFiles path =
    match DirectoryInfo(path).Exists with
    | false -> Empty
    | _     -> match DirectoryInfo(path).Attributes &&& FileAttributes.Directory with
               | FileAttributes.Directory -> let dflst = 
                                                        match Directory.GetFiles(path).Length, Directory.GetDirectories(path).Length with
                                                        |0, 0 -> Empty
                                                        |1, 0 -> let tpath = Directory.GetFiles(path).[0]
                                                                 FileElem(tpath, DirectoryInfo(tpath).Attributes)
                                                        |0, 1 -> let tpath = Directory.GetDirectories(path).[0]
                                                                 tpath |> getFiles
                                                        |_, _ -> FDList(seq{for elem in Directory.GetFiles(path) do
                                                                                yield FileElem(elem, DirectoryInfo(elem).Attributes)
                                                                            for elem in Directory.GetDirectories(path) do
                                                                                yield elem |> getFiles})
                                             DirectoryElem(path, DirectoryInfo(path).Attributes, dflst)
               | _ -> FileElem(path, DirectoryInfo(path).Attributes)

let rec copyfile (sourcepath:string) (destpath:string) ignorelst plst= 
    match plst with
    |Empty -> ()
    |FileElem(path, _) -> File.Copy(path, path.Replace(sourcepath, destpath), true)
    |DirectoryElem(path, _, lst) ->
                 if (not (Seq.exists (fun elem -> elem = DirectoryInfo(path).Name.ToLower()) ignorelst)) then
                    Directory.CreateDirectory(path.Replace(sourcepath, destpath)) |> ignore
                    copyfile sourcepath destpath ignorelst lst
    |FDList lst -> for elem in lst do
                       copyfile sourcepath destpath ignorelst elem

let backupfile (sourcepath:string) (destpath:string) ignorelst=
    getFiles sourcepath
    |> copyfile sourcepath destpath ignorelst

let auto_backup() =
    let Today = DateTime.Now.Date
    let Dest =  "d:/Backup/" + String.Format("{0}-{1}-{2}", Today.Year, Today.Month, Today.Day)
    Console.WriteLine("{0}", Dest.ToString())
    backupfile "d:/Some" Dest ["database"]
namespace System
namespace System.IO
type FileUnit =
  | Empty
  | FileElem of string * FileAttributes
  | DirectoryElem of string * FileAttributes * FileUnit
  | FDList of seq<FileUnit>

Full name: Script.FileUnit
union case FileUnit.Empty: FileUnit
union case FileUnit.FileElem: string * FileAttributes -> FileUnit
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
type FileAttributes =
  | ReadOnly = 1
  | Hidden = 2
  | System = 4
  | Directory = 16
  | Archive = 32
  | Device = 64
  | Normal = 128
  | Temporary = 256
  | SparseFile = 512
  | ReparsePoint = 1024
  ...

Full name: System.IO.FileAttributes
union case FileUnit.DirectoryElem: string * FileAttributes * FileUnit -> FileUnit
union case FileUnit.FDList: seq<FileUnit> -> FileUnit
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val getFiles : path:string -> FileUnit

Full name: Script.getFiles
val path : string
Multiple items
type DirectoryInfo =
  inherit FileSystemInfo
  new : path:string -> DirectoryInfo
  member Create : unit -> unit + 1 overload
  member CreateSubdirectory : path:string -> DirectoryInfo + 1 overload
  member Delete : unit -> unit + 1 overload
  member EnumerateDirectories : unit -> IEnumerable<DirectoryInfo> + 2 overloads
  member EnumerateFileSystemInfos : unit -> IEnumerable<FileSystemInfo> + 2 overloads
  member EnumerateFiles : unit -> IEnumerable<FileInfo> + 2 overloads
  member Exists : bool
  member GetAccessControl : unit -> DirectorySecurity + 1 overload
  member GetDirectories : unit -> DirectoryInfo[] + 2 overloads
  ...

Full name: System.IO.DirectoryInfo

--------------------
DirectoryInfo(path: string) : unit
field FileAttributes.Directory = 16
val dflst : FileUnit
type Directory =
  static member CreateDirectory : path:string -> DirectoryInfo + 1 overload
  static member Delete : path:string -> unit + 1 overload
  static member EnumerateDirectories : path:string -> IEnumerable<string> + 2 overloads
  static member EnumerateFileSystemEntries : path:string -> IEnumerable<string> + 2 overloads
  static member EnumerateFiles : path:string -> IEnumerable<string> + 2 overloads
  static member Exists : path:string -> bool
  static member GetAccessControl : path:string -> DirectorySecurity + 1 overload
  static member GetCreationTime : path:string -> DateTime
  static member GetCreationTimeUtc : path:string -> DateTime
  static member GetCurrentDirectory : unit -> string
  ...

Full name: System.IO.Directory
Directory.GetFiles(path: string) : string []
Directory.GetFiles(path: string, searchPattern: string) : string []
Directory.GetFiles(path: string, searchPattern: string, searchOption: SearchOption) : string []
Directory.GetDirectories(path: string) : string []
Directory.GetDirectories(path: string, searchPattern: string) : string []
Directory.GetDirectories(path: string, searchPattern: string, searchOption: SearchOption) : string []
val tpath : string
val elem : string
val copyfile : sourcepath:string -> destpath:string -> ignorelst:seq<string> -> plst:FileUnit -> unit

Full name: Script.copyfile
val sourcepath : string
val destpath : string
val ignorelst : seq<string>
val plst : FileUnit
type File =
  static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
  static member AppendAllText : path:string * contents:string -> unit + 1 overload
  static member AppendText : path:string -> StreamWriter
  static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
  static member Create : path:string -> FileStream + 3 overloads
  static member CreateText : path:string -> StreamWriter
  static member Decrypt : path:string -> unit
  static member Delete : path:string -> unit
  static member Encrypt : path:string -> unit
  static member Exists : path:string -> bool
  ...

Full name: System.IO.File
File.Copy(sourceFileName: string, destFileName: string) : unit
File.Copy(sourceFileName: string, destFileName: string, overwrite: bool) : unit
String.Replace(oldValue: string, newValue: string) : string
String.Replace(oldChar: char, newChar: char) : string
val lst : FileUnit
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
module Seq

from Microsoft.FSharp.Collections
val exists : predicate:('T -> bool) -> source:seq<'T> -> bool

Full name: Microsoft.FSharp.Collections.Seq.exists
Directory.CreateDirectory(path: string) : DirectoryInfo
Directory.CreateDirectory(path: string, directorySecurity: Security.AccessControl.DirectorySecurity) : DirectoryInfo
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val lst : seq<FileUnit>
val elem : FileUnit
val backupfile : sourcepath:string -> destpath:string -> ignorelst:seq<string> -> unit

Full name: Script.backupfile
val auto_backup : unit -> unit

Full name: Script.auto_backup
val Today : DateTime
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
DateTime()
   (+0 other overloads)
DateTime(ticks: int64) : unit
   (+0 other overloads)
DateTime(ticks: int64, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : unit
   (+0 other overloads)
property DateTime.Now: DateTime
property DateTime.Date: DateTime
val Dest : string
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
String.Format(format: string, [<ParamArray>] args: obj []) : string
String.Format(format: string, arg0: obj) : string
String.Format(provider: IFormatProvider, format: string, [<ParamArray>] args: obj []) : string
String.Format(format: string, arg0: obj, arg1: obj) : string
String.Format(format: string, arg0: obj, arg1: obj, arg2: obj) : string
property DateTime.Year: int
property DateTime.Month: int
property DateTime.Day: int
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
Console.WriteLine() : unit
   (+0 other overloads)
Console.WriteLine(value: string) : unit
   (+0 other overloads)
Console.WriteLine(value: obj) : unit
   (+0 other overloads)
Console.WriteLine(value: uint64) : unit
   (+0 other overloads)
Console.WriteLine(value: int64) : unit
   (+0 other overloads)
Console.WriteLine(value: uint32) : unit
   (+0 other overloads)
Console.WriteLine(value: int) : unit
   (+0 other overloads)
Console.WriteLine(value: float32) : unit
   (+0 other overloads)
Console.WriteLine(value: float) : unit
   (+0 other overloads)
Console.WriteLine(value: decimal) : unit
   (+0 other overloads)
String.ToString() : string
String.ToString(provider: IFormatProvider) : string
Raw view Test code New version

More information

Link:http://fssnip.net/is
Posted:10 years ago
Author:rigid wang
Tags: input and output