1

I have written the following code to remove both files and sub-folders from a number of folders (Folder 1, Folder 2 etc. The script works successfully when there are no spaces in the folder names however, it fails to delete the temp file (files2del.txt) and the sub-folders when there spaces. The path ends on a dot.

@echo off

setlocal EnableDelayedExpansion

set folder[0]=Folder 1
set folder[1]=Folder 2

for /l %%s in (0,1,1) do (
  set "var=C:\directory\!folder[%%s]!"
  dir "!var!"/s/b/a | sort /r >> !var!\files2del.txt
  for /f "delims=;" %%D in (!var!\files2del.txt) do (del /q "%%D" & rd "%%D")
  pause
)
exit

The below line seems to be the issue.

for /f "delims=;" %%D in (!var!\files2del.txt) do (del /q "%%D" & rd "%%D")

I have tried various things like %, quotes etc. but to no avail.

Thanks

1 Answer 1

1

try with:

for /f "usebackq delims=;" %%D in ("!var!\files2del.txt") do (del /q "%%D" & rd "%%D")

You can use double quotes in when reading a file when usebackq option is used.Tough it is not mentioned in FOR /? message.

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

1 Comment

From the usebackq portion of the FOR command help file: allows the use of double quotes to quote file names in file-set

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.