这是我的情况。项目的目标是将一些附件迁移到另一个系统。
这些附件将位于父文件夹中,比方说“Folder 0
" (see 这个问题的图 https://serverfault.com/questions/147902/windows-command-line-built-in-compression-extraction-tool为了更好地理解),它们将被压缩/压缩。
我希望像这样调用我的批处理脚本:
BatchScript.bat "c:\temp\usd\Folder 0"
我在用着7za.exe
作为命令行提取工具。
我希望我的批处理脚本做的是迭代“Folder 0
" 的子文件夹,并将所有包含的 ZIP 文件提取到各自的文件夹中。
提取的文件必须与其各自的 ZIP 文件位于同一文件夹中。因此,文件包含在“File 1.zip
“ 需要在 ”Folder 1
”等等。
我读过有关FOR...DO http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true命令开启Windows XP Professional Product Documentation - Using Batch Files http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true.
这是我的脚本:
@ECHO OFF
FOR /D %folder IN (%%rootFolderCmdLnParam) DO
FOR %zippedFile IN (*.zip) DO 7za.exe e %zippedFile
我想我还需要在调用 7za.exe e %zippedFile 进行文件提取之前更改实际目录,但我无法弄清楚这个批处理文件中的操作方式(通过我知道如何在命令行中操作,即使我知道它是相同的指令“cd”)。
EDIT #1
我已经收到了一些提示ServerFault https://serverfault.com/对于同样的问题。请参阅答案这个链接 https://serverfault.com/questions/149317/iterating-through-folders-and-files-in-batch-file.
但是,它是从根目录 (C:) 提取的,而不是从参数文件夹中给定的。
有人有主意吗?
EDIT #2
批处理脚本似乎无法充分处理包含空格字符的文件夹和文件名。谁能证实我的想法吗?
EDIT #3
我需要它是完全递归的,因为我不知道将使用它的目录结构。
编辑#4.a
有了@aphoria的解决方案,我就快到了!唯一的问题是它需要比方说File5.zip
,检索文件名仅得到File5
, 创建一个子文件夹File5
并提取File5.zip
to File5
子文件夹,然后,它想创建一个File5
子文件夹中Folder 1
,它应该在哪里创建File1
子文件夹,继续我的示例。
编辑#4.b
根据需要,这是当前的代码:
@echo off
setlocal enableextensions enabledelayedexpansion
rem
rem Display instructions when no parameter is given.
rem
if "%1" equ "" (
echo Syntaxe : od.bat ^<directory mask>^
echo Exemple : od.bat *
goto :Eof
)
rem
rem Setting the PATH environment variable for this batch file for accessing 7za.exe.
rem
path=c:\temp;%PATH%
rem
rem Removing quotes from the given command line parameter path.
rem
set root=%1
set root=%root:~%1
set root=%root:~0,-1%
rem Searching directory structure from root for subfolders and zipfiles, then extracting the zipfiles into a subfolder of the same name as the zipfile.
for /F "delims==" %%d in ('dir /ogne /ad /b /s %root%') do (
echo Traitement du dossier : "%%d"
for /F "delims==" %%f in ('dir /b "%%d\*.zip"') do (
rem Getting filename without extension.
set subfolder=~n%f
mkdir "%%d\%subfolder%"
rem Extracting zipfile content to the newly created folder.
7za.exe e "%%d\%%f" -o"%%d\%subfolder%"
)
)
:Eof
endlocal
有人有想法吗?
我的猜测是它一次挖掘一个目录层次结构。事情是这样的。考虑我们有一个Folder A
in Folder 1
(文件夹 1\文件夹 A),然后,它从Folder 1
通过Folder 5
,然后回到Folder 1\Folder A
,其中 %subfolder% 变量保留其最后一个值。
非常感谢任何人的帮助。