Reputation: 103
I am try to loop all the source file in array and make sure it is exits in the folder with the file path I have set, below is my code
@echo OFF
setlocal EnableExtensions EnableDelayedExpansion
set "string_list=A.txt B.txt C.txt D.txt"
set count=0
For %%j in (%string_list%) Do Set /A count+=1
rem echo.Total count: %count%
set current_count=0
REM for %%s in (%string_list%) do (
for %%s in (%string_list%) do (
set "var=C:\Users\ABC\Desktop\TESTING\%%s"
rem echo "!var!"
:START
if not exist "!var!" GOTO WAIT
GOTO COPY
:WAIT
timeout 5
ECHO "MISSING !var!, WAITING SOURCE FILE TO LOAD IN"
GOTO START
:COPY
ECHO "!var! FILES IS AVAIALBLE"
set /A current_count+=1
if /I %current_count%==%count% (
echo "ALL THE FILE IS IS AVAILABLE"
) else (
echo "CONTINUE CHECK FOR OTHER FILE"
)
)
rem echo %current_count%
pause
It will exit the loop once it found the first file. The code is to check and make sure all the files are in the folder, if one of the file is not there then continue loop until the file is there.
I am not sure can the function add in the FOR loop.
I'm expecting this:
C:\Users\ABC\Desktop\TESTING\A.txt FILES IS AVAILABLE
<- A.txt is in the folderWaiting for 0 seconds, press a key to continue ...
But I'm getting this:
MISSING C:\Users\ABC\Desktop\TESTING\B.txt, WAITING SOURCE FILE TO LOAD IN
<- B.txt is not in the folder for the first time.
C:\Users\ABC\Desktop\TESTING\B.txt FILES IS AVAIALBLE
<- B.txt is in the folder for the second loop
C:\Users\ABC\Desktop\TESTING\C.txt FILES IS AVAIALBLE
<- C.txt is in the folder
C:\Users\ABC\Desktop\TESTING\D.txt FILES IS AVAIALBLE
<- D.txt is in the folder
The loop should only exit if all the files are in the folder, or else it will continue loop.
Upvotes: 0
Views: 102
Reputation: 79983
What an extraordinarily complex method to fail to do a very simple task! (oh, well - we all have to start somewhere)
It would help if the actual objective of the code was stated. As it is, we've got a method that fails (otherwise there would be no question) to do a task - and we have to analyse the code to determine the desired task.
Fundamentally, the code fails because labels are not allowed within a code block
(a parenthesised sequence of commands).
@echo OFF
:: switch to the destination directory as exit is required with tat directory current
cd /d "C:\Users\ABC\Desktop\TESTING"
setlocal EnableExtensions EnableDelayedExpansion
set "string_list=A.txt B.txt C.txt D.txt"
:wait
timeout /t 5 >nul
cls
for %%j in (%string_list%) do if exist "%%~j" (
echo "%%~j" exists
) else (
echo "%%~j" missing
goto wait
)
echo all files found
Obviously, add in the counter if required. I've added a cls
to reduce screen clutter. Omit it if desired.
So - for each nominated file (since we changed to the desired directory first-off), see whether it exists. If it does, report it. Otherwise report that it's missing and wait. Repeat until all found.
Upvotes: 3
Reputation: 1853
as I said in comment goto breaks for loop please refer
Batch script for loop not working
use subroutine to get what you want. I have modified following script and it works for you case :
@echo OFF
setlocal EnableExtensions EnableDelayedExpansion
set string_list=A.txt B.txt C.txt D.txt
set count=0
For %%j in (%string_list%) Do Set /A count+=1
echo.Total count: %count%
set current_count=0
FOR %%s in (%string_list%) DO call :workProc %%s
exit /b
:workProc
set "var=%1"
echo "hey"
echo "!var!"
:START
if not exist !var! GOTO WAIT
GOTO COPY
:WAIT
timeout 5
ECHO "MISSING !var!, WAITING SOURCE FILE TO LOAD IN"
GOTO START
:COPY
ECHO "!var! FILES IS AVAIALBLE"
set /A current_count+=1
if /I %current_count%==%count% (
echo "ALL THE FILE IS IS AVAILABLE"
) else (
echo "CONTINUE CHECK FOR OTHER FILE"
pause )
)
exit /b
rem echo %current_count%
Upvotes: 1