Golang : How to check if a file is hidden?
So, how to check if a file is hidden? The solutions below are OS dependent. One for Unix/Linux and another for Windows. It is pretty straightforward to check if a file is hidden under Linux/Unix, but more complicated under Windows.
For Linux/Unix:
package main
import (
"fmt"
"log"
"os"
"runtime"
)
func IsHidden(filename string) (bool, error) {
if runtime.GOOS != "windows" {
// unix/linux file or directory that starts with . is hidden
if filename[0:1] == "." {
return true, nil
} else {
return false, nil
}
} else {
log.Fatal("Unable to check if file is hidden under this OS")
}
return false, nil
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage : %s <filename>\n", os.Args[0])
os.Exit(0)
}
filename := os.Args[1]
result, err := IsHidden(filename)
if err != nil {
log.Println(err)
}
log.Println("["+filename+"]", "is hidden? ", result)
}
Sample output:
$ ./fileishidden .vscode
2017/04/26 10:35:10 .vscode is hidden? true
$ ./fileishidden work
2017/04/26 10:35:58 [work] is hidden? false
$ ./fileishidden .themes
2017/04/26 10:36:03 [.themes] is hidden? true
NOTE: I'd tried to build a cross-platform solution, but compiling the code below under Linux will generate these errors:
# command-line-arguments
./fileishiddenwindows.go:18: undefined: syscall.UTF16PtrFromString
./fileishiddenwindows.go:22: undefined: syscall.GetFileAttributes
./fileishiddenwindows.go:26: undefined: syscall.FILE_ATTRIBUTE_HIDDEN
However, everything is fine when compiling under Windows. Somehow, the Windows related functions are not available under Linux. Maybe changing the target platform will be helpful.
For Windows:
package main
import (
// use golang.org/x/sys/windows package in case syscall package has missing
// GetFileAttributes
//"golang.org/x/sys/windows"
"fmt"
"log"
"os"
"runtime"
"syscall"
)
func IsHidden(filename string) (bool, error) {
if runtime.GOOS == "windows" {
pointer, err := syscall.UTF16PtrFromString(filename)
if err != nil {
return false, err
}
attributes, err := syscall.GetFileAttributes(pointer)
if err != nil {
return false, err
}
return attributes&syscall.FILE_ATTRIBUTE_HIDDEN != 0, nil
} else {
log.Fatal("Unable to invoke GetFileAttributes() function or FILE_ATTRIBUTE_HIDDEN property")
}
return false, nil
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage : %s <filename>\n", os.Args[0])
os.Exit(0)
}
filename := os.Args[1]
result, err := IsHidden(filename)
if err != nil {
log.Println(err)
}
log.Println(filename, "is hidden? ", result)
}
Sample output:
References:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa364944(v=vs.85).aspx
See also : Golang : Test file read write permission example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.9k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+12.2k Golang : Arithmetic operation with numerical slices or arrays example
+5.4k Javascript : How to refresh page with JQuery ?
+9.3k Golang : How to generate Code 39 barcode?
+6.9k Golang : Check if one string(rune) is permutation of another string(rune)
+11.5k Golang : Verify Linux user password again before executing a program example
+7.8k Golang : Variadic function arguments sanity check example
+4.8k Golang : Convert lines of string into list for delete and insert operation
+8k Golang : Emulate NumPy way of creating matrix example
+21.7k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+7.2k Golang : Gorrila set route name and get the current route name
+12.1k Golang : Search and extract certain XML data example