unable to delete the directory while executing a batch via cmd - batch-file

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.

Related

forfile and AD homefolders doesnt work

we have an active directory with about 80 users.
what i want to do now is place in each userfolder a script that the user can run for themselves and it makes a log file in that same folder with files older than 5 days.
The homefolder for each user is mapped as drive in windows to the letter h
right now i have this:
forfiles -p "%cd%" -s -m *.* /D -5 /C "cmd /c echo #path >> %cd%\log.txt"
but it throws me this error.
H:\>forfiles -p "H:\" -s -m *.* /D -5 /C "cmd /c echo #path >> H:\\log.txt"
ERROR: Invalid argument/option - '#path'.
is there any way to solve this?
if i run in on my local pc with a test folder it works fine.
When it comes to the closing doublequote, if you move it to before your redirection it certainly would make more sense, as would changing it to > instead of >>.
The fix to your issue, is when you use the /P option, you cannot doublequote it, and also include a trailing backslash, (it's one or the other).
My reading is that the backslash is escaping the doublequote.
Because drives tend to require a trailing backslash, all you have to do is remove the doublequotes around it, (they're not needed anyhow because drives don't include spaces).
Also please remember that, as already inferred in my comment, ForFiles already uses the current directory by default so removing it entirely is easier.

Deleting Files within Subdirectories using Batch Script

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.

Write a Batch file for automatically delete the OLD IIS log file

Whenever i want to delete the old IIS logs files on my Shared WebServer i run this command on CMD:
for /R C:\HostingSpaces %f in (u_ex*.log) do del /q "%~ff"
It searches for IIS log files and will delete all of them. For sure it can't delete the current day's log files since they are already opened in IIS and can't be deleted.
It shows me the progress of deleting log files one by one in command prompt console.
i need to set a schedule task to run a BAT file to do this automatically each day.
I have made a bat file and pasted this command within it:
for /R C:\HostingSpaces %f in (u_ex*.log) do del /q "%~ff"
But when i run the batch file nothing happens and i don't see any result and action.
How to write a batch file for running this Command?
Your help is much appreciate.
Thank you
You need to use %%A in batch files for For loops rather than %A at the command prompt.
del C:\HostingSpaces\u_ex*.log /s
is easier.
I strongly suggest to use the forfiles command for the task:
forfiles -p C:\inetpub\logs\LogFiles\ -s -m *.log -d -180 -c "cmd /C DEL #File"
Explanation of the switches:
-s or /S : recurse into all subfolders
-p or /P : path
-m or /M : file mask
-d or /D : number of days (-180 = older than 180 days)
-c or /C : command to execute
You can then put it into a Scheduled Task and have it run daily.
For a decent Powershell alternative, see this other answer: for other suggestions on how to properly reduce the IIS LogFiles folder, check out this post that I wrote on the topic.
The reason that you get error is because that the current day's logs are opened in IIS.
IIS keep the current day's log open and write on it constantly. So you will not be able to delete the current day's logs.
This is the command that worked for me. My websites are all placed in a folder named HOSTINGSPACES and i do this to delete all of the log files (only the current day logs will remain at the end)
# for /R C:\HostingSpaces %f in (u_ex*.log) do del /q "%~ff"
u_ex*.log is the syntax of my logs, your logs may have another syntax, so take care of this.

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