从具有相同结构的文件夹中提取并连接 csv 文件<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />你好,
例如,我在具有通用名称的不同文件夹中有一大堆 csv 文件,并且 csv 文件具有相同的结构
<第一个文件夹>dfgdwgssd
<具有通用名称的第二个文件夹> commonname
xxx-commonname.csv
我正在考虑使用脚本来提取和连接所有 csv 文件。 我找到了一种使用 bash 命令(“for”循环和“find”)的简单方法,有人可以帮帮我吗? 太感谢了!
回复:从具有相同结构的文件夹中提取并连接 csv 文件再次感谢你,菲尔!
我使用的是 Mac,刚刚在 Google 上搜索,发现很少有关于从 JMP 运行 shell 的信息,你有一些例子吗?
回复:从具有相同结构的文件夹中提取并连接 csv 文件当然是我的荣幸! 我试图指出您仍然可以使用 JMP 检索整个文件列表,然后使用 REGEX 搜索包含 的文件名。 事实上,我可以想象使用 1300 多个文件夹可能需要一些时间。
关于速度的几点建议:
- 打印到日志会减慢速度。
- 另外,我会先打开所有文件,然后再连接和关闭每个文件。
*如果您使用的是 Mac,您可以从 JMP 启动 shell 脚本。 在 Windows 上,除非您使用 Cygwin 或类似的工具,否则 Powershell 是编写可从 JMP 调用的实用程序的不错选择。
菲尔
回复:从具有相同结构的文件夹中提取并连接 csv 文件嗨菲尔,
非常感谢您的提示! 我没有运行你的脚本。
我正在使用少量文件夹进行尝试,后来意识到大量文件夹(比如> 1,300)需要很长时间,因为JMP将转到第二个目录并提取* .csv文件等等,非常非常时间 -虽然消耗。
为了提高效率,我考虑过使用 bash 将所有 csv 文件移动到一个文件夹中,然后将它们连接起来。 快速检查使用 bash 执行此操作(提取和连接 csv 文件)与 >1,300 个文件夹花了我 3 秒,连接 csv 文件为 13MB,3 秒!!! 我需要研究在 JSL 中运行 bash 命令的可能性。
顺便提一句,
dt = Open( dirpath || TheFileList,invisible );
处理过程中不会弹出数据表
再次感谢你!
回复:从具有相同结构的文件夹中提取并连接 csv 文件你好jyw ,
除了 Jim 的建议之外,您还可以尝试以下通用方法来选择您想要的文件。 请参见下面的示例:
allFiles = FilesInDirectory("$Documents", recursive);
selectedFiles = {};
commonName = "BrownP";
For( i = 1, i<=NItems(allFiles), i++,
If( !IsMissing( Regex(allFiles, EvalInsert(".*<COMMONNAME>.*\.csv","<",">" )) ),
InsertInto(selectedFiles, allFiles);
)
);</COMMONNAME>
1. 检索 My Documents 文件夹及其子文件夹中的所有文件名。 (替换为您的顶级文件夹路径)。
2. 创建一个空列表来存储您想要的文件名。
3. 定义 commonName 字符串。
4. 遍历所有文件名,只选择与模式匹配的文件名并存储在选定文件列表。 在这个例子中我正在检索文件名中包含“BrownP”的所有 CSV 文件。 (替换为您自己的 commonName 字符串)。
因此,完整的实现(使用上一篇文章中 Jim 的代码):
Names Default To Here( 1 );
// Setup an empty table to start to concatenate other tables to
dtbase = New Table( "Base Table" );
// Have the user select the starting folder
dirpath = Pick Directory( "Pick the Top Level Directory" );
// Get all of the files in all of the folders that are under the picked folder
TheFileList = Files In Directory( dirpath, "recursive" );
// Define common string to search for
commonName = "file";
// Go through all of the files and read in append those that meet your
// requirement. You will need to amend the IF statement to meet your criteria
For( i = 1, i <= N Items( TheFileList ), i++,
If( !Is Missing( Regex( TheFileList, Eval Insert( ".*<COMMONNAME>.*\.csv", "<", ">" ) ) ),
dt = Open( dirpath || TheFileList );
dtbase = dtbase << concatenate( dt, append to first table( 1 ) );
Close( dt, nosave );
)
);</COMMONNAME>
希望这可以帮助!
最好的,
菲尔
回复:从具有相同结构的文件夹中提取并连接 csv 文件
Names Default To Here( 1 );
// Setup an empty table to start to concatenate other tables to
dtbase = New Table( "Base Table" );
// Have the user select the starting folder
dirpath = Pick Directory( "Pick the Top Level Directory" );
// Get all of the files in all of the folders that are under the picked folder
TheFileList = Files In Directory( dirpath, "recursive" );
// Go through all of the files and read in append those that meet your
// requirement. You will need to amend the IF statement to meet your criteria
For( i = 1, i <= N Items( TheFileList ), i++,
If( Uppercase( Word( -1, TheFileList[i], "." ) ) == "CSV" & Is File( dirpath || TheFileList[i] ) == 1,
If( Contains ( TheFileList[i], "<2nd folder common name"),
If( Contains (TheFileList[i],"<COMMON name="" of="" the="" csvfile="">"),
print(TheFileList[i]);
dt = Open( dirpath || TheFileList[i] );
dtbase = dtbase << concatenate( dt, append to first table( 1 ) );
Close( dt, nosave );
);
);
);
);</COMMON>
再添加两行以使其正常工作,但需要调整对齐/缩进。
谢谢!
回复:从具有相同结构的文件夹中提取并连接 csv 文件是的,我确实知道第二层和 csv 文件的“通用名称”是什么。 谢谢!
回复:从具有相同结构的文件夹中提取并连接 csv 文件您是否提前知道“通用名称”是什么?或者你需要让脚本弄清楚吗?在这种情况下,文件夹中是否会出现多个可能的通用名称?
回复:从具有相同结构的文件夹中提取并连接 csv 文件嗨,吉姆,
我让它工作了,但它列出了所有 CSV 文件,我正在尝试找到整理或指定第二层的方法。你能多给点建议吗?
谢谢!
回复:从具有相同结构的文件夹中提取并连接 csv 文件这是一个可以为您提供良好开端的脚本:
Names Default To Here( 1 );
// Setup an empty table to start to concatenate other tables to
dtbase = New Table( "Base Table" );
// Have the user select the starting folder
dirpath = Pick Directory( "Pick the Top Level Directory" );
// Get all of the files in all of the folders that are under the picked folder
TheFileList = Files In Directory( dirpath, "recursive" );
// Go through all of the files and read in append those that meet your
// requirement. You will need to amend the IF statement to meet your criteria
For( i = 1, i <= N Items( TheFileList ), i++,
If( Uppercase( Word( -1, TheFileList[i], "." ) ) == "CSV" & Is File( dirpath || TheFileList[i] ) == 1,
dt = Open( dirpath || TheFileList[i] );
dtbase = dtbase << concatenate( dt, append to first table( 1 ) );
Close( dt, nosave );
);
);