史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   [VB.Net] 以命令列指令方式修改檔案屬性(針對指定資料夾變成唯讀) (http://forum.slime.com.tw/thread285948.html)

mini 2016-10-05 04:15 PM

[VB.Net] 以命令列指令方式修改檔案屬性(針對指定資料夾變成唯讀)
 
.使用整合開發軟體 Microsoft Visual Studio 2010
.起始頁 -> 新增專案 -> 主控台應用程式
.按Ctrl+A 反白選取貼上以下內容 代換之

PHP 語法:

Option Explicit On
Imports System
.IO

Module MainModule

    Sub Main
(ByVal cmdArgs() As String)
        If 
cmdArgs.Length 0 Then Exit Sub
        
If Right(cmdArgs(0), 1) <> "\" Then cmdArgs(0) = cmdArgs(0) & "\"

        Dim oFile As Array = IO.Directory.GetFileSystemEntries(cmdArgs(0), "
*.*", SearchOption.TopDirectoryOnly)

        Dim infoReader As System.IO.FileInfo
        Dim attributeReader As System.IO.FileAttributes

        For Each sFile As String In oFile
            infoReader = My.Computer.FileSystem.GetFileInfo(sFile)
            attributeReader = infoReader.Attributes

            If Not ((attributeReader And 1) = FileAttributes.ReadOnly) Then
                infoReader.Attributes = FileAttributes.ReadOnly Or attributeReader
            End If
        Next

    End Sub

End Module 

技術重點:
.使用引數的方式指定 資料夾
cmdArgs(0) 就是傳入的字串

.使用方法 IO.Directory.GetFileSystemEntries 獲取指定資料夾下的所有檔案 (存放在 Array類變數中)
.使用System.IO.FileInfo類變數得到檔案資訊集
.取.Attributes 得到目前檔案擁有的屬性 (放入attributeReader)

.只針對非唯讀檔案作更改使用
Not ((attributeReader And 1) = FileAttributes.ReadOnly) 作條件
FileAttributes.ReadOnly的常數值是1, 目前檔案擁有的屬性 與1 作And布林運算
如果還是等於 1 就表示目前擁有唯讀屬性
如果Not則進入條件修改之
|
V
.現在要加上唯讀屬性又保留其他屬性
就用 FileAttributes.ReadOnly Or attributeReader
並回存到 .Attributes 以更改檔案屬性
=============

最後建置成 改檔案屬性.exe 命令列用執行檔

可以用批次檔方式包裝再用定時執行方式執行
這個方法可以有效杜絕勒索病毒對你的重要檔案作修改
~~~批次檔.bat~~~
改檔案屬性.exe d:\我的重要檔案\
~~~~~~~~~~~~

雖然 Windows 檔案總管 也可輕易做到
但要說 定時/點擊 執行的話
使用 寫好的.bat的方式最方便

mini 2016-10-07 10:01 AM

以上程式碼只支援頂端資料夾下的檔案屬性修改

以下可以改成選擇性 所有附屬子資料夾都應用

PHP 語法:

 Dim so As SearchOption SearchOption.TopDirectoryOnly       
 
If UBound(cmdArgs) > 0 AndAlso cmdArgs(1) = "/a" Then
     so 
SearchOption.AllDirectories
 End 
If

 
'/將後面修改用 so變數來抉擇/
 Dim oFile As Array = IO.Directory.GetFileSystemEntries(cmdArgs(0), "*.*", so) 

例子:
改檔案屬性.exe d:\我的重要檔案\ /a
之後 d:\我的重要檔案\ 下
不論子資料夾 的檔案都會改成 唯讀檔案

===============
值得一提的是
雖然是命令列執行檔
但也可以用拖放的方式執行
只要用 Windows檔案總管 把資料夾拖放到 改檔案屬性.exe 檔案上
就可以 改檔案屬性.exe <拖放資料夾> 的結果執行
所以可以把此檔案以捷徑方式放在桌面方便執行
但如此就無法下達 /a 的參數
因此可以這樣
把 If Right(cmdArgs(0), 1) <> "\" Then cmdArgs(0) = cmdArgs(0) & "\"
改成
PHP 語法:

        If Right(cmdArgs(0), 1) <> "\" Then
            cmdArgs(0) = cmdArgs(0) & "
\"
            so = SearchOption.AllDirectories
        End If 

以後
改檔案屬性.exe d:\我的重要檔案\ /a
就可用
改檔案屬性.exe d:\我的重要檔案
取代
如用 Windows檔案總管 作資料夾拖放
就等於全修改

mini 2016-10-08 03:19 PM

最後來個綜合功能
.支援多資料夾指定
比如:
改檔案屬性.exe c:\123\ c:\abc
.可對 唯讀及隱藏 屬性 作ON/OFF 指定功能

範例如下A~D:
(A)
改檔案屬性.exe c:\123\ /h
表示隱藏 c:\123\ 下檔案,並應用唯讀
(B)
改檔案屬性.exe c:\123\ /h /nor
表示隱藏 c:\123\ 下檔案 但取消唯讀
(C)
改檔案屬性.exe c:\123 /noh
表示取消 c:\123\ 下檔案(包括附屬資料夾) 隱藏 並應用唯讀
(D)
改檔案屬性.exe c:\123\ /noh /nor /a d:\abc /h
表示
取消 c:\123\ 下檔案(包括附屬資料夾) 唯讀及隱藏 屬性
隱藏 d:\abc\ 下檔案(包括附屬資料夾) 並應用唯讀


程式碼如下,解釋就自行理解吧

PHP 語法:

Option Explicit On
Imports System
.IO

Module mainModule
    Dim so 
As SearchOption

    Sub Main
(ByVal cmdArgs() As String)
        
'**試驗用**
        '
ReDim cmdArgs(3)
        
'cmdArgs(0) = "c:\test"
        '
cmdArgs(1) = "/nor"
        'cmdArgs(2) = "/noh"
        '
cmdArgs(3) = "/h"
        '*********
        If cmdArgs.Length = 0 Then Exit Sub

        For i As Integer = 0 To UBound(cmdArgs)
            If Mid(cmdArgs(i), 2, 2) = ":\" Then
                '
======成立======
                
so SearchOption.TopDirectoryOnly '預設只處理指定資料夾,不處理附屬
                If Right(cmdArgs(i), 1) <> "\" Then '
如不是資料夾字串
                    cmdArgs
(i) = cmdArgs(i) & "\" '改成資料夾字串
                    so = SearchOption.AllDirectories '沒加 "
\"表示連同附屬資料夾都處理
                End If

                Dim infoReader As System.IO.FileInfo
                Dim attributeReader As System.IO.FileAttributes

                '屬性旗標共有 1111 1111 1111 1111 1111 1111 1111 1111 32種
                Dim NewFileAttributes As System.IO.FileAttributes = FileAttributes.ReadOnly '預設擁有唯讀屬性
                Dim NoEnFileAttributes As System.IO.FileAttributes = 0 '除能用

                '參數段處理
                For j As Integer = i + 1 To UBound(cmdArgs)
                    If Mid(cmdArgs(j), 2, 2) = "
:\" Then Exit For
                    Select Case LCase(cmdArgs(j))
                        Case "
/a" '連同附屬資料夾都處理
                            so = SearchOption.AllDirectories

                        Case "
/h" '隱藏
                            NewFileAttributes = NewFileAttributes Or FileAttributes.Hidden

                        Case "
/nor" '非唯讀
                            NoEnFileAttributes = NoEnFileAttributes Or FileAttributes.ReadOnly '表示要處理唯讀旗標
                        Case "
/noh" '非隱藏
                            NoEnFileAttributes = NoEnFileAttributes Or FileAttributes.Hidden '表示要處理隱藏旗標
                    End Select
                Next

                '資料夾內的檔案
                Dim oFile As Array = IO.Directory.GetFileSystemEntries(cmdArgs(i), "
*.*", so)
                For Each sFile As String In oFile
                    infoReader = My.Computer.FileSystem.GetFileInfo(sFile)
                    attributeReader = infoReader.Attributes '原本的屬性

                    '(致能)利用And邏輯運算 留下要修改的屬性項 是否等於 要修改的屬性,Not 的話就進入修改
                    If Not ((attributeReader And NewFileAttributes) = NewFileAttributes) Then
                        attributeReader = attributeReader Or NewFileAttributes '以Or方式 應用要修改(增加)的屬性
                    End If

                    '(除能)
                    infoReader.Attributes = attributeReader And (Not NoEnFileAttributes)
                Next
                '================
            End If
        Next
    End Sub

End Module 



所有時間均為台北時間。現在的時間是 08:09 AM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2025, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1