[英]Creating input subfolder structure inside output folder
I have a batch script that: 我有一个批处理脚本,它:
Example code: 示例代码:
set pathTmp=D:\a\b\c
set pathIn=%pathTmp%\in
set pathOut=%pathTmp%\out
for /f %%i in ('dir /b %pathIn%') do (
java XXX.jar %pathIn%\%%i >> %pathOut%\%%i
)
Now I'd like to modify it to read files from all subfolders of pathIn
and put the output file in the same subfolder but under pathOut
. 现在,我想对其进行修改,以从pathIn
所有子文件夹中读取文件,并将输出文件放在相同的子文件夹中, 但位于pathOut
下。
Example : if input file is in pathIn\\zzz
, the output file must be in pathOut\\zzz
. 示例 :如果输入文件位于pathIn\\zzz
,则输出文件必须位于pathOut\\zzz
。
How can I recreate the input subfolder structure inside output folder? 如何在输出文件夹中重新创建输入子文件夹结构?
I would use xcopy
together with the /L
switch (to list files that would be copied) to retrieve the relative paths. 我将xcopy
与/L
开关一起使用(列出要复制的文件)以检索相对路径。 For this to work, you need to change to the directory %pathIn%
first and specify a relative source path (for this purpose, the commands pushd
and popd
can be used). 为此,您需要首先切换到目录%pathIn%
并指定相对源路径(为此,可以使用pushd
和popd
命令)。
For example, when the current working directory is D:\\a\\b\\c\\in
and its content is...: 例如,当当前工作目录为D:\\a\\b\\c\\in
并且其内容为...时:
D:\\a\\b\\c\\in | data.bin +---subdir1 | sample.txt | sample.xml \\---subdir2 anything.txt
...the command line xcopy /L /I /S /E "." "D:\\a\\b\\c\\out"
...命令行xcopy /L /I /S /E "." "D:\\a\\b\\c\\out"
xcopy /L /I /S /E "." "D:\\a\\b\\c\\out"
would return: xcopy /L /I /S /E "." "D:\\a\\b\\c\\out"
将返回:
.\\data.bin .\\subdir1\\sample.txt .\\subdir1\\sample.xml .\\subdir2\\anything.txt 3 File(s)
As you can see there are paths relative to the current directory. 如您所见,存在相对于当前目录的路径。 To get rid of the summary line 3 File(s)
, the find ".\\"
command line is used to return only those lines containing .\\
. 要摆脱摘要行3 File(s)
,可以使用find ".\\"
命令行仅返回包含.\\
那些行。
So here is the modified script: 因此,这是修改后的脚本:
set "pathTmp=D:\a\b\c"
set "pathIn=%pathTmp%\in"
set "pathOut=%pathTmp%\out"
pushd "%pathIn%"
for /F "delims=" %%I in ('xcopy /L /I /S /E "." "%pathOut%" ^| find ".\"') do (
md "%pathOut%\%%I\.." > nul 2>&1
java "XXX.jar" "%%I" > "%pathOut%\%%I"
)
popd
Additionally, I placed md "%pathOut%\\%%I\\.." > nul 2>&1
before the java
command line so that the directory is created in advance, not sure if this is needed though. 另外,我在java
命令行之前放置了md "%pathOut%\\%%I\\.." > nul 2>&1
,以便预先创建目录,但是不确定是否需要。 The redirection > nul 2>&1
avoids any output, including error messages, to be displayed. 重定向> nul 2>&1
避免显示任何输出,包括错误消息。
I put quotation marks around all paths in order to avoid trouble with white-spaces or any special characters in them. 为了避免空格或空格中的任何特殊字符,我在所有路径周围都加上了引号。 I also quoted the assignment expressions in the set
command lines. 我还在set
命令行中引用了赋值表达式。
You need to specify the option string "delims="
in the for /F
command line, because the default options tokens=1
and delims=
TAB SPACE would split your paths unintentionally at the first white-space. 您需要在for /F
命令行中指定选项字符串"delims="
,因为默认选项tokens=1
和delims=
TAB SPACE会在第一个空白处无意间分割路径。
Note that the redirection operator >>
means to append to a file if it already exists. 请注意,重定向运算符>>
表示要附加到文件(如果已存在)。 To overwrite, use the >
operator (which I used). 要覆盖,请使用>
运算符(我曾经使用过)。
You could do something like this: 您可以执行以下操作:
@setlocal EnableDelayedExpansion
@echo off
set pathTmp=D:\a\b\c
set pathIn=%pathTmp%\in
set pathOut=%pathTmp%\out
REM set inLength=ADD FUNCTION TO CALCULATE LENGTH OF PATHIN
for /f %%i in ('dir /b /s %pathIn%') do (
set var=%%i
java XXX.jar %%i >> %pathOut%\!var:~%inLength%!
)
This will strip the length of the pathIn
directory from the absolute path leaving only the relative path. 这将从绝对路径中pathIn
目录的长度,仅保留相对路径。 Then it appends the relative path onto the pathOut
var 然后将相对路径附加到pathOut
var
You would need to find or write a function to get the length of the the pathIn string. 您将需要查找或编写函数以获取pathIn字符串的长度。 Check out some solutions here . 在此处查看一些解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.