Skip to content

Instantly share code, notes, and snippets.

@smallgeek
Created November 4, 2015 02:19
Show Gist options
  • Select an option

  • Save smallgeek/46cd71309454d42fbeaf to your computer and use it in GitHub Desktop.

Select an option

Save smallgeek/46cd71309454d42fbeaf to your computer and use it in GitHub Desktop.
ファイルが初期化されることを確認するため事前にマーキングする
open System
open System.IO
open System.Drawing
type Kind = Text | Image
let recognize filePath =
let ext = Path.GetExtension(filePath).ToLowerInvariant()
match ext with
| ".bmp"
| ".jpg" | ".jpeg"
| ".png"
| ".gif"
-> Image
| _ -> Text
let marking filePath =
match recognize filePath with
| Text ->
use stream = new FileStream(filePath, FileMode.Append, FileAccess.Write)
use writer = new StreamWriter(stream)
writer.WriteLine("## MARKING ##")
printfn "Text marking (%s)" filePath
| Image ->
use buffer = new MemoryStream(File.ReadAllBytes(filePath))
use image = Bitmap.FromStream(buffer)
use g = Graphics.FromImage(image)
g.DrawString("## MARKING ##", SystemFonts.DefaultFont, Brushes.Red, PointF.Empty)
image.Save(filePath)
printfn "Image marking (%s)" filePath
let allFiles targetDir =
let files = Directory.EnumerateFiles(targetDir, "*.*", SearchOption.AllDirectories)
files
[<EntryPoint>]
let main argv =
Environment.CurrentDirectory
|> allFiles
|> Seq.iter marking
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment