0

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 folder

Waiting 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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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%

12 Comments

If it start the loop again, it will start read the first file again and I want it to read all the files in the array. But why is the code will exist the array if it only read the first string?
you have string list A.txt B.txt C.txt D.txt giving list of file name. in your loop create one more string list with file name which are not found. when you loop exits if you have any values in your new list, set value of original list = new list and resets the loop using goto. this process will connue till you have any file name coming in new list.
and dont forget to reset your new list to empty after transferring its value to original list and before resetting the loop
I get what you mean, something like remove the array from the list. But what is the root cause or logic that the array will terminate after search the first string?
Just came to my mind that GOTO always breaks the for loop. give me sometime I will write proper script for you
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.