如何使用遞歸連接資料夾中的 CSV 檔案?你好,有人可以幫我修改這段程式碼嗎?我正在嘗試連接每個資料夾的 csv 檔案。 使用者必須選擇一個資料夾,所有符合條件的 csv 資料夾將根據資料夾名稱連接並儲存。 此方法使用戶在每次腳本運行時選擇一個資料夾。
但是,我現在想做的是讓使用者選擇主資料夾,然後運行腳本並連接每個子資料夾的資料(像前面的方法一樣,用資料夾名稱作為檔案名稱單獨保存它們)。
但透過這些程式碼,所有 csv 檔案都被連接成一個檔案。 你能幫忙修改和解釋遞歸是如何運作的嗎? 如何透過開啟主資料夾來單獨保存這些檔案(連接每個資料夾的資料)。 我正在使用 JMP 15。 謝謝
p = pick Directory("Select a Folder");<BR />//f = Files In Directory(p);
f = Files In Directory(p, recursive(1));
Dir = Word(-1, p, "/"); //get filename
saveDir = Substr( p, 1, Length( p ) - Length( Word( -1, p, "\/" ) ) - 1 ); //save file outside main folder
/* Removes the file names from the list f which are not CSV files */
for(j=1,j<=N Items(f),j++,
If (Substr(f[j],Length(f[j])-2,3)!= "CSV",
RemoveFrom(f,j,1);
j=j-1;
);
);
A = {}; /* creates a empty list for storing the names of each data table */
B = {};
C = {};
D = {};
k = {};
m = {};
/*Opens each one of the CSV files and deletes all unwanted rows */
for(j=1,j<=N Items(f),j++,
CSV = Open(Concat(p,f[j]), End of Field(comma), Strip Quotes(1), Labels(1),
Column Names Start(3), Data Starts(4), Columns(:MRR=Numeric));
colname = {};
colname = CSV<<GET column="" names="">20 & Contains(colname,"Weight")>50,
InsertInto(A,CSV);
CSV<<SELECT where="">1,
dt2 = dt1<<CONCATENATE>1,
dt3 = dt1<<CONCATENATE></CONCATENATE></CONCATENATE></SELECT></GET>自動化和腳本編寫回覆:如何使用遞歸連接資料夾中的 CSV 檔案?關於 recursive(1) 選項,您需要解釋什麼?
Names Default To Here(1);
Files In Directory("$SAMPLE_DATA", recursive(1));
如果沒有該選項,您將僅獲得資料夾中的第一級文件,而使用遞歸時,您還將獲得其中的子資料夾和文件。

突出顯示的部分僅存在,因為設定了 recursive(1) 。
關於您的腳本:您有多少層資料夾?它可能會改變嗎? 如果在找到 .csv 檔案之前存在多個級別,則檔案名稱應該是什麼?
回覆:如何使用遞歸連接資料夾中的 CSV 檔案?這是一個啟動腳本,它讀入所有遞歸檔案並根據它們所在的資料夾將它們連接到單獨的資料表中。
我沒有包含您所有的個人錯誤檢查等。您應該能夠修改我的範例以將這些內容放回程式碼中。
此程式碼存在結構差異。JSL 讀入一個文件,然後在讀入下一個文件之前將其連接起來。我盡可能多地檢查了,但仍然可能有問題。
names default to here(1);
p = pick Directory("Select a Folder");//f = Files In Directory(p);
f = Files In Directory(p, recursive(1));
for(i=nitems(f),i>=1,i--,
if(uppercase(word(-1,f[i],"."))!="CSV",
remove from(f,i,1)
)
);
// Create the Associative Array with all of the files, with their directories
// Place all data into a data table, and then move into an Associative Array which will give the
// folder(data table to concatenate to)
dt= new table("The Files", new column("File", character));
dt << add rows(nitems(f));
dt:file<<SET values=""></SET>回覆:如何使用遞歸連接資料夾中的 CSV 檔案?您好,我只有兩層資料夾(主資料夾),然後是包含 CSV 的多個資料夾。 以下是不使用遞歸的腳本輸出範例。 我想要做的是,當腳本運行時,僅選擇主資料夾,並且腳本將具有以下輸出。

回覆:如何使用遞歸連接資料夾中的 CSV 檔案?您可以使用帶有負索引的 Word() 輕鬆取得資料夾名稱。 下面的例子
Names Default To Here(1);
foldername = Word(-1, "F1/1.jmp", "/\"); //"1.jmp"
我很可能會建立一個關聯數組,其中鍵是資料夾名稱。 然後我將循環遍歷文件列表並檢查關聯數組中是否尚不存在該資料夾,然後將其添加到那裡並將其值初始化為空列表。 然後,您可以使用 insert into 將檔案路徑新增至該關聯陣列以修正鍵(使用完整路徑,以便更容易開啟檔案)。 查看完文件清單後,對關聯數組執行其他循環。 這次打開文件並將它們連接起來
回覆:如何使用遞歸連接資料夾中的 CSV 檔案?您好,我在這行程式碼中有一個問題,因為我仍在使用 JMP 15。 我如何將其轉換為 JMP 15(for 循環格式) 我對 JMP 15 中的 for 循環格式中的 {table} 是什麼感到有點困惑。 另外,我怎麼能相應地保存這個 CSV 檔案呢? 就我的例子來說,是按性別劃分的。 因此檔案名稱應以性別保存(Male.csv & Female.csv)
For Each( {table}, theArray << get keys,
stable = theArray << get value( table );
stable = Substitute( stable, ":", "-" );
stable = Substitute( stable, "/", "-" );
If( Try( Data Table( stable ) << get name, "" ) == "",
dt = New Table( theArray << get value( table ) ),
dt = Data Table( stable )
);
回覆:如何使用遞歸連接資料夾中的 CSV 檔案?我認為這應該有效:
arrayKeys = theArray << get keys;
For(i=1, i <=nitems( arrayKeys), i++,
table = arrayKeys[i];
stable = theArray << get value( table );
stable = Substitute( stable, ":", "-" );
stable = Substitute( stable, "/", "-" );
If( Try( Data Table( stable ) << get name, "" ) == "",
dt = New Table( theArray << get value( table ) ),
dt = Data Table( stable )
);