通过批处理文件夹和文件进行遍历? [英] Iterating through folders and files in batch file?
问题描述
下面是我的处境。一期工程已客观一些附件迁移到另一个系统。
Here's my situation. A project has as objective to migrate some attachments to another system.
这些附件将位于父文件夹,让我们说文件夹0
(见<一href=\"http://serverfault.com/questions/147902/windows-command-line-built-in-com$p$pssion-extraction-tool\">this为了更好地理解问题的图),它们将被压缩/ COM pressed。
These attachments will be located to a parent folder, let's say "Folder 0
" (see this question's diagram for better understanding), and they will be zipped/compressed.
我想我的批处理脚本被称为像这样:
I want my batch script to be called like so:
BatchScript.bat "c:\temp\usd\Folder 0"
我用 7za.exe
作为命令行提取工具。
我要什么我的批处理脚本做的是通过文件夹0
的子文件夹进行迭代,并提取所有包含ZIP文件到各自的文件夹中。
What I want my batch script to do is to iterate through the "Folder 0
"'s subfolders, and extract all of the containing ZIP files into their respective folder.
这是强制性的解压缩的文件都在同一个文件夹中各自的ZIP文件。因此,包含在文件文件1.zip
被需要文件夹1
等字样。
It is obligatory that the files extracted are in the same folder as their respective ZIP files. So, files contained in "File 1.zip
" are needed in "Folder 1
" and so forth.
我已阅读关于<一个href=\"http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true\"><$c$c>FOR...DO$c$c>在<指令href=\"http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true\"><$c$c>Windows XP专业版产品文档 - 使用批处理文件
I have read about the FOR...DO
command on Windows XP Professional Product Documentation - Using Batch Files
.
下面是我的脚本:
@ECHO OFF
FOR /D %folder IN (%%rootFolderCmdLnParam) DO
FOR %zippedFile IN (*.zip) DO 7za.exe e %zippedFile
我想我还需要调用7za.exeË%zippedFile文件提取前改变实际的目录,但我不能如何在此批处理文件中找出(通过我知道在命令行如何,甚至如果我知道这是相同的指令CD)。
I guess that I would also need to change the actual directory before calling 7za.exe e %zippedFile for file extraction, but I can't figure out how in this batch file (through I know how in command line, and even if I know it is the same instruction "cd").
编辑#1
我已经收到 ServerFault
一些提示以同样的问题。请参阅相关回答<一个href=\"http://serverfault.com/questions/149317/iterating-through-folders-and-files-in-batch-file\">this链接。
然而,从根(C :)萃取,而不是来自于参数文件夹中的给定
However, it extracted from the root (C:), and not from the given in parameter folder.
任何人有一个想法?
编辑#2
似乎批处理脚本不处理包含空格字符的文件夹充分和文件名。任何人都可以证实我在想什么?
It seems that batch script doesn't handle folder and file names containing a space character adequately. Can anyone confirm what I think?
编辑#3
我需要它完全递归的,因为我不知道针对此将要使用的目录结构。
I need it to be fully recursive, since I don't know the directory structure against which this will be used.
编辑#4.A
使用@ aphoria的解决方案,我几乎没有!唯一的问题是,它需要让我们说 File5.zip
,检索文件名只拿到 File5
,创建一个子文件夹 File5
并提取 File5.zip
到 File5
子文件夹,那么,它要创建文件夹1
,它应该不是要创建<$ C $一个 File5
子文件夹C>文件1 子文件夹,坚持我的例子。
With @aphoria's solution, I'm almost there! The only problem is that it takes let's say
File5.zip
, retrieve the filename only to getFile5
, creates a subfolderFile5
and extract theFile5.zip
toFile5
subfolder, then, it wants to create aFile5
subfolder inFolder 1
, where it should instead want to createFile1
subfolder, to stick with my example.
编辑#4.B
根据需要,这里的code,因为它看起来目前:
As required, here's the code as it currently look:
@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
想法吗?
的我的猜测是,它挖一个目录层次结构在一个时间。这里的交易。考虑到我们有一个文件夹A
在文件夹1
(文件夹1 \\文件夹A),然后,它会搜索文件夹1
到文件夹5
,回来到文件夹1 \\文件夹A
,其中%子%变量棒与最后的值。的
My guess is that it digs one directory hierarchy at a time. Here's the deal. Consider we have a Folder A
in Folder 1
(Folder 1\Folder A), then, it searches from Folder 1
through Folder 5
, and comes back to Folder 1\Folder A
, where the %subfolder% variable sticks with its last value.
任何人的帮助深表AP preciated。
Anyone's help is gratefully appreciated.
推荐答案
我不是很熟悉的7zip的命令行选项,所以你需要弄清楚的,准确的命令,但下面的脚本将一个完全指定的路径(允许空格),并打印出的文件夹名称,其中包含的.zip文件。
I'm not very familiar with the 7zip command-line options, so you will need to figure out the exact command for that, but the script below will take a fully specified path (spaces allowed) and print out the the folder name and .zip files contained within it.
@ECHO OFF
REM
REM Remove the double quotes from the front and end of the root path
REM
SET ROOT=%1
SET ROOT=%ROOT:~1%
SET ROOT=%ROOT:~0,-1%
ECHO %ROOT%
FOR /F "DELIMS==" %%d in ('DIR "%ROOT%" /AD /B') DO (
ECHO %%d
FOR /F "DELIMS==" %%f in ('DIR "%ROOT%\%%d\*.zip" /B') DO (
ECHO %%f
)
)
结果
运行这样的:
Run it like this:
MyScript.CMDC:\\ TEMP \\ USD \\文件夹0
结果
结果
你应该得到类似这样的输出:
MyScript.CMD "c:\temp\usd\Folder 0"
You should get output similar to this:
Folder A
File 1.zip
File 2.zip
Folder B
File 1.zip
File 2.zip
结果
在code下面将提取文件夹\\文件1.zip
来一个新的文件夹文件夹\\文件1
。
The code below will extract Folder A\File 1.zip
to a new folder Folder A\File 1
.
有几件事情要记住:
- 在第一个
的
循环,你需要有%ROOT%
用双引号引起来处理与文件夹空间中的名称。 - 既然你
SET
廷第二次内部变量的
,你需要把SETLOCAL ENABLEDELAYEDEXPANSION
开头。然后,使用引用变量! (例如,!子!
)来强制在运行时扩展。 - 这一行的code的
设置子=〜N%F
应该是这样的设置子= %%〜NF
- 我把
ECHO
在MKDIR
和7za.exe $前C $ C>命令来测试。一旦你确定它是做你想要的东西,删除
ECHO
语句。
- In the first
FOR
loop, you need to have%ROOT%
enclosed in double quotes to handle folders with spaces in the name. - Since you're
SET
ting a variable inside the secondFOR
, you need to putSETLOCAL ENABLEDELAYEDEXPANSION
at the beginning. Then, reference the variable using ! (for example,!subfolder!
) to force expansion at runtime. - This line of your code
set subfolder=~n%f
should be thisset subfolder=%%~nf
- I put
ECHO
in front of theMKDIR
and7za.exe
commands to test. Once you are sure it is doing what you want, remove theECHO
statement.
下面是code:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM
REM Remove the double quotes from the front and end of the root path
REM
SET ROOT=%1
SET ROOT=%ROOT:~1%
SET ROOT=%ROOT:~0,-1%
ECHO %ROOT%
REM Searching directory structure from root for subfolders and zipfiles,
REM 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=%%~nf
ECHO mkdir "%%d\!subfolder!"
REM Extracting zipfile content to the newly created folder.
ECHO 7za.exe e "%%d\%%f" -o"%%d\!subfolder!"
)
)
ENDLOCAL
这篇关于通过批处理文件夹和文件进行遍历?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!