批处理文件加密多个档案
我有一组 ZIP 存档,位于Folder1\ 内的一组文件夹中,每个文件夹有多个 zip 文件。
我想在另一个目标文件夹 Destination\ 中创建此文件夹结构的副本,但所有 ZIP 文件均已加密。
Folder1\ 内的文件夹的嵌套永远不会超过一个,但递归到文件夹中的通用解决方案会很好。
我已经搞乱了子字符串,但无法让它工作。我确信我只有 % 的距离,但它让我难住了:
for /D %%S in (.\*) do (
echo %%S
set PN=%%S:~2,99%
echo %PN%
for %%F in (%%S\*.zip) do (
echo "%UserProfile%\Desktop\Destination\%PN%\%%~nxF"
)
)
%%S 返回“.\Folder”形式的路径,而“set PN=%%S:~2,99%”应该是删除“.\”,但它没有发生。
echo $$S 显示“.\Folder”(不带引号),这是可以的
echo %PN% 显示“.\Folder:~2,99”,这是不行的,
我对解压缩/压缩没问题,只是路径名让我难住了。
I have a collection of ZIP archives residing in a collection of folders inside Folder1\, with more than one zip file per folder.
I want to create a duplicate of this folder structure in another destination folder Destination\, but with all the ZIP files encrypted.
the folders inside Folder1\ are never nested any deeper than one, but a general solution that recurses into folders would be nice.
I have messed around with substrings but cannot get it to work. I'm sure I'm only a % away but it's got me stumped:
for /D %%S in (.\*) do (
echo %%S
set PN=%%S:~2,99%
echo %PN%
for %%F in (%%S\*.zip) do (
echo "%UserProfile%\Desktop\Destination\%PN%\%%~nxF"
)
)
the %%S returns a path in the form ".\Folder" and "set PN=%%S:~2,99%" is supposed to remove the ".\" but it ain't happening.
echo $$S displays ".\Folder" (without the quotes) which is OK
echo %PN% displays ".\Folder:~2,99" which is not OK
I'm OK with the unzipping/zipping, it's just the pathnames that have me stumped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的脚本存在一些问题。
不能将子字符串表达式与循环变量一起使用。您必须将其值存储到环境变量(例如
SET name=%%S
)并从该变量中提取子字符串。如果不启用变量的延迟扩展,如果变量是在同一个块中初始化的,则您将无法在括号内的命令块内使用环境变量。问题是,在解析父命令的同时解析块内的命令(并评估变量)(在本例中为
FOR
)。因此,很可能您总是会用一个空字符串来代替%PN%
。实际上您不需要
PN
变量。似乎您引入它只是为了删除文件夹名称的.\
部分。但您不必在外部FOR
循环中使用.\*
掩码,只需使用*
即可。 (不过,如果.\*
对您来说更有意义,您可以简单地使用%%~nxS
来替换文件夹的名称。)因此,这应该给出您的预期输出:
如果您坚持使用
.\*
掩码:There are some issues with your script.
You cannot use substring expressions with a loop variable. You'll have to store its value to an environment variable (like
SET name=%%S
) and extract the substring from that variable.Without enabling delayed expansion of variables you won't be able to use environment variables inside a command block enclosed in parentheses, if the vars are initialised within that same block. The problem is, the commands within the block are parsed (and vars are evaluated) at the same time the parent command is parsed (
FOR
in this case). So most probably you'll always have an empty string in place of%PN%
there.Actually you don't need the
PN
var. Seems like you've only introduced it to drop the.\
part of the folder name. But you don't have to use the.\*
mask in the outerFOR
loop, just use*
instead. (Still, if.\*
seems to you more meaningful, you can simply use%%~nxS
where you need to substitute the folder's name.)So, this should give you the expected output:
And if you insist on using the
.\*
mask: