Filerename statement causing procedure to stop

I have several folders in the same folder (named ’ Moving’, that’s with a leading space) as this procedure. In the following code,

;  Loop through files in this folder
looparray FileNames, cr(), FileFolderName
    ;  Loop through strings in text file
    looparray TagsArray,cr(),StringName
        if FileFolderName contains StringName
            filerename FileFolderName, strip(replace(FileFolderName,StringName,""))
        endif
    endloop
endloop

FileNames contains a list of the folders and TagsArray contains a list of strings which I want removed from the folder names. The filerename statement successfully removes the first string from the first folder name and then stops with this error message:

FileRename error: “Sketchbook 03 (of 05) (2021) (Digital)” couldn’t be moved to “ Moving” because either the former doesn’t exist, or the folder containing the latter doesn’t exist.

If I put an if error trap after the filerename, the procedure removes one string from each of the folder names and then stops without an error message. Each subsequent run of the procedure removes another string from each folder.

The leading space in the file name is not a factor. Specifying the full path name in each parameter makes no difference.

Am I doing something wrong or is this a bug?

The bug is yours. The first filerename statement changes the name, but it doesn’t change the content of the FileFolderName variable. The second attempt at a filerename, is still referring to the folder by its former name, and that fails because there isn’t any folder with that name anymore.

1 Like

Try writing it like this.

;  Loop through files in this folder
looparray FileNames, cr(), FileFolderName
    let NewName = FileFolderName
    ;  Loop through strings in text file
    looparray TagsArray,cr(),StringName
        NewName = strip(replace(NewName,StringName,""))
    endloop
    filerename FileFolderName, NewName
endloop
1 Like

Ah, yes - and I expect that would be much more efficient. Thank you Dave.