I am trying to delete old files and folders in my Google Drive using a bat script on Windows 10 Home.
forfiles /M *.* /P "C:\Users\bruker\Google Drive\" /S /D -180 /C "cmd /c del /F /Q #path"
Gives the error message:
ERROR: Invalid argument/option - '/F'. Type "FORFILES /?" for usage.
Anyone have a solution for this?
The quotes aren't needed. Both forfiles ... /C cmd /c del /F /Q #path and forfiles ... /C (cmd /c del /F /Q #path) should work.
Related
I need a Windows script to delete files older than 1 year with:
forfiles /S /P "E:\" /M *.* /D -365 /C "cmd /c echo #path" >> "xxx_log"
I am not familiar with Windows scripting.
Can anyone please help me?
If you plan on using windows scripts/batch file, but you are not familiar with them, I really recommend you familiarize yourself first, otherwise it may be dangerous to play with, especially when deleting in bulk...
Fist of all open a Command Prompt and type FORFILES /? to read through the help for the FORFILES command and DEL /? for the DEL (delete) command.
The complete command you are looking for is something like this:
FORFILES /S /P "E:\" /M "*" /D -365 /C "CMD /C DEL #path"
Where E:\ is the top level directory, * is the wildcard you want to match (e.g. change to *.bak to delete only files with the extension .bak) and 365 is the number of days (the minus sign means delete anything older than 365 days).
As well as the invalid /Q option you had used (now corrected), I would also advise that there is no need to use the /M option, as it's default is already *. Also, you may also wish to include some other options for your DEL command, such as its /A and /F options. Additionally, since the intention is to delete files, you should omit possible directory paths, using #ISDIR too. Example: FORFILES /P "E:" /S /D -365 /C "CMD /C IF #ISDIR==FALSE DEL /A /F #PATH". – Compo yesterday
Thank you for your answer. Can I use the day to replace 365 ?
FORFILES /P "E:" /S /D -"01/01/2020" /C "CMD /C IF #ISDIR==FALSE DEL /A /F #PATH" ? Thank you.
I have a script to delete .zip files more than 30 days:
#echo off
FORFILES /p "c:\respaldos" /M *.zip /D -30 /C "cmd /c del #file"
I hope this help you, for more information write in CMD FORFILES /?
I have created a batch script to delete 7 days older files as follows:
ForFiles /p "C:\Users\John.Kapen\Desktop" /s /d -7 /c "cmd /c del #file"
But this is deleting executable files and folders as well.
I tried below command to exclude executable file deletion. But not working:
for /f %%F in ('C:\Users\John.Kapen\Desktop /b /d -7 /a-d ^| findstr /vile ".exe"') do del "%%F"
Could someone help me here?
The forfiles /? "manpage" contains following information:
...
#ext - returns only the extension of the
file.
...
Examples:
...
FORFILES /M *.txt /C "cmd /c if #isdir==FALSE notepad.exe #file"
You can try something like (not tested):
ForFiles /p "C:\Users\John.Kapen\Desktop" /s /d -7 /c "cmd /c if NOT #ext=="""exe""" del #file"
F.e. there is some folder on user's desktop. I want to remove any files within this folder except shortcuts. There are two types of shortcuts - to some local\network resources and shortcuts to some sites.
"shortcuts on some local\network resources" have .lnk extension
"shortcuts to some sites" have .url extension
I've already found how to do this task separately
forfiles /p "%userprofile%\Desktop\folder" /s /m *.* /c "cmd /c for %G in (#path) do #if /I [%~xG] neq [.lnk] del /F /Q %G"
and
forfiles /p "%userprofile%\Desktop\folder" /s /m *.* /c "cmd /c for %G in (#path) do #if /I [%~xG] neq [.url] del /F /Q %G"
But how to combine these conditions into one string? Something like if /I [%~xG] neq [.lnk] AND [.lnk] ...
There's absolutely no need to use a for-loop within your forfiles /C command.
Try either;
ForFiles /P "%UserProfile%\Desktop\folder" /S /C "Cmd /C If /I Not #ext==\"lnk\" If /I Not #ext==\"url\" Del /A /F #path"
Or use the hex codes as shown in the help information, available at the Command Prompt by entering, forfiles /?
ForFiles /P "%UserProfile%\Desktop\folder" /S /C "Cmd /C If /I Not #ext==0x22lnk0x22 If /I Not #ext==0x22url0x22" Del /A /F #path"
#ECHO OFF
SET backdir=backup
SET snapshotdir=snapshots
SET worldprefix=world_
SET itdate=%date:~10,4%-%date:~4,2%-%date:~7,2%
SET hour=%time:~0,2%
IF "%hour:~0,1%" == " " SET hour=0%hour:~1,1%
echo Current date: %itdate%. Current hour: %hour%. Current Minute:Second: %time:~3,2%:%time:~6,2%
forfiles /m "%worldprefix%*" /c (
echo Copying World: #path
cmd /c xcopy /e /c /h /i /v /r /y /q #file %snapshotdir%\#file\%itdate%-%hour%-%time:~3,2%-%time:~6,2%
cmd /c xcopy /e /c /h /i /v /r /y /q #file %backdir%\%itdate%D\worlds\#file
)
echo Copying Plugins
xcopy /e /c /h /i /v /r /y /q plugins %backdir%\%itdate%D\plugins\
xcopy /e /c /h /i /v /r /y /q %backdir%\%itdate%D %backdir%\%itdate%-%hour%H\
echo Backup Complete (assuming no errors above). Attempting to remove old files..
forfiles /p "%snapshotdir%" /c "cmd /c rmdir /s /q #path" /d -7
forfiles /p "%backdir%" /m "*H" /c "cmd /c rmdir /s /q #path" /d -2
forfiles /p "%backdir%" /m "*D" /c "cmd /c rmdir /s /q #path" /d -14
PAUSE
I am trying to copy all files with "world_" as a prefix. I run into a problem when I try to use multiple commands in a loop. I have attempted to write the batch script I want above.
The two commands have absolutely nothing in common, so no, you cannot use parentheses like that.
You must execute all the commands within a single CMD /C. You can concatenate commands on one line using &. I've defined a simple XCOPY "macro" to save a bit of typing.
set XCOPY=xcopy /e /c /h /i /v /r /y /q"
forfiles /m "%worldprefix%*" /c "cmd /c echo Copying World: #path&%XCOPY% #file %snapshotdir%\#file\%itdate%-%hour%-%time:~3,2%-%time:~6,2%&%XCOPY% #file %backdir%\%itdate%D\worlds\#file"
If you escape the quotes, then you can use line continuation to split the logical line accross multiple lines. But then you must also escape the &.
set XCOPY=xcopy /e /c /h /i /v /r /y /q"
forfiles /m "%worldprefix%*" /c ^"cmd /c ^
echo Copying World: #path ^&^
%XCOPY% #file %snapshotdir%\#file\%itdate%-%hour%-%time:~3,2%-%time:~6,2% ^&^
%XCOPY% #file %backdir%\%itdate%D\worlds\#file^"
Or you could put the & in the front of the line so that you don't need to escape it. The line continuation also escapes the first character of the next line:
set XCOPY=xcopy /e /c /h /i /v /r /y /q"
forfiles /m "%worldprefix%*" /c ^"cmd /c ^
echo Copying World: #path^
&%XCOPY% #file %snapshotdir%\#file\%itdate%-%hour%-%time:~3,2%-%time:~6,2%^
&%XCOPY% #file %backdir%\%itdate%D\worlds\#file^"
I realize this is an old post, but another way of doing it is to call a batch file and pass it the parameters that it needs. And I do not believe that there is any limitations in what can be in that batch file. For example:
forfiles /M "*.img" /C "cmd /c ProcessFile.bat #file #fdate #ftime"
I tried to make batch file in windows by using task schedule once a month to find *.bak. With condition more then 30 old. I create two condition full path name and non-8dot3 path name. For those unable to delete it be recorded to a TXT file.
Here the command I found:
forfiles /P E:\WP /S /M *.bak /D -30 /C "cmd /C del #path"
Here is my command for delete using non-8dot3 file names:
forfiles /P E:\WP /S /M *.bak /D -30 /C "cmd /C for %A in (#path) do #echo del %~sA
For file can't be delete because it to long path and file name, it recorded to a TXT file with long full path file name and non-8dot3:
forfiles /P E:\WP /S /M *.bak /D -30 /C "cmd /c echo #path >> list.txt"
forfiles /P E:\WP /S /M *.bak /D -30 /C "cmd /c for %A in (#path) do #echo %~sA" >> list.txt
These 4 command are working well if I just copy and paste into dos command prompt.
But when I put them into batch file e.g. "del30days.bat", it does not working for non-8dot3 file names:
#echo off
:: Set host computer name
set host=%COMPUTERNAME%
:: Set save list path
set list_path=F:\LogFiles
:: Set min age of files and folders to delete
set file_list=ListCantDeleted-%host%-%date:~10,4%-%date:~7,2%-%date:~4,2%.txt
:: Set target folder path
set target_path=E:\WP
:: Set min age of files and folders to delete
set max_days=30
:: Set what kind files or extension
set file_ext=*.bak
:: Delete files from target path
forfiles /P %target_path% /S /M %file_ext% /D -%max_days% /C "cmd /C del #path"
forfiles /P %target_path% /S /M %file_ext% /D -%max_days% /C "cmd /C for %A in (#path) do #echo del %~sA"
:: Record files from target path
forfiles /P %target_path% /S /M %file_ext% /D -%max_days% /C "cmd /c echo #path >> %list_path%\%file_list%"
forfiles /P %target_path% /S /M %file_ext% /D -%max_days% /C "cmd /C for %A in (#path) do #echo %~sA >> %list_path%\%file_list%"
I got this error on non-8dot3 files name but not on long file name on record to a TXT file, by mean it can't delete file using non-8dot3 file names as well.
~sA" was unexpected at this time.
Any reason why?
The goal I create this is to delete *.bak file more then 30 days without any issue on long path and file name. Anyone got simplify solution for this?
You're going to slap yourself. The problem is that in a batch file, for loop variables need a double percent. %%A and %%~sA.