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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bartender_1Microsoft 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
NVITEnd-user supportCommented:
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
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

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
NVITEnd-user supportCommented:
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
NVITEnd-user supportCommented:
the folder "userprofiles" has the sub-folders TShine, RJohnson, MWebb.
That's what my post addresses. I've tested it. It works.
0
NVITEnd-user supportCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NVITEnd-user supportCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.