Batch file 在最近的父目录';的名称中有一个特定的字符串
我正在尝试编写一个批处理脚本,将驻留在目录中的所有文本文件与包含特定字符串的名称递归地连接起来 在下面的示例中,标记为(x)的文件应该合并,因为它们最近的父目录的名称包含一个包含字符串的名称Batch file 在最近的父目录';的名称中有一个特定的字符串,batch-file,recursion,merge,Batch File,Recursion,Merge,我正在尝试编写一个批处理脚本,将驻留在目录中的所有文本文件与包含特定字符串的名称递归地连接起来 在下面的示例中,标记为(x)的文件应该合并,因为它们最近的父目录的名称包含一个包含字符串的名称 Directory |-- STRINGdirectory2 | |-- textfile.txt (x) | |-- anotherfile.txt (x) |-- anotherdirectory2 | |-- textfile.txt |-- directory3
Directory
|-- STRINGdirectory2
| |-- textfile.txt (x)
| |-- anotherfile.txt (x)
|-- anotherdirectory2
| |-- textfile.txt
|-- directory3
| |-- STRINGdirectory3
| | |-- file.txt (x)
如何做到这一点?给你:
@echo off
setlocal
SET tempfile=%temp%\%RANDOM%
if '%1' == '' goto usage
if '%2' == '' goto usage
if '%3' == '' goto usage
if exist %3 del %3
dir /S /B | find %2 > %tempfile%
if '%4' == 'test' goto dryrun
for /F %%i in (temp.txt) do type %%i >> "%3"
goto end
:dryrun
type %tempfile%
goto end
:usage
echo Usage %0 ^<dir^> ^<pattern^> ^<destination^> [test]
echo Concatente all files inside ^<dir^> whose names have the substring ^<pattern^> into ^<destination^>
echo Specify 'test' optionally the fourth argument for a dry run which will merely list the files
:end
if exist %tempfile% del %tempfile%
@echo关闭
setlocal
设置tempfile=%temp%\%RANDOM%
如果“%1”==“转到使用
如果“%2”==“转到使用
如果“%3”==“转到使用
如果存在%3删除%3
目录/S/B |查找%2>%tempfile%
如果“%4”==“测试”转到干运行
对于/F%%i in(temp.txt),请键入%%i>%3
转到终点
:干运行
类型%tempfile%
转到终点
:用法
回显使用率%0^^[测试]
echo Concatente^中名称包含子字符串^in的所有文件^
echo可以选择指定'test'第四个参数,用于只列出文件的试运行
:结束
如果存在%tempfile%del%tempfile%
给你:
@echo off
setlocal
SET tempfile=%temp%\%RANDOM%
if '%1' == '' goto usage
if '%2' == '' goto usage
if '%3' == '' goto usage
if exist %3 del %3
dir /S /B | find %2 > %tempfile%
if '%4' == 'test' goto dryrun
for /F %%i in (temp.txt) do type %%i >> "%3"
goto end
:dryrun
type %tempfile%
goto end
:usage
echo Usage %0 ^<dir^> ^<pattern^> ^<destination^> [test]
echo Concatente all files inside ^<dir^> whose names have the substring ^<pattern^> into ^<destination^>
echo Specify 'test' optionally the fourth argument for a dry run which will merely list the files
:end
if exist %tempfile% del %tempfile%
@echo关闭
setlocal
设置tempfile=%temp%\%RANDOM%
如果“%1”==“转到使用
如果“%2”==“转到使用
如果“%3”==“转到使用
如果存在%3删除%3
目录/S/B |查找%2>%tempfile%
如果“%4”==“测试”转到干运行
对于/F%%i in(temp.txt),请键入%%i>%3
转到终点
:干运行
类型%tempfile%
转到终点
:用法
回显使用率%0^^[测试]
echo Concatente^中名称包含子字符串^in的所有文件^
echo可以选择指定'test'第四个参数,用于只列出文件的试运行
:结束
如果存在%tempfile%del%tempfile%
@echo off
setlocal enableextensions disabledelayedexpansion
设置“root=c:\where\Directory”
(对于(“*字符串*”)中的/r“%root%”/d%%a,请执行以下操作(
对于(“%%~fa\*.txt”)中的%%b,请执行以下操作(
键入“%%~fb”
回音(
)
)
)>%root%\concatenated.txt“2>nul
给定根文件夹,对于其名称中包含指定字符串的每个文件夹,对于文件夹中的每个txt文件,键入该文件
输出的完整被重定向到一个文件以保存“连接”的数据。@echo关闭
setlocal enableextensions disabledelayedexpansion
设置“root=c:\where\Directory”
(对于(“*字符串*”)中的/r“%root%”/d%%a,请执行以下操作(
对于(“%%~fa\*.txt”)中的%%b,请执行以下操作(
键入“%%~fb”
回音(
)
)
)>%root%\concatenated.txt“2>nul
给定根文件夹,对于其名称中包含指定字符串的每个文件夹,对于文件夹中的每个txt文件,键入该文件
的完整输出被重定向到一个文件以保存“连接”的数据。正确处理文件中最后一行可能没有行终止符(\n或\r\n)的可能性。如果他没有回音(,则如果没有行终止符,则可以将文件的第一行追加到上一个文件的最后一行。MC ND答案的唯一问题是,即使不需要,也可以在文件之间添加额外的空行
如果您知道所有文件的末尾都有一个行终止符,那么有一个非常简单的解决方案,它依赖于undocumented
如果您知道某些文件可能缺少最后一行终止符,并且您认为某些文件可能使用\r\n而不是\r\n,那么您可以使用my仅在需要时有条件地附加\r\n。JREPL.BAT是一种混合JScript/批处理脚本,从XP开始在任何Windows计算机上本机运行
@echo off
pushd "c:\your\root\path"
>concatenated.txt 2>nul (
for /r /d %%D in (*string*) do for %%F in ("%%D\*.txt) do (
jrepl "$(?~\r?\n)" "\r\n" /m /x /f "%%F"
)
)
这可以正确处理文件中最后一行可能没有行终止符(\n或\r\n)的可能性。如果他没有回音(,则如果没有行终止符,则可以将文件的第一行追加到上一个文件的最后一行。MC ND答案的唯一问题是,即使不需要,也可以在文件之间添加额外的空行
如果您知道所有文件的末尾都有一个行终止符,那么有一个非常简单的解决方案,它依赖于undocumented
如果您知道某些文件可能缺少最后一行终止符,并且您认为某些文件可能使用\r\n而不是\r\n,那么您可以使用my仅在需要时有条件地附加\r\n。JREPL.BAT是一种混合JScript/批处理脚本,从XP开始在任何Windows计算机上本机运行
@echo off
pushd "c:\your\root\path"
>concatenated.txt 2>nul (
for /r /d %%D in (*string*) do for %%F in ("%%D\*.txt) do (
jrepl "$(?~\r?\n)" "\r\n" /m /x /f "%%F"
)
)
你说的合并是什么意思?你是说把它们放在一个目录中还是连接起来?@rep_movsd我想连接它们。我会澄清的。你说的合并是什么意思?你是说把它们放在一个目录中还是连接起来?@rep_movsd我想连接它们。我会澄清的。太好了!我如何在每次添加之后添加一个新行选项?不应该设置为root=c:\somewhere\Directory而不是设置“root=c:\somewhere\Directory”@rep\u movsd-两者都可以工作,但是引用的版本更好,因为它可以防止无意中的尾随空格进入值中。很好!您甚至有了echo(
要在文件之间添加换行符,以防最后一行丢失。\r\n唯一的改进是有条件地添加,但这对于纯批处理来说是很困难的。My可以很好地工作-将TYPE替换为jrepl“$(?!\r?\n)”“\r\n”/m/x/f”%%~fb”
太好了!如何在每次添加后添加换行符?是否应该设置为root=c:\somewhere\Directory而不是设置“root=c:\somewhere\Directory”@rep\u movsd-两者都可以工作,但引用的版本更好,因为它可以防止无意中的尾随空格进入值中。很好!您甚至还有echo(
要在文件之间添加换行符,以防最后一行丢失。\r\n唯一的改进是有条件地添加,但这对于纯批处理来说是很困难的。My会很好地工作-将TYPE替换为jrepl“$(?!\r?\n)”“\r\n”/m/x/f“%%~fb”
在