forfile and AD homefolders doesnt work - batch-file

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.

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"

How to substring the last slash of a directory string in batch file?

I wanted to remove the last forward slash of a directory string so that I use the same path for forfiles tool to delete old files. I have tried the solution here but it didn't work. The script saves the output to a text file so that I see the result. The forfiles doesn't work as the directory is incorrect format.
What I want to just to remove the last backward slash "C:\Database\Backup\".
The runable code below is what I have attempted. The problem is line 6, which outputs "C:\Database\Backup\" :~0,-1 instead of "C:\Database\Backup"
set BACKUP_DIR="C:/Database/Backup/"
set LOG_FILE=%BACKUP_DIR%log_file.txt
if not exist BACKUP_DIR mkdir %BACKUP_DIR%
set BACKWARD_SLASH_DIR=%BACKUP_DIR:/=\%
echo %BACKWARD_SLASH_DIR% > %LOG_FILE%
set DELETE_DIR=%BACKWARD_SLASH_DIR%:~0,-1%
echo %DELETE_DIR% >> %LOG_FILE%
forfiles -p %DELETE_DIR% -s -m *.* -d -1 -c "cmd /c echo Deleted #file" >> %LOG_FILE%
forfiles -p %DELETE_DIR% -s -m *.* -d -1 -c "cmd /c del #path"
This is a good example of why you should always set "variable=value" with the "variable=value" pair quoted. That way, the quotation marks aren't part of the variable value, and you can explicitly quote "%variable%" when necessary. It also removes ambiguity and ensures that there's no trailing whitespace inadvertently added to the end of the value. Matter of fact, in the code you pasted, there is an extra space at the end of your set BACKWARD_SLASH_DIR=%BACKUP_DIR:/=\% line.

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