在保持目录结构的同时批量解密文件夹?

时间:2016-07-17 15:33:58

标签: windows batch-file

我在目录D:\Data中有数千个加密文件。它们具有不同的文件扩展名。例如D:\Data\a.txtD:\Data\Sub\b.jpg

我也有decrypt.exe。用法是:

  

解密<输入文件> [输出文件=输入] [选项]

我使用的选项是-o

现在我需要使用原始文件夹结构将所有文件解密为D:\Data_2

有人可以告诉我如何使用Windows批处理文件完成此任务吗?

我对Windows批处理文件编码知之甚少,因此不知道从哪里开始。

1 个答案:

答案 0 :(得分:0)

以下是此任务的注释批处理代码:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "SourcePath=D:\Data"
set "TargetPath=D:\Data_2"

rem If the batch file was started with a string as
rem parameter, interpret this string as source path.
if not "%~1" == "" set "SourcePath=%~1"

rem If the batch file was started with one more string
rem as parameter, interpret this string as target path.
if not "%~2" == "" set "TargetPath=%~2"

rem Remove backslash at end of source and target path
rem in case of being specified with a backslash at end.
if "%SourcePath:~-1%" == "\" set "SourcePath=%SourcePath:~0,-1%"
if "%TargetPath:~-1%" == "\" set "TargetPath=%TargetPath:~0,-1%"

rem Determine length of source path by finding out at which
rem position in source path there is no more character.
set "PathLength=1"
:GetPathLength
if not "!SourcePath:~%PathLength%,1!" == "" (
    set /A PathLength+=1
    goto GetPathLength
)

rem Process each file not having hidden or system attribute set and
rem decrypt it to the target path relative to source path. The relative
rem path is determined by removing from full path of current file the
rem first PathLength characters and the last character which is the
rem directory separator (backslash).
for /R "%SourcePath%" %%I in (*) do (
    set "RelativePath=%%~dpI"
    set "RelativePath=!RelativePath:~%PathLength%,-1!"
    md "%TargetPath%!RelativePath!" 2>nul
    decrypt.exe "%%I" "%TargetPath%!RelativePath!\%%~nxI" -o
)
endlocal

通过插入md左侧和decrypt.exe命令echo,可以通过干运行测试此批处理文件,只输出真正修改存储介质上某些内容的行。

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • call /? ...有关%~1%~2的解释。
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • rem /?
  • set /?
  • setlocal /?

另请阅读Microsoft有关Using command redirection operators的文章,了解2>nul用于重定向命令md的错误消息以解决目录上的 STDERR 的说明已存在于设备 NUL 以禁止它。

另请参阅Microsoft's command-line reference和SS64的A-Z Index of the Windows CMD command line

相关问题