Prankman
Prankman

Reputation: 11

Problem with batch file doesn't detect file

I have a problem with my program, I don't know how can I make it detect that the file NewFile(%i%) exists.

    for /l %%i in (1, 1, 100) do (

    if EXIST NewFile(%i%).txt (
    echo "New file"> NewFile(%%i).txt
    goto EndLoop
    )
    
    if EXIST NewFile.txt(
    echo "New file"> NewFile(%%i).txt
    goto EndLoop
    ) else (
    echo "New file"> NewFile.txt
    goto EndLoop
    )
)

It says ") was unexpected at this time."
How can I fix this?

Upvotes: 1

Views: 81

Answers (1)

user7818749
user7818749

Reputation:

You currently have a few issues, of which the first is the one causing the issue you experienced, but the others will also cause the script to not function. You have a for loop with a parathesis inside the loop, for is expecting that loop as the ending parenthesis of the loop. So you would need to escape that closing parenthesis as if EXIST NewFile(%%i^).txt (

You also have an invalid %i% in the first if statement. it should be %%i The line if EXIST NewFile.txt( should have a space before the opening parenthesis.

@echo off
for /l %%i in (1,1,100) do (
    if EXIST "NewFile(%%i^).txt" (
    echo "New file"> NewFile(%%i^).txt
    goto :EndLoop
   )
   if EXIST NewFile.txt (
       echo "New file"> NewFile(%%i^).txt
       goto EndLoop
   ) else (
       echo "New file"> NewFile.txt
       goto :EndLoop
   )
)

Then, the goto's as I am not sure what you want to achieve there, but just to make sure you know, if it finds one file that matches it will exit the loop. If that is your intention then that is fine, if you want to perform the action on all the numbers, you should not have the goto there.

Upvotes: 1

Related Questions