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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

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

#1 Post by goodywp » 03 May 2022 10:23

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 04 May 2022 11:33

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

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

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

#3 Post by goodywp » 04 May 2022 14:47

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!

Post Reply