打开文件(从文件名查找字符)全部,
我试图通过根据用户输入搜索文件名字符来打开文件夹中的文件。
/* 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]);
);
);