Go practice-File system operations

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

Because of their own PC notebook, with a poor 120G SSD hard disk, compared to the vast Win7,vs now, hard disk space is poor, often lead to insufficient hard disk space. There's no way, every time human flesh clears temporary files such as

vs compile, the. pdb file that is generated when the. obj file is connected is also the. sdf file that VS2010 introduced to improve smart hints.

So think of using the program to automatically implement this function, just learning the go language, all when practiced hand.

The simple procedure is to start from a directory recursively and delete certain types of files


My first go Little program, dirty files for VSPackage mainimport ("FMT" "OS" "Path" "strings") var Filetypes2clean = []string{". sdf", ". obj", ". pdb", ". TLog"}func main () {Dir_to_clean, _: = OS. GETWD () filecount, dirtysize: = Clean_dirtyfiles (Dir_to_clean) fmt. Printf ("Total delete%d files,%d mbytes\n", FileCount, dirtysize>>10>>10)}func clean_dirtyfiles (dir string ) (FileCount Int64, dirtysize Int64) {//fmt. Println ("to \ Dirty files for" + dir) f_dir, err: = OS. Open (dir) if err! = Nil {fmt. PRINTLN (Err) return}//enum Filesfis, Err_readdir: = F_dir. Readdir ( -1) if err_readdir! = nil {fmt. Println (Err_readdir) Return}for _, Fi: = Range fis {file_name: = fi. Name () if!path. Isabs (file_name) {file_name = path. Join (dir, file_name)}if fi. Isdir () {sub_filecount, Sub_dirtysize: = Clean_dirtyfiles (file_name) FileCount + = sub_filecountdirtysize + = Sub_ Dirtysizecontinue}file_ext: = Strings. ToLower (path. Ext (file_name)) for _, Ext: = Range Filetypes2clean {if ext! = file_ext {continue}fSize: = Fi. Size () Err_rm: = OS. Remove (file_name) if err_rm = = Nil {FileCount + = 1dirtysize + = fsizefmt. Println ("Delete:" + file_name)} else {fmt. Println (ERR_RM)}}//for _, Ext: = range}//return}





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.