learningpython
learningpython

Reputation: 97

Batch script with spaces in folder path

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

Upvotes: 1

Views: 597

Answers (1)

npocmaka
npocmaka

Reputation: 57252

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.

Upvotes: 1

Related Questions