gpt4 book ai didi

batch-file - 批处理脚本GPG解密

转载 作者:行者123 更新时间:2023-12-01 02:06:59 24 4
gpt4 key购买 nike

我正在尝试编写一个批处理文件来解密 .gpg 的文件夹用相同的公钥加密的文件。这是我到目前为止:

@ECHO off
SET outbound=C:\encrypted files
SET olddir=%CD%
SET password=correcthorsebatterystaple
CD /d %outbound%
DIR *.gpg /B > tmp.txt
FOR /F "tokens=*" %%F IN (tmp.txt) DO (
ECHO %%F > tmplen.txt
FOR %%L IN (tmplen.txt) DO (SET namelen=%%~zL)
DEL tmplen.txt
gpg --output %%F:~0, namelen-4 --batch --yes --passphrase %password% --decrypt %%F)
DEL tmp.txt
CD /d %olddir%

目前它只是打印
usage: gpg [options] [filename]

这是我第一次尝试编写批处理脚本,所以我确信它很简单。

最佳答案

以下应该工作:

@ECHO off
SET password=correcthorsebatterystaple
PUSHD "C:\encrypted files"
FOR /F "tokens=*" %%F IN ('DIR *.gpg /B') DO (
gpg --output %%~nF --batch --yes --passphrase %password% --decrypt %%F)
POPD

解释:
  • PUSHDPOPD用于临时进入另一个目录;
  • 不需要临时文本文件来保存 DIR 的输出, 因为 FOR还能够解析命令的输出(IN () 中的集合被 '' 括起来,因此它被解释为命令,而不是文件规范。);
  • 要截断文件扩展名(您想对第二个临时文件和内部 FOR 循环做什么,至少根据我的解释),您只需要提供修饰符 ~n , 在我们的情况下 %%~nF ;您的方法不起作用,因为:
  • 您不能像 namelen-4 所尝试的那样进行在线数学运算(您需要将临时变量与 SET /A 一起用于算术运算,然后 delayed expansion 必须处于事件状态);
  • 子串扩展如 :~0,8不适用于 FOR变量(您需要一个临时变量来执行此操作,然后 delayed expansion 必须再次处于事件状态);

  • 添加:
    如果脚本在输入文件名中出现空格问题,您可能需要更换 gpg命令行:
    gpg --output "%%~nF" --batch --yes --passphrase %password% --decrypt "%%~F"
    ~修饰符删除潜在的双引号,所以 "%%~nF""%%~F"始终用双引号将文件名括起来。请注意 "%%F"可能会无意中导致双双引号......

    关于batch-file - 批处理脚本GPG解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31707172/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com