从具有相同结构的文件夹中提取并连接 csv 文件<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />你好,
例如,我在不同的文件夹中有一大堆具有共同名称的 csv 文件,并且 csv 文件具有相同的结构
<第一个文件夹>dfgdwgssd
<具有通用名称的第二个文件夹> commonname
xxx-通用名称.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 对超过 1,300 个文件夹执行此操作(提取和连接 csv 文件)的快速检查花了我 3 秒,而连接的 csv 文件为 13MB,3 秒! 我需要研究在 JSL 中运行 bash 命令的可能性。
顺便提一句,
dt = Open( dirpath || TheFileList,invisible );
处理时不会弹出数据表
再次感谢你!
回复:从具有相同结构的文件夹中提取并连接 csv 文件你好杰威,
除了吉姆的建议之外,您还可以尝试以下通用方法来选择所需的文件。 请参阅下面的示例:
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. 检索“我的文档”文件夹及其子文件夹中的所有文件名。 (替换为您的顶级文件夹路径)。
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 );
);
);