Solved Batch file to delete specific files from network computers

October 12, 2012 at 08:43:29
Specs: Windows 7
I'm trying to write a script that I can use to delete specific files from 16 computers on my network. This is mainly for regular cleanup tasks as the files are no longer needed. What I want to be able to do is have a user open a text file and type in the names of the files that they want to delete and separate them with semicolons (;) or by putting one file on each line. The script when executed would open the text document and go through it line by line and start at a directory (on the network) and move down through the subdirectories looking for a specific file, and if found, would delete it. It would then go to the next item in the list and do the same until all of the files were searched for and deleted. It would then move to the next computer on the network and repeat this task.

I've by trying to use nested FOR /R, FOR /F, and FORFILES commands to get the outcome I'm looking for but to no avail. It seems that I can get script to walk through the text file and echo back the correct names, and I can get it to go out to a network folder and recurse through the subdirectories and delete files, but I can't get it to do both together. I'm relatively new at this with minimal background in scripting.

Here is what I have so far (which I'm sure is way off):

:deletefilecomp1
setlocal EnableDelayedExpansion
net use z:%computer%\starting folder
FOR /F "delims=; tokens=*" %%G IN (file_removal_list.txt) DO (
FOR /F %%H IN \\%cell1%\windyn\XConsole\ DO DEL (%%H)
)
net use z:%computer%\starting folder /delete

Any help would be greatly appreciated.


See More: Batch file to delete specific files from network computers

Report •

✔ Best Answer
October 24, 2012 at 17:36:57
@Razor: Thanks!
From what Razor said, and what you want, it sounds like net view might be the best trade-off, because it confirms server services operational while ping does not, and I would assume that any automated feature on a network (set up by admin.) would account for permissions and make sure they are adequate for the operation.
That said, you could choose between net view or, as per your suggestion, "if exist".

IF EXIST method (untested):
...
for /f "tokens=*" %%a in (serverdir_test.txt) do (
if exist %%a\. (
net use z: %%a
z:
del /s/p %files%
c:
net use z: /delete /y
) else (>>log echo %%a failed to respond)
)
::==== end snippet

net view method (also untested):
...
for /f "tokens=1* delims=\" %%a in (serverdir_test.txt) do (
net view \\%%a && (
net use z: %%a\\%%b
z:
del /s/p %files%
c:
net use z: /delete /y
) || (>>log echo %%a failed to respond)
)
::=== end

I have not tested all this flak, I may have lots of mistakes. When you said 15-20 second lag, was that overall, or per computer? If overall, things probably won't improve much. If per computer, hopefully get it down to under 5 seconds per failure (successes are almost immediate).



#1
October 12, 2012 at 18:17:08
I'm not sure, but this might work as a basis:

::========= begin script
@echo off & setlocal enabledelayedexpansion
::== load the file-list from the control-file
set files=
for /f "tokens=*" %%a in (cleanup.txt) do set files=!files! "%%a"
for /f "tokens=*" %%a in (serverdirs) do (
:: this is a file containing all the computers and their resp. dir.s to start from,
:: each line like: \\emach\c\test1
set serverdir=%%a
::====
::=== apply the deletion specs.
net use z: %%a
cd /d z:
del /s /q %files%
net use z: /del
)


Report •

#2
October 15, 2012 at 12:34:00
I'll give this a shot and get back to you. I'm travelling for work this week but hopefully I can give it a shot in the evening. Thanks!

Report •

#3
October 17, 2012 at 05:33:14
For serverdir do I need to create another text file with a list of all of the network computers and the starting folder? I tried running this script and it maps z: but for whatever reason doesn't seem to end up in the right folder because I get a 'file cannot be found' type of error. I even placed a test file right in the root folder and it couldn't identify that.

Report •

Related Solutions

#4
October 17, 2012 at 13:30:30
Q&D batch (test at your own risk):
SET fileList=
SET com=
FOR /F %%a IN (file_removal_list.txt) DO CALL :MakeFileList %%a

REM TODO: Populate %com% with the current computer. This bit will probably be in
REM       a loop.
CALL DEL /S /F %fileList%
REM TODO: End the loop mentioned in the previous TODO.

GOTO :EOF
:MakeFileList
IF "%~1"=="" GOTO :EOF
SET fileList=%fileList% "\\%%com%%\c$\starting folder\%~nx1"
SHIFT
GOTO MakeFileList

How To Ask Questions The Smart Way


Report •

#5
October 17, 2012 at 19:25:22
I ran a quick test, with no problems. make sure your user-file does NOT contain the dbl-quotes, since the batch-script adds them. The list of files looks like:

delete.ths
del.tht
delete this file with spaces in it.xls
temp

One filename per line, but no quotes. and yes, the list of servers is as you described. you might try debugging by adding this line at this point:
...
for /f "tokens=*" %%a in (cleanup.txt) do set files=!files! "%%a"
echo DEBUG files:%files%
for /f "tokens=*" %%a in (serverdirs) do (
...


Report •

#6
October 23, 2012 at 07:25:57
nbrane - I realized I had added a semicolon for a delimiter in the cleanup.txt file so it appears to be working correctly now. I also fat-fingered one of my network locations. It was a long week on the road last week. Things seem to be getting closer. I had to add another change directory to switch from z: back to c: to avoid an error after it deleted the mapped network drive. This is where my script stands now:

@echo off & setlocal enabledelayedexpansion
set files=
for /f "tokens=*" %%a in (file_removal_list.txt) do set files=!files! "%%a"
for /f "tokens=*" %%a in (serverdir_test.txt) do (
set serverdir=%%a
net use z: %%a
cd /d z:
del /s/p %files%
cd /d c:
net use z: /delete /y
)


Is there an easy way to take this a step further and return an error if the computers in serverdir_test.txt are not on the network? Something as simple as "Computer X is not active on the network."? It also hangs forever waiting for a response from the network computer.

Razor2.3 - I appreciate the help and for the most part I'm quite new to this. I have not tried your solution yet as I think I'm close with what nbrane suggested, and I also don't understand some of the commands fully yet.

Thanks for the help everyone!


Report •

#7
October 23, 2012 at 18:38:05
...
for /f "tokens=1* delims=\" %%a in (serverdir_test.txt) do (
:: consult: ping /? for details...
ping -n 1 -w 500 > nul 2>&1 %%a && (
rem set serverdir=%%a\\%%b
net use z: %%a\\%%b
cd /d z:
del /s/p %files%
cd /d c:
net use z: /delete /y
) || (>>log echo %%a failed to respond)
)
::===== end

not fully tested. I think pinging is the fastest and best way to test access, but then, I don't know much... net view was about 2 seconds slower, but "dir \\x\y" was about the same as ping.


Report •

#8
October 24, 2012 at 07:35:50
The DIR method is probably the slowest, but both it and NET VIEW will establish a session between you and the remote. NET VIEW however doesn't check to see if the account has the required permissions to access the required folder.

PING is the fastest, but doesn't guarantee the Server service is started on the remote, nor that the shares are there, nor that the account has the required permissions, nor anything beyond the remote's network stack is up and there's a routable connection between the two. Also, you'll still have to establish a session.

How To Ask Questions The Smart Way


Report •

#9
October 24, 2012 at 09:11:45
A solution that only takes a couple of seconds would be a big improvement over the 15-20 second lag I had with the original script.

How would I implement the DIR or NET VIEW solution? It sounds like that is the better route to go since most (if not all) computers should be available and I will still need to establish a session. Do I need to use an IF EXIST statement?


Report •

#10
October 24, 2012 at 17:36:57
✔ Best Answer
@Razor: Thanks!
From what Razor said, and what you want, it sounds like net view might be the best trade-off, because it confirms server services operational while ping does not, and I would assume that any automated feature on a network (set up by admin.) would account for permissions and make sure they are adequate for the operation.
That said, you could choose between net view or, as per your suggestion, "if exist".

IF EXIST method (untested):
...
for /f "tokens=*" %%a in (serverdir_test.txt) do (
if exist %%a\. (
net use z: %%a
z:
del /s/p %files%
c:
net use z: /delete /y
) else (>>log echo %%a failed to respond)
)
::==== end snippet

net view method (also untested):
...
for /f "tokens=1* delims=\" %%a in (serverdir_test.txt) do (
net view \\%%a && (
net use z: %%a\\%%b
z:
del /s/p %files%
c:
net use z: /delete /y
) || (>>log echo %%a failed to respond)
)
::=== end

I have not tested all this flak, I may have lots of mistakes. When you said 15-20 second lag, was that overall, or per computer? If overall, things probably won't improve much. If per computer, hopefully get it down to under 5 seconds per failure (successes are almost immediate).


Report •

#11
October 29, 2012 at 19:06:13
Thanks everyone! It doesn't really appear that the 'if exist' method is any quicker so I'm just going to chalk this up to slow network communication. Thanks for all of the explaining...it helped me pick up a lot of scripting for such a small project.

Report •

Ask Question