Page 1 of 1

How to get the second last folder if the last folder not contain the desired file yet

Posted: 03 May 2022 10:23
by goodywp
Hello All,

I have a below script to do the job to scan the latest folder which should contain the wanted file from build.
Since it is a runtime, now the issue is sometimes the last (latest) folder created but somehow the wanted file not generated yet or this build (folder) only for something else....
So how can we modify this script to find the last second one which will contain the file desired if the last folder not having the file yet.
Since the build number folder is not known in this case. So my first option is to get the latest build number folder as below.
However, there are two exceptions. First this build (folder) may not have desired file, or second the build has this file but is running to generating and not ready yet....

Code: Select all

@echo off
set agentsource=C:\bamboo-agent-home\build-dir\BA-CAEXTEST-RAT\source
set remotesource=\\ussnprdevapp8\bamboo2_artifacts
set plannum=plan-430113518
set flavr=CHC

::scan the latest folder
FOR /F "delims=" %%i IN ('dir "%remotesource%\%plannum%\shared" /b /ad-h /t:c /od') DO SET newfld=%%i
echo Most recent subfolder: %newfld%

::prepare the latest package
if not exist %agentsource% (md %agentsource%)
if exist %agentsource%\*.zip (del %agentsource%\*.zip) 
copy "%remotesource%\%plannum%\shared\%newfld%\A591\*_%flavr%_*.zip" "%agentsource%"
echo %ERRORLEVEL%	
Thanks

Re: How to get the second last folder if the last folder not contain the desired file yet

Posted: 04 May 2022 11:33
by aGerman
Reverse the sorting (newest folder first) and bail out when the folder containing the zip file is found.
Untested:

Code: Select all

FOR /F "delims=" %%i IN ('dir "%remotesource%\%plannum%\shared" /b /ad-h /t:c /o-d') DO (
  IF EXIST "%remotesource%\%plannum%\shared\%%i\A591\*_%flavr%_*.zip" (
    SET "newfld=%%i"
    GOTO :end_loop
  )
)
:end_loop
Steffen

Re: How to get the second last folder if the last folder not contain the desired file yet

Posted: 04 May 2022 14:47
by goodywp
Thanks Steffen!!! It is quick and simple just by sorting newest first! plus checking zip file.
Yes it works for sure for the first case.
1) When the zip file not in the last folder then search up in the second last...
2) When the zip file is there but not finished yet, still in generating (since normally take time to do it), so this case not tested yet, will check when this happen...how that works
Thanks again!