用脚本实现文件夹隐藏显示

wen 实用脚本 2

本文目录导读:

用脚本实现文件夹隐藏显示

  1. Windows批处理版本 (hide_show.bat)
  2. PowerShell版本 (hide_show.ps1)
  3. 高级版本 - VBScript (隐藏更彻底)
  4. Python版本 (如果系统有Python)
  5. 使用方法
  6. 注意事项

我来为您创建一个文件夹隐藏/显示的脚本,这里提供Windows批处理和PowerShell两种版本。

Windows批处理版本 (hide_show.bat)

@echo off
chcp 65001 >nul文件夹隐藏/显示工具
color 0A
:MENU
cls
echo ========================================
echo       文件夹隐藏/显示工具
echo ========================================
echo.
echo   [1] 隐藏文件夹
echo   [2] 显示文件夹
echo   [3] 退出
echo.
echo ========================================
echo.
set /p choice=请选择操作 (1/2/3): 
if "%choice%"=="1" goto HIDE
if "%choice%"=="2" goto SHOW
if "%choice%"=="3" exit
goto MENU
:HIDE
cls
echo.
set /p folder=请输入要隐藏的文件夹完整路径: 
if not exist "%folder%" (
    echo.
    echo [错误] 文件夹不存在!
    pause
    goto MENU
)
:: 设置隐藏和系统属性
attrib +h +s "%folder%"
if %errorlevel%==0 (
    echo.
    echo [成功] 文件夹已隐藏!
) else (
    echo.
    echo [失败] 隐藏文件夹失败!
)
pause
goto MENU
:SHOW
cls
echo.
set /p folder=请输入要显示的文件夹完整路径: 
if not exist "%folder%" (
    echo.
    echo [错误] 文件夹不存在!
    pause
    goto MENU
)
:: 取消隐藏和系统属性
attrib -h -s "%folder%"
if %errorlevel%==0 (
    echo.
    echo [成功] 文件夹已显示!
) else (
    echo.
    echo [失败] 显示文件夹失败!
)
pause
goto MENU

PowerShell版本 (hide_show.ps1)

# 文件夹隐藏/显示工具 - PowerShell版本
# 设置控制台编码
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
function Show-Menu {
    Clear-Host
    Write-Host "========================================" -ForegroundColor Cyan
    Write-Host "      文件夹隐藏/显示工具" -ForegroundColor Yellow
    Write-Host "========================================" -ForegroundColor Cyan
    Write-Host ""
    Write-Host "  [1] 隐藏文件夹" -ForegroundColor Green
    Write-Host "  [2] 显示文件夹" -ForegroundColor Green
    Write-Host "  [3] 退出" -ForegroundColor Green
    Write-Host ""
    Write-Host "========================================" -ForegroundColor Cyan
}
function Hide-Folder {
    Write-Host ""
    $folder = Read-Host "请输入要隐藏的文件夹完整路径"
    if (-not (Test-Path $folder)) {
        Write-Host "`n[错误] 文件夹不存在!" -ForegroundColor Red
        Read-Host "按回车键继续"
        return
    }
    try {
        # 设置隐藏和系统属性
        $item = Get-Item $folder -Force
        $item.Attributes = $item.Attributes -bor [System.IO.FileAttributes]::Hidden -bor [System.IO.FileAttributes]::System
        Write-Host "`n[成功] 文件夹已隐藏!" -ForegroundColor Green
    }
    catch {
        Write-Host "`n[失败] 隐藏文件夹失败:$($_.Exception.Message)" -ForegroundColor Red
    }
    Read-Host "`n按回车键继续"
}
function Show-Folder {
    Write-Host ""
    $folder = Read-Host "请输入要显示的文件夹完整路径"
    if (-not (Test-Path $folder)) {
        Write-Host "`n[错误] 文件夹不存在!" -ForegroundColor Red
        Read-Host "按回车键继续"
        return
    }
    try {
        # 取消隐藏和系统属性
        $item = Get-Item $folder -Force
        $item.Attributes = $item.Attributes -band (-bnot [System.IO.FileAttributes]::Hidden) -band (-bnot [System.IO.FileAttributes]::System)
        Write-Host "`n[成功] 文件夹已显示!" -ForegroundColor Green
    }
    catch {
        Write-Host "`n[失败] 显示文件夹失败:$($_.Exception.Message)" -ForegroundColor Red
    }
    Read-Host "`n按回车键继续"
}
# 主循环
while ($true) {
    Show-Menu
    $choice = Read-Host "`n请选择操作 (1/2/3)"
    switch ($choice) {
        "1" { Hide-Folder }
        "2" { Show-Folder }
        "3" { 
            Write-Host "再见!" -ForegroundColor Cyan
            exit 
        }
        default {
            Write-Host "`n无效选择,请重试!" -ForegroundColor Red
            Start-Sleep -Seconds 1
        }
    }
}

高级版本 - VBScript (隐藏更彻底)

' folder_hide_show.vbs
' 使用VBScript实现文件夹隐藏/显示
Option Explicit
Dim WshShell, fso, folderPath, choice
Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Do
    choice = InputBox("文件夹隐藏/显示工具" & vbCrLf & vbCrLf & _
              "1 - 隐藏文件夹" & vbCrLf & _
              "2 - 显示文件夹" & vbCrLf & _
              "3 - 退出" & vbCrLf & vbCrLf & _
              "请输入选择(1/2/3):", "文件夹工具", "1")
    If choice = "" Then Exit Do
    Select Case choice
        Case "1"
            folderPath = InputBox("请输入要隐藏的文件夹完整路径:", "隐藏文件夹")
            If folderPath <> "" Then
                If fso.FolderExists(folderPath) Then
                    fso.GetFolder(folderPath).Attributes = fso.GetFolder(folderPath).Attributes Or 2 Or 4
                    MsgBox "文件夹已成功隐藏!", vbInformation, "成功"
                Else
                    MsgBox "文件夹不存在!", vbExclamation, "错误"
                End If
            End If
        Case "2"
            folderPath = InputBox("请输入要显示的文件夹完整路径:", "显示文件夹")
            If folderPath <> "" Then
                If fso.FolderExists(folderPath) Then
                    fso.GetFolder(folderPath).Attributes = fso.GetFolder(folderPath).Attributes And (Not 2) And (Not 4)
                    MsgBox "文件夹已成功显示!", vbInformation, "成功"
                Else
                    MsgBox "文件夹不存在!", vbExclamation, "错误"
                End If
            End If
        Case "3"
            Exit Do
        Case Else
            MsgBox "无效的选择!", vbExclamation, "错误"
    End Select
Loop

Python版本 (如果系统有Python)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import ctypes
def set_folder_attributes(folder_path, hidden=True):
    """
    设置文件夹属性
    hidden=True: 隐藏
    hidden=False: 显示
    """
    if not os.path.exists(folder_path):
        print("文件夹不存在!")
        return False
    try:
        # Windows API设置属性
        # FILE_ATTRIBUTE_HIDDEN = 0x2
        # FILE_ATTRIBUTE_SYSTEM = 0x4
        if hidden:
            ctypes.windll.kernel32.SetFileAttributesW(folder_path, 0x2 | 0x4)
        else:
            ctypes.windll.kernel32.SetFileAttributesW(folder_path, 0x80)  # NORMAL
        return True
    except:
        # 备用方法
        try:
            if hidden:
                os.system(f'attrib +h +s "{folder_path}"')
            else:
                os.system(f'attrib -h -s "{folder_path}"')
            return True
        except:
            return False
def main():
    while True:
        print("\n" + "="*50)
        print("          文件夹隐藏/显示工具")
        print("="*50)
        print("  [1] 隐藏文件夹")
        print("  [2] 显示文件夹")
        print("  [3] 退出")
        print("="*50)
        choice = input("\n请选择操作 (1/2/3): ").strip()
        if choice == "1":
            folder = input("请输入要隐藏的文件夹路径: ").strip()
            if set_folder_attributes(folder, True):
                print("文件夹已成功隐藏!")
            else:
                print("操作失败!")
        elif choice == "2":
            folder = input("请输入要显示的文件夹路径: ").strip()
            if set_folder_attributes(folder, False):
                print("文件夹已成功显示!")
            else:
                print("操作失败!")
        elif choice == "3":
            print("再见!")
            break
        else:
            print("无效选择,请重试!")
if __name__ == "__main__":
    if sys.platform == "win32":
        main()
    else:
        print("此脚本仅支持Windows系统!")

使用方法

  1. 批处理版本:保存为 hide_show.bat,双击运行
  2. PowerShell版本:保存为 hide_show.ps1,在PowerShell中运行
  3. VBScript版本:保存为 folder_hide_show.vbs,双击运行
  4. Python版本:保存为 folder_tool.py,使用Python运行

注意事项

  • 需要管理员权限运行(推荐)
  • 隐藏的文件不会在资源管理器中显示(需要开启"显示隐藏文件")
  • 设置的系统属性可以防止文件夹被普通方式删除
  • 建议备份重要数据后再使用

选择适合您需求的版本使用即可!

抱歉,评论功能暂时关闭!