Add value to a column in a while loop in SQL Server
Here is an example of how using recursion might solve your problem.

Assuming that my demo table variable @Folder is equal to what comes our of your #folder at the very bottom of your procedure:

--demo setup
declare @Folder Table (
  Id INTEGER,
  Foldername VARCHAR(8),
  rank INTEGER,
  status INTEGER
);

INSERT INTO @Folder
  (Id, Foldername, rank, status)
VALUES
  ('1', 'folder 1', '1', '1'),
  ('2', 'folder 2', '2', '1'),
  ('3', 'folder 3', '3', '1');

--the solution
;
WITH x
AS (
    SELECT id
        ,Foldername
        ,convert(VARCHAR(10), rank) AS rank
        ,STATUS
    FROM @Folder
    WHERE id = 1

    UNION ALL

    SELECT y.id
        ,y.Foldername
        ,convert(VARCHAR(10), convert(VARCHAR(10), x.rank) + convert(VARCHAR(10), y.rank)) AS rank
        ,y.STATUS
    FROM x
    INNER JOIN @Folder AS y
        ON y.Id = x.Id + 1
    )
SELECT id
    ,Foldername
    ,rank
    ,STATUS
FROM x
ORDER BY id
OPTION (MAXRECURSION 10000);




| id | Foldername | rank | STATUS |
|----|------------|------|--------|
| 1  | folder 1   | 1    | 1      |
| 2  | folder 2   | 12   | 1      |
| 3  | folder 3   | 123  | 1      |