Windows batch scripting & Del files in multiple directories

Windows batch scripting & Del files in multiple directories

I want to delete *.dmp & text files from within the user profiles.      “What command or commands in a script can do this ? “  Because they will be put into the Windows task scheduler for automation.

Location of the files are in the AS400 folder. This is the full path.
H:\USERS\userProfiles\IBM dump\AS400\*.dmp and *.txt.

Note: The environment is Windows 2008 r2, in an Active Directory environment.  userProfiles  contains all of the “different user folders”  who are on the network.

The problem I have been having is bad info claiming to work that is tested and Does not work.
LVL 1
Bob BrownIT Asked:
Who is Participating?
 
NVITCommented:
My post also takes care of the 'two words with a space' issue.

This version traverses each user profile, i.e. user1, user2, etc. and deletes just the desired file patterns under their IBMDump\AS400:

@echo off
setlocal enabledelayedexpansion

set SrcDir=d:\users
set IBMDir=IBMDump\AS400
set FNDelPatt1=*.txt
set FNDelPatt2=*.dmp


for /d %%k in ("%SrcDir%\*") do (
   echo del /q /f /s "%%~dpnk\%IBMDir%\%FNDelPatt1%"
   echo del /q /f /s "%%~dpnk\%IBMDir%\%FNDelPatt2%"
)

Open in new window

0
 
Christopher McKayMicrosoft Network AdministratorCommented:
Have you tried this:

del "%userprofile%\IBM dump\AS400\*.dmp"
del "%userprofile%\IBM dump\AS400\*.txt"


Hope this helps!

Christopher.
0
 
Dean ChafeeIT/InfoSec ManagerCommented:
I like to use FORFILES to do this.  It's more robust and you can keep a report of what was deleted (with the echo Deleted ... )


forfiles /p "%userprofile%\IBM dump\AS400\" /s /m *.dmp /c "cmd /c del @path && echo Deleted @path @fdate @fsize" 

forfiles /p "%userprofile%\IBM dump\AS400\" /s /m *.txt /c "cmd /c del @path && echo Deleted @path @fdate @fsize" 

Open in new window



you can also add a /d parameter to only delete when they are X days old.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
NVITCommented:
The above posts work only for the active user's profile. I think the OP wants to traverse each profile. This works.
- This version deletes files in sub-folders as well.
- It is in debug mode for visual confirmation. To run it for real, remove the ECHO prefixing the DEL commands.
- Change SrcDir to your desired folder.
- Always do on a test folder first.

@echo off
setlocal enabledelayedexpansion

set SrcDir=H:\USERS\userProfiles
set FNDelPatt1=*.txt
set FNDelPatt2=*.pdf


for /d %%k in ("%SrcDir%\*") do (
   ECHO del /q /f /s "%%~fk\%FNDelPatt1%"
   ECHO del /q /f /s "%%~fk\%FNDelPatt2%"
)

Open in new window

0
 
Bob BrownIT Author Commented:
Hi NVIT  - I hate to bother you - but do you have any other suggestions. This does not work . What am I doing wrong

@echo off
setlocal enabledelayedexpansion

set SrcDir=D:\USERS\%userprofile%\IBMdump\AS400
set FNDelPatt1=*.txt
set FNDelPatt2=*.dmp


for /d %%k in ("%SrcDir%\*") do (
              del /q /f /s "%%~fk\%FNDelPatt1%"
              del /q /f /s "%%~fk\%FNDelPatt2%"
0
 
NVITCommented:
Quick guess ...You're missing the bottom closing parenthesis:

for /d %%k in ("%SrcDir%\*") do (
  del /q /f /s "%%~fk\%FNDelPatt1%"
  del /q /f /s "%%~fk\%FNDelPatt2%"
)

Open in new window


Also, posting your code with the CODE formatting as I've done here makes it easier to read.
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
Also, posting ERROR MESSAGES (rather than "does not work") is helpful in understanding what you're doing wrong or what the script is missing.
0
 
Bob BrownIT Author Commented:
Ok I think I see what the problem is . If I try to delete from IBM dump folder you will get the error message
 c:>Del D:\IBM dump\*.txt   The system can not find the file specified.  This is done from a prompt for testing.

 However, I can delete successfully from folders which have no space in the name.

So - How do I resolve this Issue ??

Because I believe this is what is stopping everyone suggestions from working.
0
 
Dean ChafeeIT/InfoSec ManagerCommented:
"The above posts work only for the active user's profile. "


True, but just need to adjust the path like this:

forfiles /p "H:\USERS\userProfiles\IBM dump\AS400\" /s /m *.dmp /c "cmd /c del @path && echo Deleted @path @fdate @fsize" 

forfiles /p "H:\USERS\userProfiles\IBM dump\AS400\" /s /m *.txt /c "cmd /c del @path && echo Deleted @path @fdate @fsize" 

Open in new window


I feel that forfiles is best for this job.
0
 
Dean ChafeeIT/InfoSec ManagerCommented:
c:>Del D:\IBM dump\*.txt   The system can not find the file specified.  This is done from a prompt for testing.

You need to add the /S switch:

Del /S D:\IBM dump\*.txt

And... you went from saying the files were here:
H:\USERS\userProfiles\IBM dump\AS400\*.dmp and *.txt.

to here
D:\IBM dump\*.txt

not sure your paths are correct
0
 
Bob BrownIT Author Commented:
When you specify the name of a folder that is split up into two words with a space in between  you will need to use "" on each end of the name.  Example DEL D:\"IBM dump"\*.txt . When you don't use "" you will get the path error mentioned previouly.
Then the command will start to work.

However I am still confronted with getting the command to work with a folder that has multiple user folders in it.
Can a wild card be used. ?? Example the folder "userprofiles" has the sub-folders TShine, RJohnson, MWebb.
How would I delete the dmp and txt files from their folders.

DEL H:\USERS\userprofiles\"IBM dump"\AS400\*.dmp
DEL H:\USERS\userprofiles\"IBM dump"\AS400\*.txt
0
 
NVITCommented:
the folder "userprofiles" has the sub-folders TShine, RJohnson, MWebb.
That's what my post addresses. I've tested it. It works.
0
 
NVITCommented:
Dean...


just need to adjust the path like this:

Your code doesn't address the OP's requirement of '...userProfiles  contains all of the “different user folders”'
0
 
Bob BrownIT Author Commented:
Your solution works
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.