1

I need a batch that will delete files from a LAN, all the paths of the files being saved in a txt file. Don't know how the batch will "read" the paths then delete those files with DEL command. The line that works so far is: del "path\*.txt" - for deletion of all txt in some folder (path being the actual line, like c:\folder\folder\*.txt), but I need for a lot more paths. i pushed then the batch with psexec.exe (for the LAN deletion)

I guess it's 2-3 lines of code, but I'm new to batching & scripting, could someone pls help! Thanks in advance

Paradigm
  • 193
  • 1
  • 4
  • 11

2 Answers2

3

You can use the FOR /F command to process each line in your input file. Here is a SO answer: How do you loop through each line in a text file using a windows batch file? This just worked for me (the parentheses around the file name are necessary):

for /F "tokens=*" %%A in (myfile.txt) do del "%%A"

myfile.txt looks like this:

a.txt
b.txt
a b c.txt
Community
  • 1
  • 1
BubuIIC
  • 380
  • 3
  • 15
  • so the text file that contains the paths look like (eg): c:\path\file1.pdf; second line is c:\path\file2.txt, and so on. Will the `for /f` loop through each file then where should I put the `del `command? (how should I write it?) @BubullC – Paradigm Jan 13 '13 at 19:52
  • something like this: `for /F "tokens=*" %%A in (myfile.txt) do [process] %%A` ? where [process] will be the del command? @BubullC – Paradigm Jan 13 '13 at 19:59
  • I included an example in my answer. – BubuIIC Jan 13 '13 at 20:13
  • thanks! it worked!!! was wondering: If some file is currently being modified (someone works with it or just is open at the time of deletion), does the batch delete it after the user closes it? thanks a lot again for your prompt answer @BubullC – Paradigm Jan 14 '13 at 12:05
  • thanks! it worked!!! If some file is currently being modified (someone works with it or just is open at the time of deletion), the batch deletes it after the user closes it!!! thanks a lot again for your prompt answer Can I make the batch delete the files just after the user closes it? @BubullC – Paradigm Jan 14 '13 at 12:10
  • Depending on whether the program is blocking the file or not, the file gets either deleted immediately or not all all. – BubuIIC Jan 14 '13 at 16:32
1
del /s *.txt

/s means - delete from all subfolders..

or to iterate over directories:

for /d %i in (*.*) do del %i\*.exe

!!!) you should escape % with % if your code is in batch file

doniyor
  • 36,596
  • 57
  • 175
  • 260