Batch Delete Files on server

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
RichTea88
Posts: 4
Joined: 09 Mar 2016 09:35

Batch Delete Files on server

#1 Post by RichTea88 » 09 Mar 2016 09:45

Good Afternoon all,

I'm trying to delete a whole bunch of files on a server. I'm new to writing .bat files and I'm not entirely sure what to do. I have a long list of files and currently I just used excel to add 'del' in from of the path and '/f' at the end then saved it in a .bat file from notepad. It worked fine on my desktop for the tests I ran but doesn't work on the server locations.

Code: Select all

del \\ServerName\BigFolder\OtherFolders\Folder\subfolder\file.txt /f
del \\ServerName\BigFolder\OtherFolders\Folder\subfolder\test.txt /f


I tried mapping the location of the files to a network drive and changed the code to the below but again no result.

Code: Select all

del Z:\Folder\subfolder\file.txt /f
del Z:\Folder\subfolder\test.txt /f


Any help would be greatly appreciated.

Kind Regards in advance!

Cheers,

R

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch Delete Files on server

#2 Post by foxidrive » 09 Mar 2016 10:45

RichTea88 wrote:then saved it in a .bat file from notepad. It worked fine on my desktop for the tests I ran but doesn't work on the server locations.


Did you see any error messages on the console? What did you see?

RichTea88
Posts: 4
Joined: 09 Mar 2016 09:35

Re: Batch Delete Files on server

#3 Post by RichTea88 » 09 Mar 2016 10:50

foxidrive wrote:
RichTea88 wrote:then saved it in a .bat file from notepad. It worked fine on my desktop for the tests I ran but doesn't work on the server locations.


Did you see any error messages on the console? What did you see?



Nothing, I mean the console popped up and did something really quickly then disappeared but I don;t know how to get the window to stay open long enough for me to see what it says

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch Delete Files on server

#4 Post by ShadowThief » 09 Mar 2016 13:28

You may want to

Code: Select all

pushd \\ServerName\BigFolder\OtherFolders\Folder\subfolder\
and then you can just

Code: Select all

del /f file.txt
del /f test.txt
etc.
and finish your script with

Code: Select all

popd
.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch Delete Files on server

#5 Post by foxidrive » 09 Mar 2016 14:08

RichTea88 wrote:I mean the console popped up and did something really quickly then disappeared


Put pause as the last line in your script, or
open a cmd prompt and launch your batch file that way.

RichTea88
Posts: 4
Joined: 09 Mar 2016 09:35

Re: Batch Delete Files on server

#6 Post by RichTea88 » 10 Mar 2016 03:26

Awesome, just added the pause and ran it. I think I know what the problem is. I'm accessing the files through a remote desktop, the error reads

"C:\Users\MyName\Desktop>del Z:\Folder\subfolder\file.txt /f
The system cannot find the file specified"

If I use pushd to a certain folder level is it almost the same as creating a network drive to that folder and popd removes it once complete?

I ask because not all of the files are in the same folder, they're all over the place. So I'd use pushd to the most common folder then specify the file path from there?

RichTea88
Posts: 4
Joined: 09 Mar 2016 09:35

Re: Batch Delete Files on server

#7 Post by RichTea88 » 10 Mar 2016 03:41

Okay,

I tried using pushd to the most common folder, didn't work. However it does work it right to the folder the files sit in.
What I'll do is create a spreadsheet with all the file paths and seperate the file names from their path.
Then

Code: Select all

pushd folderpath
del file1.txt
del file2.txt
popd
pushd nextfolderpath
del file1.txt
del file2.txt
popd

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch Delete Files on server

#8 Post by ShadowThief » 10 Mar 2016 10:45

RichTea88 wrote:If I use pushd to a certain folder level is it almost the same as creating a network drive to that folder and popd removes it once complete?


Yes, that's exactly what that does. It also moves you into that directory once the network drive is created. You don't have any say in what letter the network drive is assigned to, so you can't use full paths, but if you pushd \\server\directory\other_directory and your files are located in \\server\directory\other_directory\third_directory\file.txt, you can simply del .\third_directory\file.txt to delete them.

Post Reply