0

So I am trying to create a batch file, that gets all the files in deletesrc and find's if it's name matches any folder in deletedest. To accomplish this, I have tried to generate a list of file names in deletesrc which is stored in deletefiles (in the format "first" "second" "third file"). Then I intend to loop through each folder in deletedest and check for it's existence in deletesrc (using this). So far I have managed:

setlocal enabledelayedexpansion

set "deletesrc=F:\Delete Source"
set "deletedest=C:\Users\Spaced Name\Delete\Dest"

set "deletefiles=" ::I think this is useless, but I would prefer it to be here

for /R "%deletesrc%\" %%i in (*) do (
  set "deletefiles=!deletefiles!^"%%i^" "
  ::random comment that is source of error
)

set deletefiles=!deletefiles!:rTrim

echo !deletefiles!

The main problem is, I keep getting ) was unexpected at this time.

Placing an echo test as the first thing within the loop, does not change the output, implying the loop is not run and the error encountered first. Placing an echo just before the loop, does produce output and the error immediately after it.

If there are any syntax standards or things I should/should not be doing that do not answer the question, I would like to know.

Community
  • 1
  • 1
Winestone
  • 1,500
  • 9
  • 19

2 Answers2

2

Your problem is you're using :: as a comment inside your FOR loop. You can't do this because :: is actually the label designator and it is breaking the FOR loop.

Change this line:

::random comment that is source of error

To this:

REM random comment that is source of error

UPDATE

As foxidrive stated in the comments, you can use :: as a comment inside a FOR as long as it is not the last line before the closing ).

aphoria
  • 19,796
  • 7
  • 64
  • 73
  • 2
    You can use `::` but not as the last text line before the closing parenthesis, mind you is it wiser not to use it in a loop. – foxidrive Oct 14 '14 at 13:16
1

Try this:

 set deletefiles=!deletefiles! "%%i"
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • Then maybe you are adding too many paths\files as a variable can only be around 8 KB long. – foxidrive Oct 14 '14 at 12:51
  • It works here. Did you use the suggestion I made as it is shown? – foxidrive Oct 14 '14 at 12:57
  • Ok, my fault, I had comments within the ``()`` but I didn't think that would create an error. Can you find out why this happens? – Winestone Oct 14 '14 at 13:02
  • 1
    You waste people's time when you don't show accurate code. It's the prime sin when asking questions. – foxidrive Oct 14 '14 at 13:03
  • Yes you are right, though I was trying to strip the code down as it is mentioned somewhere on stackoverflow. Thank you for pointing me in the right direction to finding the error, without your stimulus I would not have found it. I would still like you to find out why this happens if possible, I will try to find as well. – Winestone Oct 14 '14 at 13:05
  • I found the reason (or I think I did), [here](http://blog.crankybit.com/why-that-batch-for-loop-isnt-working/) comment 38. If you could quote that in your answer, I'll give you a well earned upvote + correct answer. – Winestone Oct 14 '14 at 13:10