[英]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.