Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 用上一个日期文件解密文件_Batch File_Encryption_Gnupg - Fatal编程技术网

Batch file 用上一个日期文件解密文件

Batch file 用上一个日期文件解密文件,batch-file,encryption,gnupg,Batch File,Encryption,Gnupg,我每天都会收到带有昨天日期戳的.gpg文件(例如my_daily_export_20170908.csv.gpg),如果我将日期戳更改为当前日期,但不是昨天日期,我可以解密该文件,有什么想法吗?我的代码如下: @echo off for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set "dt=%%a" set "YYYY=%dt:~0,4%" set "MM=%dt:~4,2%" set "DD=%dt

我每天都会收到带有昨天日期戳的.gpg文件(例如my_daily_export_20170908.csv.gpg),如果我将日期戳更改为当前日期,但不是昨天日期,我可以解密该文件,有什么想法吗?我的代码如下:

@echo off

for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set "dt=%%a"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"
set "secret=mypassphrase"
set datestamp=%YYYY%%MM%%DD%

set "origPath=Y:\"
set "origFile=my_daily_export_"
set "origExt=.csv.gpg"
set "origCompleteFile=%origPath%%origFile%%datestamp%%origExt%"

set "destPath=G:\"
set "destFile=my_daily_export_"
set "destExt=.csv"
set "destCompleteFile=%destPath%%destFile%%datestamp%%destExt%"

set "ctaFolder=S:\"

echo Decrypt file: "%origCompleteFile%"
echo Save decrypted file in: "%destCompleteFile%"

rem Decrypt CTA file
gpg --batch --passphrase "%secret%" -o "%destCompleteFile%" --decrypt "%origCompleteFile%"


rem Copy to CTA folder in .54
%SystemRoot%\System32\xcopy.exe "%destCompleteFile%" "%ctaFolder%" /Q /Y >nul
endlocal

我建议简化工作,不要依赖文件名中的时间戳

下面注释的批处理代码对设置了存档文件属性的每个文件解密一次,并忽略所有其他文件

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem All folder paths must end with a backslash!

set "ctaFolder=S:\"
set "decryptFolder=G:\"
set "encryptFolder=Y:\"
set "secret=mypassphrase"
set "Error=0"

rem Process all files matching the wildcard pattern in folder with the
rem encrypted files having the archive attribute set. Skip all directories
rem and all files matching also the pattern having archive attribute not
rem set anymore as being processed already once in the past by this script.

for /F "delims=" %%I in ('dir /AA-D /B "%encryptFolder%\my_daily_export_*.csv.gpg" 2^>nul') do call :DecryptFile "%encryptFolder%%%I"

rem Output an empty line and standard user prompt for any key
rem press in case of a decryption or copying error occurred.

if %Error% == 1 echo/ & pause

endlocal
goto :EOF


rem DecryptFile is a subroutine called by the FOR loop above to
rem decrypt the file passed as first argument to the subroutine
rem which must be with full path and enclosed in double quotes.

rem An error message is output if gpg.exe fails to decrypt the file
rem detected by exit code of gpg or by decrypted file missing or by
rem decrypted file has a file size of 0 bytes in which case the empty
rem output file is deleted before batch file processing continues.

rem The file is copied to the cta folder after successful decryption
rem with verification on successful copying. Otherwise the decrypted
rem file is kept in decrypt folder and an error message is output.

rem The archive attribute on encrypted file is removed after successful
rem decryption and copying to cta folder. So this file is not processed
rem once again in future except it is modified or copied once again into
rem folder for encrypted files on which Windows sets the archive attribute
rem automatically again on this file.

:DecryptFile
gpg.exe --batch --passphrase "%secret%" -o "%decryptFolder%%~n1" --decrypt %1 2>nul

if errorlevel 1 goto FailedDecrypt
if not exist "%decryptFolder%%~n1" goto FailedDecrypt
for /F %%J in ("%decryptFolder%%~n1") do if %%~zJ == 0 goto FailedDecrypt

set "CopiedCount=0"
for /F %%J in ('%SystemRoot%\System32\xcopy.exe "%decryptFolder%%~n1" "%ctaFolder%" /C /Q /V /Y 2^>nul') do set "CopiedCount=%%J"
if not "%CopiedCount%" == "1" (
    echo Failed to copy "%decryptFolder%%~n1" to "%ctaFolder%"
    set "Error=1"
    goto :EOF
)

%SystemRoot%\System32\attrib.exe -a %1
goto :EOF

:FailedDecrypt
del "%decryptFolder%%~n1" 2>nul
echo Failed to decrypt file: %1
set "Error=1"
goto :EOF
另一种变体是根据文件名解密文件夹中的最新文件,该文件名是文件名中的最新文件夹,包含格式为
YYYYMMDD
的日期

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem All folder paths must end with a backslash!

set "ctaFolder=S:\"
set "decryptFolder=G:\"
set "encryptFolder=Y:\"
set "secret=mypassphrase"

rem With all files matching pattern my_daily_export_????????.csv.gpg
rem containing the date in format YYYYMMDD in file name the list output by
rem DIR in reverse order sorted by name results in getting as first line
rem output the newest file in the folder. This file has to be decrypted.

for /F "delims=" %%I in ('dir /A-D /B /O-N "%encryptFolder%\my_daily_export_????????.csv.gpg" 2^>nul') do (
    set "FileNameFull=%encryptFolder%%%I"
    set "FileNameOnly=%%~nI"
    goto DecryptFile
)

goto ExitBatch

rem DecryptFile block decrypts the file specified which full file
rem name which is assigned to the environment variable FileNameFull.

rem An error message is output if gpg.exe fails to decrypt the file
rem detected by exit code of gpg or by decrypted file missing or by
rem decrypted file has a file size of 0 bytes in which case the empty
rem output file is deleted before batch file processing continues.

rem The file is copied to the cta folder after successful decryption
rem with verification on successful copying. Otherwise the decrypted
rem file is kept in decrypt folder and an error message is output.

:DecryptFile
gpg.exe --batch --passphrase "%secret%" -o "%decryptFolder%%FileNameOnly%" --decrypt "%FileNameFull%" 2>nul

if errorlevel 1 goto FailedDecrypt
if not exist "%decryptFolder%%FileNameOnly%" goto FailedDecrypt
for /F %%I in ("%decryptFolder%%FileNameOnly%") do if %%~zI == 0 goto FailedDecrypt

set "CopiedCount=0"
for /F %%I in ('%SystemRoot%\System32\xcopy.exe "%decryptFolder%%FileNameOnly%" "%ctaFolder%" /C /Q /V /Y 2^>nul') do set "CopiedCount=%%I"
if not "%CopiedCount%" == "1" (
    echo Failed to copy "%decryptFolder%%FileNameOnly%" to "%ctaFolder%"
    echo/
    pause
)
goto ExitBatch

:FailedDecrypt
del "%decryptFolder%%FileNameOnly%" 2>nul
echo Failed to decrypt file: "%FileNameFull%"
echo/
pause

:ExitBatch
endlocal
批处理文件将根据上次修改日期使用
/O-D
而不是DIR命令对最新文件进行解密,这将使批处理文件独立于文件名

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • attrib/?
  • 呼叫/?
  • del/?
  • dir/?
  • echo/?
  • endlocal/?
  • 获取/?
  • goto/?
  • gpg--help
  • 如果/?
  • 暂停/?
  • rem/?
  • 设置/?
  • setlocal/?
  • xcopy/?
阅读Microsoft关于的文章,了解有关
2>nul
的解释


请阅读上的答案,了解操作员的解释
&

昨天是几号?根据你的问题,今天是几号?这些加密档案的实际文件名是什么?什么错误消息。。。你知道吗,它们发生在哪一行?此问题缺少非常基本的信息,无法按原样回答。在批处理文件中('powershell-NoP-C'(获取日期)。AddDays(-1)。ToString(\'yyyyMMdd\'))中为/f%%Y设置昨天=%%Y如果搜索堆栈覆盖流,您将在获取昨天日期时找到大量问题和答案。