SoFunction
Updated on 2025-03-05

Go implements automatic copying of USB drive widget demo

Automatically copy USB drive widget

Recently, golang wrote a widget about a computer that will automatically copy the contents in the USB flash drive after inserting it into the USB flash drive. However, the technology is limited, and a problem arises. I will send out the code and listen to everyone's opinions.

The code is as follows

package main
import (
  "io"
  "strings"
  "os"
  "path/filepath"
  "fmt"
  "strconv"
  "syscall"
  "/x/sys/windows/registry"
)
// Get the inserted U disk letterfunc GetUDisk() []string {
    //Check the registry to determine whether to insert the USB flash drive    k, err := (registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Services\USBSTOR\Enum`, registry.QUERY_VALUE)
    if err != nil {
        ("Not have U-Disk")
        return nil
    }
    defer ()
    // Get the value in the registry and get several USB drives inserted    count, _, err := ("Count")
    // Get all drive letters    disks := GetSystemDisks()
    return disks[len(disks)-int(count):]
}
//Recursively copy the directoryfunc copyDir(src string, dest string)  {
  src_original := src
  err := (src, func(src string, f , err error) error {
    if f == nil {
      return err
    }
    if () {
      //      (())
      //copyDir((), dest+"/"+())
    } else {
      //(src)
      //(src_original)
      //(dest)
      dest_new := (src, src_original, dest, -1)
      //(dest_new)
      //("CopyFile:" + src + " to " + dest_new)
      CopyFile(src, dest_new)
    }
    //println(path)
    return nil
  })
  if err != nil {
    //("() returned %v\n", err)
  }
}
// Copy the filefunc CopyFile(src, dst string) (w int64, err error) {
  srcFile, err := (src)
  if err != nil {
    (())
    return
  }
  defer ()
  //("dst:" + dst)
  dst_slices := (dst, "\\")
  dst_slices_len := len(dst_slices)
  dest_dir := ""
  for i := 0; i < dst_slices_len-1; i++ {
    dest_dir = dest_dir + dst_slices[i] + "\\"
  }
  //dest_dir := getParentDirectory(dst)
  //("dest_dir:" + dest_dir)
  b, err := PathExists(dest_dir)
  if b == false {
    err := (dest_dir, ) //Generate md directory in the current directory    if err != nil {
      (err)
    }
  }
  dstFile, err := (dst)
  if err != nil {
    (())
    return
  }
  defer ()
  return (dstFile, srcFile)
}
func getFilelist(path string) {
  err := (path, func(path string, f , err error) error {
    if f == nil {
      return err
    }
    if () {
      return nil
    }
    //println(path)
    return nil
  })
  if err != nil {
    //("() returned %v\n", err)
  }
}
func PathExists(path string) (bool, error) {
  _, err := (path)
  if err == nil {
    return true, nil
  }
  if (err) {
    return false, nil
  }
  return false, err
}
func main() {
  if len(GetUDisk()) <= 0 {
    ("No USB drive inserted")
    return
  }
  for _, v := range  GetUDisk() {
    (v + "\\")
    copyDir(v + "\\",v + ":\\go_copy")
  }
}

Problem Disadvantages

The problem that the above code is now encountered is:

Too many or too large U disk files will cause the computer to get stuck

Recursive copying takes a lot of time

The above is the detailed content of Go to automatically copy USB drive. For more information about Go to automatically copy USB drive, please pay attention to my other related articles!