开发者

batch file to encrypt multiple archives

开发者 https://www.devze.com 2023-02-15 00:10 出处:网络
I have a collection of ZIP archives residing in a collection of folders inside Folder1\\, with more than one zip file per folder.

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 O开发者_如何转开发K with the unzipping/zipping, it's just the pathnames that have me stumped.


There are some issues with your script.

  1. 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.

  2. 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.

  3. 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 outer FOR 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:

for /D %%S in (*) do (
  for %%F in ("%%S\*.zip") do (
    echo "%UserProfile%\Desktop\Destination\%%S\%%~nxF"
    )
  )

And if you insist on using the .\* mask:

for /D %%S in (.\*) do (
  for %%F in ("%%~nxS\*.zip") do (
    echo "%UserProfile%\Desktop\Destination\%%~nxS\%%~nxF"
    )
  )
0

精彩评论

暂无评论...
验证码 换一张
取 消