Go to get windows under the file is hidden

Source: Internet
Author: User
Tags sublime text
This is a creation in Article, where the information may have evolved or changed.

At first, a small program was used to detect all the files on the disk.

package mainimport(  "fmt"  "io/ioutil"  "os")var dirpath ="D:\\"func main(){  CheckDir(dirpath)}func CheckDir(dirpath string){  dirs, err := ioutil.ReadDir(dirpath)  if err !=nil{    panic("目录输入有误!")  }  for _, dir := range dirs {    if dir.IsDir(){    if dir.Name()=="System Volume Information"{      fmt.Println("检测目录:", dirpath+"\\"+dir.Name(),"sys", dir.Sys())      return    }    fmt.Println("检测目录:", dirpath+"\\"+dir.Name(),"sys", dir.Sys())      CheckDir(dirpath +"\\"+ dir.Name())    }else{        fmt.Println("文件:", dirpath+"\\"+dir.Name(),"大小:", dir.Size())        if dir.Size()==0{        fmt.Println("删除文件:", dirpath+"\\"+dir.Name(), dir.Size())       }    }  }}

The output is:

File: d:\\my documents\downloads\wcftestclient_exe.rar Size: 110608

Detection directory: D:\\softdown sys &{16 { 2081520578 30419524} {1134116594 30422735} {1134116594 30422735} 0 0}

File: D:\\softdown\sublime Text Build 3012 Set Up.exe size: 7051120

Detection directory: D:\\softdown\vice.2015.720p.web-dl.dd5.1.h.264-playnow sys &{16 {753370423 30421925} {753410426 30421925} {753420426 30421925} 0 0}

File: d:\\softdown\ Vice.2015.720p.web-dl.dd5.1.h.264-playnow\vice.2015.720p.web-dl.dd5.1.h.264-playnow.mkv.tdl Size: 3255167129

File: d:\\softdown\vice.2015.720p.web-dl.dd5.1.h.264-playnow\f7a8aa8e2082c4ebe28f2c26cf16e4b08a27d5c1.torrent Size: 31751

File: D:\\softdown\vice.2015.720p.web-dl.dd5.1.h.264-playnow.qud.cfg size: 556

File: d:\\softdown\[ Iplaysoft.com]vs2013_rtm_ult_chs.iso size: 3077509120

Detection directory: D:\\system Volume information sys &{22 {1206047926 30310737} {1206983927 30310737} {1206983927 30310737} 0 0}

Panic: Wrong catalogue input!

Goroutine 1 [Running]:
Main. Checkdir (0xc0820585c0, 0x1d)
F:/goproj/gittest.git/trunk/src/website/main.go:22 +0xdemain. Checkdir (0x4f3890, 0x3)
F:/goproj/gittest.git/trunk/src/website/main.go:28 +0x46amain.main ()
F:/goproj/gittest.git/trunk/src/website/main.go:15 +0XFC

We're going to report an exception.

D:\System Volume Information Microsoft's answers:
This article describes how to access the System Volume Information folder. The System Volume Information folder is a hidden system folder that uses this folder to store its information and restore points. There is a System Volume information folder on each partition of the computer. You may need to access this folder for troubleshooting purposes.

It is necessary to determine whether the file is a hidden file, but the Golang API does not directly give this Ishidden attribute

Mode source learned:

Os. The stat method can get to a fileinfo, so write the following code FileInfo, _: = OS. Stat (dirpath) Sysifno: = FileInfo. Sys () fmt. Println (SYSIFNO)

    1. Os. Stat
    2. Func Lstat (name string) in the OS package (FI FileInfo, err Error)
    3. fs,err:=&fileStat{name: basename(name)}a FileInfo object was obtained by this initialization.

Code:

type Filestat struct {    name string    SYS  Syscall.Win32fileattributedata    //used to implement Samefile    Sync.Mutex    Path  string    Vol.   UInt32    Idxhi UInt32    Idxlo UInt32}type Win32fileattributedata struct {    FileAttributes UInt32    Creationtimefiletime    Lastaccesstimefiletime    Lastwritetimefiletime    Filesizehigh UInt32    Filesizelow  UInt32}

Then call the syscall.GetFileAttributesEx(namep, syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&fs.sys))) 3rd step of FS into the secondary method, call Kernel32.dll's GetFileAttributes method to get the file properties

The return value of the GetFileAttributes method for Kernel32.dll is as follows:

In MSDN, there are 15 properties for a file, and the properties of the file vary depending on the partition format of the disk.

The following collation is now done for the return value of the GetFileAttributes function

return field return value Property Type
File_attribute_readonly 1 Read-only
File_attribute_hidden 2 Hide
File_attribute_system 4 System
File_attribute_directory 16 Directory
File_attribute_archive 32 Archive
File_attribute_device 64 Keep
File_attribute_normal 128 Normal
File_attribute_temporary 256 Temporary
File_attribute_sparse_file 512 Sparse files
File_attribute_reparse_point 1024 Hyperlinks or shortcuts
File_attribute_compressed 2048 Compression
File_attribute_offline 4096 Offline
File_attribute_not_content_indexed 8192 Index
file_attribute_encrypted 16384 Encryption
File_attribute_virtual 65536 Virtual

The properties of the orange tag are the public properties of the files in the Windows system, where read-only, hidden, system, archive are the four basic properties of the file. Compressed,content_indexed,encrypted only exists in NTFS partitions.

After the file has stripped all of its properties (four basic properties), it is automatically marked as normal. Files with both system and hidden properties are completely invisible in the system, which is a common trick for viruses. Commpressed and encrypted cannot coexist. Files have the Content_indexed property by default

Here we can understand the output of this
FileAttributes = 22
Detection directory: D:\System Volume information sys &{22 {1206047926 30310737} {1206983927 30310737} {1206983927 30310737} 0 0}

22 is the meaning of
1 0 0 0 0 16 Catalog
1 0 0 4 system
& 1 0 2 Hidden
-—————-
1 0 1 1 0 = 22

Represents this file is a hidden file

Detection directory: D:\SoftDown\Vice.2015.720p.WEB-DL.DD5.1.H.264-PLAYNOW sys &{16 {753370423 30421925} {753410426 30421925} { 753420426 30421925} 0 0}
16 Catalog
This is clear: that is, if the second-to-last digit in the binary is 1, it is a hidden directory (file)

So we can make a judgment.

  Package Mainimport ("FMT" "Io/ioutil" "OS" "Reflect" "StrConv") var Dirpath = "D:\\" Func Main () { Checkdir (Dirpath)}func checkdir (Dirpath string) {dirs, err: = Ioutil. ReadDir (Dirpath) If Err!=nil{panic ("directory input is incorrect! ")} for _, dir: = Range dirs {if dir. Isdir () {if! Checkishidden (dir) {fmt. PRINTLN ("Detection directory:", dirpath+ "\ \" +dir. Name ()) Checkdir (Dirpath + "\ \" + dir. Name ())}}else{FMT. Println ("File:", dirpath+ "\ \" +dir. Name (), "Size:", dir. Size ()) if dir. Size () ==0{FMT. Println ("Delete file:", dirpath+ "\ \" +dir. Name (), dir. Size ())}}}}func Checkishidden (file OS. FileInfo) bool{//"obtains Win32fileattributedata's fileattributes FA by reflection: = Reflect. ValueOf (file. Sys ()). Elem (). Fieldbyname ("FileAttributes"). Uint () Bytefa: =[]byte (StrConv. Formatuint (fa,2)) if Bytefa[len (BYTEFA) -2]== ' 1 ' {fmt. Println ("Hidden directory:", file.) Name ()) return true} return false}  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.