打開文件(從文件名查找字符)全部,
我試圖通過根據用戶輸入搜索文件名字符來打開文件夾中的文件。
/* Input values in 3 columns by user. eg of file name: ABC_12_XYZ. User will input ABC in 1st column, 12 in second col and XYZ in 3rd column. all three should match in a row for selecting the file. There could be 100's of files (unique name) in the folder. User will input all that he wants to select from folder into input file. The file name search is limited to 10 characters from beginning of file name. The code is getting stuck somewhere in loop*/
//Script ------
Retrieve = Open( "C:\*\inputfile.jmp" );
x = Column( Retrieve, "Col1" ) << Get Values;
y = Column( Retrieve, "Col2" ) << Get Values;
z = Column( Retrieve, "Col3" ) << Get Values;
// location of files which needs to be read from a folder based on user inputs.
Set Default Directory("C:\test\");
Close( Retrieve, NoSave );
Col1_search = 1;
Col2_search = 1;
Count = 0;
For( i = 1, i <= N Items( files ), i++,
For( j = 1, j <= N Items( z ), j++,
If( Contains( files[i], z[j] ),
If( Col1_search == 1,
If( (Contains( files[i], z[j] ) & Contains( Left( files[i], 10 ), x[j] )) > 0,
If( Col2_Search == 1,
If(
((Contains( files[i], z[j] ) & Contains( Left( files[i], 10 ), x[j] )) > 0) & (Contains( Left( files[i], 10 ), y[j] ) > (Contains( Left( files[i], 10 ), x[j] ))),
Open( files[i] );
Count++;
),
Open( files[i] );
If( Count == N Items( z ),
Break();
);
)
)
),
Open( files[i] )
)
);
);
openDTs = {};回复:打開文件(從文件名查找字符)@烏代
我能夠弄清楚這個問題。 源是 18 個字符,模式是整個字符串。
我又添加了一個循環來對所有文件執行輸入文件查找,如下所示。
感謝您的快速回复!
謝謝!
為了(我= 1 ,我<= N 項(文件) ,我++,
為了( j = 1 , j <= N 項( X ) , j ++,
如果( !不見了(正則表達式(子字符串(文件[我] , 1 , 18 ) ,查爾( X [ j ] || “_” || y [ j ] || “_” || z [ j ] ) ) ) ,
打開(測試目錄|| “\” ||文件[我] ) ;
休息() ;
) ;,
)
) ;
回复:打開文件(從文件名查找字符)dt = Open( "$SAMPLE_DATA/Cities.jmp" ); // Open Sample Data
dt << Select Where(!IsMissing(Regex(Char(:city),"ALB")));
您不需要使用“包含”功能。 看上面的例子。 如果您只想搜索文件名的 18 個字符,則需要使用 Substr 函數提取
dt = Open( "$SAMPLE_DATA/Cities.jmp" ); // Open Sample Data
dt << Select Where(!IsMissing(Regex(Char(:city),Substr("ALBANY",1,3))));
回复:打開文件(從文件名查找字符)這是日誌的副本 -
在以下腳本中,錯誤標記為 /*###*/
For( i = 1, i <= N 個項目( 文件 ), i++,
如果(
!不見了(
正則表達式/*###*/(字符(
x|| /*###*/"_" || /*###*/y || /*###*/"_" || /*###*/z/*###*/
), 文件[i])
),
打開( TestDir || "\" || 文件[i] )
)
)
我使用 contains 的原因是從文件名中查找 18 個字符,該文件名可能有 40 個字符長。
我不確定我做得是否正確。
回复:打開文件(從文件名查找字符)@CG ,
你能分享一下錯誤是什麼嗎? 你的日誌的副本?
另外,我不明白你為什麼要添加 Contains ?
第二個參數應該是您要匹配的術語:
因此,如果您想查找您的名字,它會喜歡:
If (!IsMissing(Regex(Char(x || "_" || y || "_"||z),"CG")),
回复:打開文件(從文件名查找字符)我正在尋找文件名中的前 18 個字符
我修改了下面的循環並添加了條件來查找文件名 x_y_z1234 的 18 個字符(最多 z)
為了(我= 1 ,我<= N 項(文件) ,我++,
如果( !不見了(正則表達式(查爾( X || “_” || y || “_” || z ) ,包含(左邊(文件[我]) , 18 )) ,
打開(測試目錄|| “\” ||文件[我]) ;
) ,
) ;
) ;
但我仍然收到錯誤 -
For( i = 1, i <= N 個項目( 文件 ), i++,
如果/*###*/(缺少(
Regex( Char( x || "_" || y || "_" || z ), 包含( Left( 文件[i] ), 18 ) ),
打開( TestDir || "\" || 文件[i] )
))
)
回复:打開文件(從文件名查找字符)@CG ,
假設在您的情況下,文件格式始終為 ABC_12_XYZ (即 x_y_z) 是否公平? 如果是,另一種更簡單的方法可能是:
Retrieve = Open( "C:\*\inputfile.jmp" );
x = Column( Retrieve, "Col1" ) << Get Values;
y = Column( Retrieve, "Col2" ) << Get Values;
z = Column( Retrieve, "Col3" ) << Get Values;
Close(Retrieve,"No Save");
TestDir = "C:\test\";
Files = Files In Directory(TestDir); // Returns a list of files in the directory
for(i = 1, i <= N Items(Files), i++,
If(!IsMissing(Regex(Char(x || "_" || y || "_"||z),Files[i])),
Open(TestDir ||"\" || Files[i]);
);
);