Batch file 批量分析字符串

Batch file 批量分析字符串,batch-file,directory,Batch File,Directory,我有一个输入目录和一个输出目录 我的主要目标是将输入目录中具有特定扩展名的所有文件读入数组 然后我希望在输入目录下的输出目录下创建每个文件的目录树 例如: Input目录=C:\Input 输出目录=C:\Output 输入目录树: set k=0 for /f "eol=: delims=" %%F in ('dir /b /s %InputDir%\*.%StandardExtension%') do ( set /a k+=1 set f

我有一个输入目录和一个输出目录

我的主要目标是将输入目录中具有特定扩展名的所有文件读入数组

然后我希望在输入目录下的输出目录下创建每个文件的目录树

例如:

Input目录=
C:\Input

输出目录=
C:\Output

输入目录树:

    set k=0
    for /f "eol=: delims=" %%F in ('dir /b /s %InputDir%\*.%StandardExtension%') do (
        set /a k+=1  
        set filesArray[!k!]=%%F 
    )
    set n=%k%
for /l %%i in (1,1,%n%) do (
    set CurrentFile=!filesArray[%%i]!
    call :CheckAndCreateDirectory %SequencesDir% !CurrentFile! %OutputDir% UpdatedOutputDir
)
:CheckAndCreateDirectory
set IntputDir=%1
set CurrentDir=%~dp2
set OutputDir=%3

set UpdatedOutputDir=!CurrentDir:%IntputDir%=%OutputDir%!
set %4=%UpdatedOutputDir%

if not exist %UpdatedOutputDir% (^
echo. & ^
mkdir %UpdatedOutputDir% & ^
echo.)
C:\Input\1\first.bat

C:\Input\2\3\second.bat

C:\Input\2\3\4\third.bat

C:\Input\3\1\forth.bat

我希望现在没有文件和文件夹的输出树如下所示:

输出目录树:

    set k=0
    for /f "eol=: delims=" %%F in ('dir /b /s %InputDir%\*.%StandardExtension%') do (
        set /a k+=1  
        set filesArray[!k!]=%%F 
    )
    set n=%k%
for /l %%i in (1,1,%n%) do (
    set CurrentFile=!filesArray[%%i]!
    call :CheckAndCreateDirectory %SequencesDir% !CurrentFile! %OutputDir% UpdatedOutputDir
)
:CheckAndCreateDirectory
set IntputDir=%1
set CurrentDir=%~dp2
set OutputDir=%3

set UpdatedOutputDir=!CurrentDir:%IntputDir%=%OutputDir%!
set %4=%UpdatedOutputDir%

if not exist %UpdatedOutputDir% (^
echo. & ^
mkdir %UpdatedOutputDir% & ^
echo.)
C:\Output\1\

C:\Output\2\3\

C:\Output\2\3\4\

C:\Output\3\1\

我有以下代码:

用于创建文件数组:

    set k=0
    for /f "eol=: delims=" %%F in ('dir /b /s %InputDir%\*.%StandardExtension%') do (
        set /a k+=1  
        set filesArray[!k!]=%%F 
    )
    set n=%k%
for /l %%i in (1,1,%n%) do (
    set CurrentFile=!filesArray[%%i]!
    call :CheckAndCreateDirectory %SequencesDir% !CurrentFile! %OutputDir% UpdatedOutputDir
)
:CheckAndCreateDirectory
set IntputDir=%1
set CurrentDir=%~dp2
set OutputDir=%3

set UpdatedOutputDir=!CurrentDir:%IntputDir%=%OutputDir%!
set %4=%UpdatedOutputDir%

if not exist %UpdatedOutputDir% (^
echo. & ^
mkdir %UpdatedOutputDir% & ^
echo.)
为每个文件创建目录:

    set k=0
    for /f "eol=: delims=" %%F in ('dir /b /s %InputDir%\*.%StandardExtension%') do (
        set /a k+=1  
        set filesArray[!k!]=%%F 
    )
    set n=%k%
for /l %%i in (1,1,%n%) do (
    set CurrentFile=!filesArray[%%i]!
    call :CheckAndCreateDirectory %SequencesDir% !CurrentFile! %OutputDir% UpdatedOutputDir
)
:CheckAndCreateDirectory
set IntputDir=%1
set CurrentDir=%~dp2
set OutputDir=%3

set UpdatedOutputDir=!CurrentDir:%IntputDir%=%OutputDir%!
set %4=%UpdatedOutputDir%

if not exist %UpdatedOutputDir% (^
echo. & ^
mkdir %UpdatedOutputDir% & ^
echo.)
最后,创建目录的函数:

    set k=0
    for /f "eol=: delims=" %%F in ('dir /b /s %InputDir%\*.%StandardExtension%') do (
        set /a k+=1  
        set filesArray[!k!]=%%F 
    )
    set n=%k%
for /l %%i in (1,1,%n%) do (
    set CurrentFile=!filesArray[%%i]!
    call :CheckAndCreateDirectory %SequencesDir% !CurrentFile! %OutputDir% UpdatedOutputDir
)
:CheckAndCreateDirectory
set IntputDir=%1
set CurrentDir=%~dp2
set OutputDir=%3

set UpdatedOutputDir=!CurrentDir:%IntputDir%=%OutputDir%!
set %4=%UpdatedOutputDir%

if not exist %UpdatedOutputDir% (^
echo. & ^
mkdir %UpdatedOutputDir% & ^
echo.)
事情是这样的: 如果输入(我猜输出也是如此)不包含任何“特殊”字符,如
&
等,代码就可以工作。 但是,如果是,则不会创建目录树

工作输入:
C:\input\u Dir

不工作输入:
C:\input&Dir


有什么想法吗?

我想我有一个更简单的解决方案:

xcopy /T c:\input c:\ouptut
一些细节:

/不要创建文件夹结构,但不要复制文件。不要 包括空文件夹或子文件夹。 /T/E将包括空文件夹和子文件夹


Kaysax的解决方案看起来不错,但要回答您最初的问题:
始终使用延迟扩展。
试着把你的论点用引号括起来,比如

mkdir "!UpdatedOutputDir!"
关键是,延迟扩展可以毫无问题地处理所有特殊字符

剩下的唯一问题是在循环中,因为您可能会丢失感叹号
在文件或路径名中,因为FOR循环参数在延迟扩展occours之前展开。

要解决这个问题,需要付出更多的努力。

Kayasax为您提供了更好的解决方案。代码失败的原因是该特殊字符既没有转义也没有引用。最简单的解决方法是引用各种路径。好的,首先,非常感谢,这就解决了问题。现在我有另一个问题。我正在尝试为每个文件创建一个新的文件路径,因此,对于:
C:\input\file.exe
我希望创建:
C:\output\file.new.bat
我该怎么做?这里有一种方法,但不支持递归性:
copy C:\input\*.exe C:\output\*.new.bat