亞當,
我不確定這是否符合您的情況,但幾年前我遇到過類似的問題。在文件服務器上,每天都會創建一個新目錄,該文件夾包含當天的文件。 問題是時間不一致。 文件夾(目錄)名稱的時間為當天第一個文件到達的日期時間。 因此,具體時間不得而知。
最簡單的 JSL 命令是使用folders = 目錄中的文件(“路徑”)並且不要使用遞歸。 然後使用 JSL 搜索以今天日期開頭或包含今天日期的文件夾。這工作得很好,但是,服務器有“無數”的文件夾,而且性能非常慢。我使用的是 Windows 操作系統,所以我使用帶有 cmd.exe 的運行程序來運行自定義目錄命令。 例如,這個 MS Dir 命令
目錄 /a:d /o:-dc:\temp\jsl*
將返回我的 c:\temp 目錄中文件夾名稱以 jsl 開頭的所有文件夾(/a:d 所有目錄),從最近到最舊(/o:-d 順序反向日期)。 所以我可以使用當天的日期來縮小列表的範圍。
對於我的“無數”文件夾問題,我必須搜索的目錄名稱結構類似於 20180524*something_somethingelse。因為有時備份會重新啟動文件夾構建,所以有時我會在列表中得到 2 個值,很少有 3 個值。 但因為它是倒序的,所以我列表中的第一項是目標文件夾。
在我的腳本中,一旦我得到目標文件夾,我就會使用目錄中的文件 (),現在我有了具有特定的、未知的、變化的時間的目錄,並處理每個文件並創建一個報告,所有這些都是遠程的(批量的) ).
如果您使用的是 Mac,我只能提供 Jim 的非常棒的建議。 對於每個文件,在 Files inDirectory() 返回的列表中搜索包含今天日期的文件,如果超過一個,則獲取創建日期並找到最新的一個。
如果您使用的是 Windows,並且您正在處理性能問題,那麼這裡有一個腳本可以說明如何獲取 JMP 安裝目錄中的所有文件夾。[注意像這個例子一樣簡單的東西,Directory() 中的文件是最好的解決方案。]
// Get the full path for the JMP Install Directory
jmpDir = Convert File Path( "$JMP_Home", Windows );
Show( jmpDir );
// Build the DOS "dir" command
// dir /-C /T:C /S "C:\Program Files\SAS\JMPPRO\13\Samples\Data\*.J*" > c:\temp\dirOut_rp.txt
// --- DOS requires quoted file paths if the path includes spaces Program Files has a space
//this has no output file and /b only the directory name is returned
cmd_txt ="\[/c dir /a:d /b /-c /o:-d "]\"
|| jmpDir //will replace it with the full path of your installed JMP directory
||"\[* ]\";
show(cmd_txt ); //see the log file
//results returned as text
rpobj = RunProgram(Executable("CMD.EXE" ),
Options({Eval(cmd_txt)}),
ReadFunction("text")
);
show(rpobj);
if(length(rpobj) >0, dirList= words(rpobj,"\!N") );
show(dirList);
/*
{"ExcelProfiler", "imagemaps", "Tip of the Day", "Help", "Resources", "pptx",
"Flash", "Documentation", "tkext", "Scoring", "Samples", "Web", "Maps"}
*/
//____________________________________________________________________________________________
//alternate command to return information to a file
outDir = "c:\temp\dirOut_rp.txt";
cmd_txt ="\[/c dir /a:d /b /-c /o:-d "]\"
|| jmpDir //will replace it with the full path of your installed Sample Data directory
|| "\[*" > ]\"
|| outDir;
show(cmd_txt ); //see the log file
t0 = Today();
rpobj = RunProgram(Executable("CMD.EXE" ),
Options({Eval(cmd_txt)}),
ReadFunction("text")
);
//--- ReadFunction is reading statements from the OS, it reads until the program quits.
// This command suspends subsequent JSL until executable has completed the tasks.
//==== Show results=============================================================================
// A couple of "checks" that you may employ here if the file does not does not exist or if it is older than expected
If( !File Exists( outDir ) | (Creation Date( outDir ) - t0 > 10),
Caption( "There was a problem with the dir command, it did not run. Aborting..." );
Wait( 4 );
Caption( Remove );
Throw();
);
// Open the results file as text stored in column 1
dir_dt = Open(
outDir,
columns( Name( "Dir results" ) = Character ),
Import Settings(
End Of Line( CRLF ),
End Of Field( CRLF ), // ensures only 1 column
Strip Quotes( 1 ),
Use Apostrophe as Quotation Mark( 1 ),
Scan Whole File( 1 ),
Treat empty columns as numeric( 0 ),
Labels( 0 ),
Data Starts( 1 ),
Lines To Read( All ),
Year Rule( "10-90" )
)
);
您沒有說明如何指定從哪個目錄獲取文件,所以我只使用了 Pick Directory()。我假設您只有一個標准文件夾可以訪問,在這種情況下,您可以使用以下方法繞過 Pick Directory():
folder = "<YOUR folder="" path="">";</YOUR>
吉姆,
感謝您的幫助,
我不知道選擇目錄功能是否可以非提示運行?我的腳本應該每天在後台運行,沒有用戶參與
謝謝
這是一個簡單的腳本,我認為它可以讓您了解如何找出滿足您需求的文件
Names Default To Here( 1 );
folder = Pick Directory();
// Get all of the files in the folder
FileList = Files In Directory( folder );
// Create the known part of the files to look for
Header = Char( Day( Today() ) ) || "_" || Char( Month( Today() ) ) || "_" ||
Char( Year( Today() ) - 2000 ) || "_";
// Loop through the found files and eliminate all that do not
// match the pattern we are looking for
For( i = N Items( FileList ), i >= 1, i--,
If( Left( FileList, Length( Header ) ) != Header,
FileList = Remove( FileList, i, 1 )
)
);