Link to home
Start Free TrialLog in
Avatar of WellingtonIS
WellingtonIS

asked on

Deleting Files automatically via script

I have 2 bat files I created and scheduled to run.  The problem is when the scheduled task kicks off the script runs and nothing happens.  When I copy the information into a command prompt it does it job. What am I doing wrong?  
script 1 - del /q P:\Integrad\3.0\Data\spool\ResendDICOMImagesQueue_169.123.190.19\*
for /d %x in (P:\Integrad\3.0\Data\spool\ResendDICOMImagesQueue_169.123.190.19\*) do @rd /s /q "%x"

script 2 - del /q P:\Integrad\3.0\Data\spool\DicomSendJob_169.123.190.19\*
for /d %x in (P:\Integrad\3.0\Data\spool\DicomSendJob_169.123.190.19\*) do @rd /s /q "%x"
Avatar of oBdA
oBdA

"P:" is probably a mapped network drive.
Replace it with the share it's mapped to, and make sure that the account used to run the task has the required NTFS and Share permissions.
del /q \\Server\Share\Integrad\3.0\Data\spool\ResendDICOMImagesQueue_169.123.190.19\*
for /d %%x in (\\Server\Share\Integrad\3.0\Data\spool\ResendDICOMImagesQueue_169.123.190.19\*) do @rd /s /q "%%x"

Open in new window

Avatar of WellingtonIS

ASKER

It's actually a SAN connected to the device and it's a mapped drive - there's no server and share.  I don't think i can.. This has something to do with the actual scheduled task because I can run the script manually and it does it's job.
Wellington,
Have you checked Event Viewer? There might some clues in there.
what do you see in Task Scheduler?
When is last run and whats the result?

how did you setup jobs?
User generated image
How about this... Does anyone know a powershell command to delete subfolders within a folder:
The folder path is "P:\Integrad\3.0\Data\Spool\DicomSendJob_169.123.190.19
There are many subfolders that I need to delete in this folder.  So far everything I've tried doesn't delete the subfolders.  I can delete the entire root folder but then I have to re-create it.  Why is this so complicated???  Ive tried :
Get-ChildItem -Path P:\Integrad\3.0\Data\Spool\DicomSendJob_169.123.190.19 -Include *.* -Recurse | foreach { $_.Delete()}
doesn't seem to do it.
powershell
to delete all files recursively in a folder

Get-ChildItem -Path C:\Temp\X -Include *.* -Recurse | foreach { $_.Delete()}

Open in new window

The second line of the script above will do exactly that.
Note that I not only changed the drive to UNC, but as well the loop variable from %x to %%x. The single percent sign only works directly in the cmd prompt; in a batch script, two percent signs are required:
del /q \\Server\Share\Integrad\3.0\Data\spool\ResendDICOMImagesQueue_169.123.190.19\*
for /d %%x in (\\Server\Share\Integrad\3.0\Data\spool\ResendDICOMImagesQueue_169.123.190.19\*) do @rd /s /q "%%x"

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
powershell
delete all subfolders, keep files in target only... 

$path = "C:\Temp\X"
Get-ChildItem -Path $path -Directory -Recurse| Foreach-object {Remove-item -Recurse -path $_.FullName }

Open in new window

Why is this so complicated??? <<<< It will get better. Hang in there :-)
Sorry the task scheduler runs however, nothing happens.  But If I run the script manually it works just fine????
I"m using Get-ChildItem -Path P:\Integrad\3.0\Data\Spool\DicomSendJob_169.123.190.19 -Include *.* -Recurse | foreach { $_.Delete()} so let me try something else.
Sorry the task scheduler runs however, nothing happens.
can you show a screen shot of task scheduler

User generated image