0

I had created one batch file.In this I have stored: My query's o/p into file named List.txt Its contains multiple files. I want to delete these files which are contains in List.txt.

I am writing code :

set file=E:\AppData\List.txt
findstr /s /p /x /m /c:"test" *.txt> %file%
for /F "tokens=* delims=" %%A in (%file%) Do 
(
    echo %%A
    set name = "E:\AppData\%%A"
    del %name% 
)
pause

I can not access %name% value. How can I delete these files?

My List.txt contains 4 files :

Data\Application 1\1.txt
Data\Application 1\4.txt
Data\Application 2\1.txt
Data\Application 2\4.txt
Data\Application 3\1.txt

EDIT

I made complete batch file and when i run it i get an error

The syntax of the command is incorrect.
 E:\AppData\Data>for /F "tokens=* delims=" %A in (E:\AppData\List.txt) Do

I share my batch script here please can anyone find my mistake please.

@echo off
set file=E:\AppData\List.txt

findstr /s /p /x /m /c:"test" *.txt> %file%

  :END
    echo Files are Safe not deleted.

    call "E:\batch files\linecount.bat"

    SET /P "ANSWER=Click Y to delete all Files or N to stop (Y/N)"

    for /F "tokens=* delims=" %%A in (%file%) Do 
    (
       echo %%A
       del "E:\AppData\%%A"
    )   
    echo Files are deleted successfully.
pause
Jui Shah
  • 108
  • 1
  • 10

2 Answers2

1

Try this

@echo off
for /f "tokens=*" %%a in (List.txt) do (
  del /F /Q %%a
)
pause

Its working.

Jomon Antony
  • 258
  • 3
  • 15
0

you need either delayed expansion or to change your code a little bit:

set file=E:\AppData\List.txt
findstr /s /p /x /m /c:"test" *.txt> %file%
for /F "tokens=* delims=" %%A in (%file%) Do (
    echo %%A
    del "E:\AppData\%%A"
)
pause
Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • What is delayed Expression? How it will used and what's the purpose of it? – Jui Shah Mar 15 '16 at 11:08
  • I linked to a short explanation of delayed ex_pansion_ in my answer (click onto the blue words). In the comments there, I linked to some deeper explanations. – Stephan Mar 15 '16 at 11:43
  • I again get an error when i run my batch file. The syntax of the command is incorrect. E:\AppData\Data> for /F "tokens=* delims=" %A in (E:\AppData\List.txt) Do – Jui Shah Mar 16 '16 at 05:39
  • The `(` has to be on the same line as `do`. I corrected it in my answer (no idea how it happened to be on the next line `:(`). The syntax error was, because there was no command after `do`. – Stephan Mar 16 '16 at 06:51