Home > front end >  When I put a batch program in the task scheduler, it didn't finish the whole task, so why did t
When I put a batch program in the task scheduler, it didn't finish the whole task, so why did t

Time:01-18

This problem is from the scheduler task, batch program, or the files in a folder that I want to delete it using the batch program? and this is the batch program:

forfiles /p "D:\nameOfFolder" /s /m *.* /d -7 /c "cmd /c del @path"

CodePudding user response:

Are you sure you even have such files?

I tried the following (very similar) command:

forfiles /p "C:\Temp_Folder" /s /m . /d -7 /c "cmd /c echo @path"
ERROR: No files found with the specified search criteria.

One thing I noticed, is that the name of the directory should not end with a backslash:

forfiles /p "C:\Temp_Folder"  is working fine.
forfiles /p "C:\Temp_Folder\" is not working.

CodePudding user response:

. on its own is not a valid searchmask to supply to /m.
You ned to use *.* (all) *.ext (the supplied extension) *string*.* (files with names containing the string).
? is also supported as a searchmask to match any character. example: *.?a* would match any file with an extension type with a as the second character

CodePudding user response:

Replace /M *.* by /M * to even include files without extension, because forfiles treats wildcards differently than most other commands. /M * may even be omitted since this is the default setting anyway.

Regard that forfiles iterates both files and directories, so you need to exclude the latter, because del may try to delete its contents then. Therefore, implement a condition based on the forfiles-specific variable @isdir.

forfiles /S /P "D:\nameOfFolder" /M * /D -7 /C "cmd /D /C if @isdir==FALSE del @path"

As a side note, never append a trailing backslash to a (quoted) path, because something like /P "D:\" would let the command fail since \" constitutes an escaped quotation mark for forfiles, ruining the command line. You may however specify /P "D:\.".

  •  Tags:  
  • Related