简体   繁体   English

在输出文件夹中创建输入子文件夹结构

[英]Creating input subfolder structure inside output folder

I have a batch script that: 我有一个批处理脚本,它:

  1. read input files from a folder 从文件夹中读取输入文件
  2. elaborate them 阐述他们
  3. store output files in another folder 将输出文件存储在另一个文件夹中

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%并指定相对源路径(为此,可以使用pushdpopd命令)。

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

相关问题 Nsis Delete文件夹和子文件夹-安装程序目录中 - Nsis delete folder & subfolder — inside installer directory 使用.bat文件循环浏览文件夹/子文件夹,并提供该文件夹/子文件夹作为exe的输入 - Use .bat file to loop through folders/subfolder and and provide the folder/subfolder as input to an exe 批处理文件以计算文件夹中的所有文件和子文件夹文件,并使用批处理文件以文件夹名称写入输出文件 - batch file to count all files in a folder and subfolder files and write output file in folder name using batch file 批量创建文件夹和子文件夹 - Create folder and subfolder in batch 将子文件夹的内容复制到另一个文件夹 - Copy contents of subfolder to another folder 文件夹及其子文件夹的权限列表 - List of permissions for a folder and it's subfolder 如何判断一个文件夹是否是特殊Windows文件夹的子文件夹? - How to tell if a folder is a subfolder of a special Windows folder? 如何判断文件夹是否是递归文件夹副本的子文件夹? - How to tell if folder is a subfolder for recursive folder copy? 如何将文件夹以及其中的项目复制到输出目录? - How to copy folder along with items inside to output directory? 输出子文件夹中的所有文件:绝对文件路径 - Output all files inside Sub Folder: Absolute File Path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM