Deleting Files within Subdirectories using Batch Script - batch-file

Here is the script that I am currently working with:
forfiles -p E:\datatel\coll18\developmentSQL\apphome\_HOLD_\ -s -m * /D -28 /C "cmd /c del #path"
The -s should allow me to delete files within the sub-directories without removing the folders, but instead I receive a "No files found" error. Am I overlooking something simple? I've tried switching the - to a / for continuity, but the results are the same.

Related

unable to delete the directory while executing a batch via cmd

I'm having trouble deleting the sub-directory, while I'm executing the following batch via cmd. The mentioned dir is getting deleted, and I'm getting the relevant log file of the deleted files, but the sub-dir isn't getting deleted. Since it ain't getting deleted, I don't have a proper log file.
Here is the code that I'm executing,
#echo off
SETLOCAL
:: set folder path
set dump_path=%1
:: set min age of files and folders to delete
set max_days=%2
shift
shift
:: remove files from %dump_path%
forfiles -p %dump_path% -m *.* -d -%max_days% -c "cmd /c del /q #path && echo #path>>logfile.log"
:: remove subdirectories from %dump_path%
forfiles -p %dump_path% -m *.* -d -%max_days% -c "cmd /c IF #isdir == TRUE rd /S /Q #path && echo #path>>logfile.log"
Any help is appreciated.
Below is the pic, in that further, I added a folder with some contents to check, but it ain't working.
More info, as it still ain't working, any way to change it into a PowerShell execution?
Below pic, before executing the cmd,
Post execution of the cmd,
Still, I'm unable to understand why it isn't removing the sub-dir files...This time, I removed the sub-dir cmd from my batch and added a '/S' to my first cmd.
There are two problems with your batchfile:
You mention you want to remove the files with are older than 15 days, but in your batchfile you remove the files which are more recent than 15 days (there's a minus in front of the %max_days%, not a plus, you need to change that).
You mention launching the batchfile, using 15>nul as a second parameter, which is wrong. It should simply be 15.

Delete all *.Zip files older than today using Windows batch file

Assuming you must use a Windows batch file, (not powershell), and one wants to delete all files ending in .zip that are in the current active directory. How to do this?
All attempts so far are failing:
forfiles -p "C:\temp\test" -s -m *.zip -d 1 -c "cmd /c del *.zip"
For this it says
ERROR: No files found with the specified search criteria.
As suggested in my comment, your problem could easily be solved by reading the usage information for your command, (available when entering FORFILES /? at the Command Prompt).
Based on your questions criteria, "delete all files ending in .zip that are in the current active directory":
You don't need to use the /P option because as stated in the usage information, "The default folder is the current working directory (.)".
There is nothing in your question regarding recursing subdirectories of the current directory, so the /S option which "Instructs forfiles to recurse into subdirectories" is not required.
For the /D option you are looking for files with a last modified date less than yesterday, i.e. "the current date minus "dd" days", /D -1.
Because you're wanting to delete files in the current directory, there's no need to use the "Full path of the file", #path, so what you need is the "The name of the file", #file.
FORFILES /M *.zip /D -1 /C "CMD /C DEL #file"
You did not mention anything about subdirectories or windows version so I'm assuming somewhat. You have an old version syntax. In Windows 7 and further the syntax changed a little bit.
For windows 7:
forfiles /P "C:\temp\test" /S /M *.zip /D -1 /C "cmd /c del #path"

findstr or grep output to a file

Im in Windows Server 2003 and using below commands to fetch string matching lines in files created today in a specific directory.
forfiles -p D:\ -m *.1 -d +0 -c "cmd /c findstr /i /c:\"Hey Hi\" #FILE" >> txt5.txt
or
forfiles -p D:\ -m *.1 -d +0 -c "cmd /c grep \"Hey Hi\" #FILE" >> txt5.txt
Geeting error 'FINDSTR: Write error' and 'grep write error bad file descriptor' respectively for both commands. So basically the commands work i.e. display the output in screen but unable to redirect the output to a file.
I did not find suitable solution though users reported this same error for different scenarios. Any help is appreciated. Thanks in advance!
You should be able to remove the cmd /c; I don't see any reason you'd need a new copy of the command shell open for the findstr call.
This works for me correctly at the command prompt:
forfiles -m t*.xml -d +0 -c "findstr /i "Item" #file" >> out.txt.
It produces an out.txt file that contains the proper content matching the search criteria.

If Batch File Modified Date is older than 90 days, delete files

Basically what I'm trying to do is create a batch file and place it in my startup that will use the modified date of the same batch file and see if it is greater or less than 90 days old. If it isn't, nothing happens and life goes on, but if it is, I want it to delete the contents of my downloads folder. I have a little bit of coding, but I've been testing it, but for some reason it isn't wanting to work for me. Any help would be greatly appreciated.
forfiles -p "C:\LOCATION OF .BAT\" -s -m rmdownload.bat /D -90 /C "cmd /c del C:\user folder\Downloads"
I'm just starting out using batch commands and would really love the help
This works for you if you are using Win2003 or WinXP and have forfiles.exe installed on your machine...
forfiles -p "C:\path_of_your_bat" -s -m rmdownload.bat -d -90 -c "cmd /c del C:\user folder\Downloads"
Later versions of Windows and Windows Server have it installed by default.
For Win7 or higher: Syntax has changed a little therefore the updated command is:
forfiles -p "C:\path_of_your_bat" -s -m rmdownload.bat /D -90 /C "cmd /c del C:\user folder\Downloads"
Assuming your target path contains spaces etc, then use this in your forfiles command. The 0x22 represents a double quote character.
If it echos the command then try it without the echo to actually perform the deletion
"cmd /c echo del 0x22C:\user folder\Downloads\*.*?0x22"

Forfiles is not working in batch file but it is working in command line

FORFILES -pc:\tempfolder -s -d-6 -m* -c "CMD /C if #ISDIR==TRUE RD /S /Q #FILE"
is not working in batch file but it is working in command line
I am using older version of forfiles for windows xp. As mentioned above forfiles is working in command prompt, but when i copy the same command to batch file it's giving can't execute (error 2).
http://web.archive.org/web/20150527024532/http://www.sharedcache.com/cms/tips_and_tricks.aspx
Click Download
Drop Forfiles.exe C:\
C:\forfiles.exe -p "C:\Documents and Settings\test\My Documents\Downloads\Test file" -s -m . /C "cmd /c del #path" /d -14
Copy a old computer .exe file in the above location (don't drag it or you lose it forever.)
Change the "-14" to any days with a newer file in the same location, (make sure you copy it) run the batch file.
Now change "-14" to "-1", both files should be gone if you did it correctly.
Now set up a schedule to run it every whatever days for your heart contempts.
wrote this here bc I keep running in here from google and so many sites doesn't say you needed fortfiles.exe program itself for the cmd prompt from seeing any errors.
You can test the file your self and list the files wherever forfile.exe is located in. It is safe to run bc it doesn't have a cmd batch file to delete.
Enjoy!
ps. you can Drop the "forfiles" here "C:\WINDOWS\system32\"
without having to add "C:\forfiles.exe" in the cmd prompt and run it.

Resources