简体   繁体   English

如何在包含许多Excel文件的目录中获取受密码保护的* .xlsx文件的列表?

[英]How to get a list of password protected *.xlsx files in a directory containing many Excel files?

Is there a way to see if *.xlsx files in a folder are password protected by reading the directory? 有没有办法查看文件夹中的* .xlsx文件是否通过读取目录进行密码保护?

Example directory text file 目录文本文件示例

1.xlsx 1.xlsx
2.xlsx 2.xlsx
3.xlsx 3.xlsx
4.xlsx 4.xlsx

Example results text file 示例结果文本文件

1.xlsx - protected 1.xlsx-受保护
2.xlsx 2.xlsx
3.xlsx - protected 3.xlsx-受保护
4.xlsx 4.xlsx

I basically want to see which ones out of my directory are password protected without trying to open them. 我基本上想查看目录中的哪些受密码保护,而无需尝试打开它们。 I am wondering if there is a directory switch. 我想知道是否有目录开关。 This is on a Windows machine by the way. 顺便说一下,这是在Windows机器上。

This will only work for OOXML files. 这仅适用于OOXML文件。 As this kind of files are ZIP files, the first two bytes in the file are PK unless the file is password protected. 由于这类文件是ZIP文件,因此除非文件受密码保护,否则文件的前两个字节为PK

So, assumming all the files to process are office OOXML files, let's test if the first character in the file is a P . 因此,假设要处理的所有文件都是Office OOXML文件,让我们测试文件中的第一个字符是否为P To capture this character, we will use a xcopy /w , that will wait for a keypress and echo this keypress. 要捕获此字符,我们将使用xcopy /w ,它将等待按键并回显此按键。 To capture the first character, simply redirect the file as the input to the xcopy, so, the keypress will be the first character in the file. 要捕获第一个字符,只需将文件作为输入重定向到xcopy,因此,按键将是文件中的第一个字符。 If this character is a P the file is not password protected. 如果此字符是P则文件不受密码保护。

@echo off
    setlocal enableextensions enabledelayedexpansion

    for %%a in (*.xlsx) do (
        call :isOfficeFilePasswordProtected "%%a" 
        if errorlevel 1 (
            echo %%a
        ) else (
            echo %%a - protected
        )
    )
    exit /b

:isOfficeFilePasswordProtected file
    if not exist "%~1" exit /b 1
    if %~z1 lss 1  exit /b 1
    setlocal enableextensions disabledelayedexpansion
    set "id=" & for /f "delims=" %%a in ('
        xcopy /l /w "%~f0" "%~f0" 2^>nul ^<"%~1" 
    ') do if not defined id set "id=%%a"
    if "%id:~-1%"=="P" ( set "exitCode=1" ) else ( set "exitCode=0" )
    endlocal & exit /b %exitCode%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将4个最新文件从受密码保护的在线目录复制到另一个目录? - How do I copy the 4 latest files from a password-protected online directory to another directory? 如何在 c# 中处理大量 excel 工作簿时跳过一些受密码保护的 excel 文件 - How to skip a few password protected excel files when processing a large number of excel workbooks in c# 获取应用程序目录中的文件列表 - Get list of files in application directory 有没有一种快速的方法来确定给定目录中的哪些文件是否已损坏或是否受密码保护(Windows) - Is there a quick way to determine which if any files are corrupt or password protected in a given directory (Windows) 解压缩受密码保护的文件时出现权限错误 - Permission error when unzipping files that are password protected 在 R 中解压缩受密码保护的 zip 文件 - Unzip password protected zip files in R 如何从C#中的目录中获取文件列表 - How to get a list of files from a directory in C# 如何使用 Windows API 列出目录中的文件? - How to list files in a directory using the Windows API? 可以在Windows上创建受密码保护的zip文件的C zip库吗? - C zip library that can create password protected zip files on windows? 从一个目录中读取许多小文件有多大问题? - How problematic is it to read many small files from one directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM