After deleting folders older than X days I get ERROR - batch-file

I used this command:
FORFILES /S /D -10 /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
to delete all folders older than 10 days ,
and it is working fine.
problem is that I get an error and it's failing my build:
ERROR: The system cannot find the file specified.
I need to be able to exit with ERRORLEVEL=0.

Add echo before the command to see what's wrong (maybe it's trying to delete a subfolder of an already deleted folder):
FORFILES /S /D -10 /C "cmd /c IF #isdir==TRUE echo #path & rd /S /Q #path"
Or you can simply make it fail-proof by explicitly checking if the folder exists:
FORFILES /S /D -10 /C "cmd /c IF #isdir==TRUE if exist #path rd /S /Q #path"
BTW, forfiles method doesn't seem reliable as a folder date isn't updated for its 'grandchild' files. I would use robocopy in list-mode to generate the list of old files and then process it: get the folder path of each file and delete it if it still exists.

Related

Batch File path with an [at] # sign

I am trying to run a script to clear out the recycle bin on a QNAP NAS periodically.
The problem is the path for the recycle bin on the NAS includes an [at] sign:
"\nas01\SQLBackup\#Recycle" (Had to use double slash here to get it to display correctly)
Can someone please point me in the right direction as to what I'm doing wrong?
Thanks in advance for your help.
Batch File Code
#ECHO ON
NET USE X: "\\nas01\SQLBackup\#Recycle"
forfiles /p "X:\" /s /m * /c "cmd /c del #path"
NET USE X: /delete
PAUSE
Output
C:\Windows\system32>NET USE X: "\\nas01\SQLBackup\#Recycle"
The command completed successfully.
C:\Windows\system32>forfiles /p "X:\" /s /m * /c "cmd /c del #path"
ERROR: Invalid argument/option - '#path'.
Type "FORFILES /?" for usage.
C:\Windows\system32>NET USE X: /delete
X: was deleted successfully.
C:\Windows\system32>PAUSE
Press any key to continue . . .
The reason you get the error is due to the double quotes around the path, where backslash is present. The backslash escapes the last double quote. You should either use:
forfiles /p "X:" /s /m * /c "cmd /c del #path"
or
forfiles /p X:\ /s /m * /c "cmd /c del #path"
anyway, I would not use forfiles at all here. You can quite simply use del /s:
net use X: "\\nas01\SQLBackup\#Recycle"
pushd "x:\">nul 2>&1 && del /Q /S *.* || echo X:\ Not available.
popd
net use X: /delete
pause
Quite simply, we attempt pushd to x:\ if not available, it will fail with a message, if available it will del /s everything on X:\ where /s is basically recursive search throughout the root of X:\ in this instance.
The #-symbol in your path does not cause the error. It is the backslash in:
forfiles /p "X:\" /s /m * /c "cmd /c del #path"
that unintentionally escapes the closing quote (this is specific to forfiles!). To avoid that, simply append a . to the path, like:
forfiles /p "X:\." /s /m * /c "cmd /c del #path"
The . means current directory, so in a path it does not change anything, hence X:\ equals X:\., and D:\some\.\path equals D:\some\path. Of course, you could just remove the quotes around X:\ in your particular situation, but appending . is a general solution that even works with a relative path like X: (meaning the current directory of drive X:), and removal of quotes introduces problems with paths containing SPACEs.
By the way, are you aware that forfiles returns both files and directories, and that del is there to delete just files, and there is rd to delete directories?
So to ensure to handle only files, use this:
forfiles /S /P "X:\." /M * /C "cmd /C if #isdir==FALSE del #path"
And to ensure to handle only directories, use this:
forfiles /S /P "X:\." /M * /C "cmd /C if #isdir==TRUE rd /S /Q #path"
Of course you can handle both, if you want:
forfiles /S /P "X:\." /M * /C "cmd /C if #isdir==TRUE (rd /S /Q #path) else (del #path)"
When you use PushD, it will create a temporary drive map, (allocated in available reverse alphabetical order, Z:..A:), and will then use that new drive. For that reason you should be able to do this without using net.exe.
Example:
#PushD "\\nas01\SQLBackup\#Recycle" 2> NUL && (RD /S /Q . 2> NUL & PopD)
This example uses RD to Remove the Directory instead of your used Del command. When the target directory is the current working directory, it cannot be removed, (returning an error message), however its contents will be. The code above redirects the error message to the NUL device, so that it is not output.

Batch File: Deleting .File Extension Type Files

I'm working with an application that creates a generic file type extension for data log information. I'm trying to create a batch file script that would delete data log files that are 5 days or older. I don't have the most Batch File experience, but I have found the following script below works correctly for many file types, except for the generic .File type extension.
forfiles /p "C:\SOAP_Data" /s /m *.* /d -5 /c "cmd /c del #path" &
forfiles /p "C:\HL7_Data" /s /m *.* /d -5 /c "cmd /c del #path"
Is there some tweak I can do to the script to make it include the deletion of the .File extension files as well? I've tried to add *.file to the folder path, but that didn't seem to work.
Through troubleshooting using the ECHO of the delete path, I was able to find the following solution:
forfiles /p "C:\SOAP_Data" /d -5 /c "cmd /c DEL #path" &
forfiles /p "C:\HL7_Data" /d -5 /c "cmd /c DEL #path"

Adapting forfiles command to look through folders instead of files

I have written a backup command that deletes files within a E:\Backup\ldap directory that are older than two days. I have tested and this works fine.
(forfiles /p E:\Backup\ldap /s /d -2 /c "cmd /c echo #path >> E:\Backup\files_deleted_log_%date:~-10,2%%date:~-7,2%%date:~-4,4%.txt & cmd /c del /q #path")
I am in the process of adapting this command so that it looks at the top level folders within the specified directory and recursively deletes the folder and its contents if it is older than two days.
(forfiles /p E:\Backup\Sybase /d -2 /c "cmd /c if #isdir == true echo #path >> E:\Backup\files_deleted_log_%date:~-10,2%%date:~-7,2%%date:~-4,4%.txt & cmd /c if #isdir == true del /s /q #path")
I took out the first /s from the previous file command as I do not want to search recursively, I only want to look at the last modified dates of the top level folders within the E:\Backup\Sybase directory.
I also added if #isdir == true to determine whether the file is a folder and updated the delete command to include /s so that a recursive delete is performed.
Each time I execute the command though, nothing is deleted.
I used if #ISDIR==TRUE to determine whether the file is a directory before using RD to recursively remove the directory.
(forfiles /p E:\Backup\Sybase /d -2 /c "CMD /C if #ISDIR==TRUE echo #PATH >> E:\Backup\files_deleted_log_%date:~-10,2%%date:~-7,2%%date:~-4,4%.txt & if #ISDIR==TRUE RD /Q /S #FILE")

deleting files only in sub folder using .batch

i have a question. I need to delete all files in subfolders but not the sub folder or the main folder older than 1 day.
#echo off
forfiles -p "C:\Users\remote\Downloads" -s -m *.* /D -1 -c "cmd /c del #path"
i have this code which delete all files older than 1 day in downloads but what i need /seek is:
C:\users\remote\downloads\%variable%
and that only the files whitin variable will be deleted. i have many "variables".
it is for my work and as a test i do it local.
FOR /D will allow you to iterate over subdirectories. How about
FOR /D %d IN (C:\Users\remote\Downloads\*) DO ( forfiles /P "%~d" /D -1 /C "cmd /c del #path" )
You'll need to "escape" the percent sign in the iteration variable if you put this command in a script:
FOR /D %%d IN (C:\Users\remote\Downloads\*) DO (
FORFILES /P "%%~d" /D -1 /C "cmd /c del #path"
)

Batch file to delete folders older than 10 days in Windows 7

I want to create a batch file which should delete all subfolders of a folder which are older than 10 days, using Windows 7
Any help would be appreciated.
Adapted from this answer to a very similar question:
FORFILES /S /D -10 /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
You should run this command from within your d:\study folder. It will delete all subfolders which are older than 10 days.
The /S /Q after the rd makes it delete folders even if they are not empty, without prompting.
I suggest you put the above command into a .bat file, and save it as d:\study\cleanup.bat.
FORFILES /S /D -10 /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
I could not get Blorgbeard's suggestion to work, but I was able to get it to work with RMDIR instead of RD:
FORFILES /p N:\test /S /D -10 /C "cmd /c IF #isdir == TRUE RMDIR /S /Q #path"
Since RMDIR won't delete folders that aren't empty so I also ended up using this code to delete the files that were over 10 days and then the folders that were over 10 days old.
FOR /d %%K in ("n:\test*") DO (
FOR /d %%J in ("%%K*") DO (
FORFILES /P %%J /S /M . /D -10 /C "cmd /c del #file"
)
)
FORFILES /p N:\test /S /D -10 /C "cmd /c IF #isdir == TRUE RMDIR /S /Q #path"
I used this code to purge out the sub folders in the folders within test (example n:\test\abc\123 would get purged when empty, but n:\test\abc would not get purged
If you want using it with parameter (ie. delete all subdirs under the given directory), then put this two lines into a *.bat or *.cmd file:
#echo off
for /f "delims=" %%d in ('dir %1 /s /b /ad ^| sort /r') do rd "%%d" 2>nul && echo rmdir %%d
and add script-path to your PATH environment variable. In this case you can call your batch file from any location (I suppose UNC path should work, too).
Eg.:
YourBatchFileName c:\temp
(you may use quotation marks if needed)
will remove all empty subdirs under c:\temp folder
YourBatchFileName
will remove all empty subdirs under the current directory.

Resources