My intention is to use WHILE loop in a script object, bu I’m having difficulties to create one. The idea is to check the count of .txt files in a folder and then use move command to move and rename those .txt files. Any ideas how to implement this? Many thanks!
Pass an OS command (ls for Linux / dir for Windows) into the path that you want to check the files using exec() function and assign it to a variable. (Say $Filelist). Maintain the length of the variable to be long enough (say varchar 5000) to accommodate all the available files.
While loop:
In $Filelist, check for .txt extention and extract the file names one by one into another variable (Say $Filename) using word_ext() function and remove the same from $G_Filelist using replace_substr(). Iterate a counter along with. This way, you can get the count.
A variable $New_Name can be appended with the existing filename along with the change that you want to make. (Eg: if you want to add the current date, you can concatenate sysdate()) with filename.
In the same while loop, pass a move command (mv for Linux, move for Windows) and move each of the $FileName from source path to destination path. Include $New_Name for renaming, at the end of the move command, after destination path.
If required, you can use a print command before exec function with return code 8, so that you can see the OS command status in job log.
Exit the while loop when length($Filename) is zero.
In the method that I mentioned above, there is a chance that the loop executes once more, even after all the files have been moved to the target.
This is because, the value of $FileName remains to be not null, even after all the files are moved. It becomes null only in the next execution.
So, after the while loop, do a count-1 on the counter, so that you get the exact number of files moved.
We will have to check the length ($FileName)<>0 for the while loop as there is a chance that some other string may enter, which does not have the filename. In this scenario, the while loop should not run infinite times.
Larus has used V_Path as a Variable, the definition of which can be given in the job parameters if the variable is Global, or in a script preceding this code. V_Path is the folder in which the files exist.
You can give $G_LOAD_DATE_CHAR like this
$G_LOAD_DATE_CHAR = to_char(sysdate(),‘yyyymmddhh24miss’); This makes the script smaller.
You can use quotes in your execution command
Eg : print(‘MOVE “[$V_Path_Luku][$V_FileName]” “[$V_Path_Arkisto][$V_FileNewName]”’);
so that you can avoid the double slashes while defining value for path. In this case, a double slash will be required at the end of the path. Eg:’:<Rootpath>\Ariksto\’