Delete specific file and copy file to folder using forfiles - windows

I need to delete a specific file from the folder (\10.0.1.223\ClamAV_DB) then paste in a file to the folder (\10.0.1.223\ClamAV_DB) from (C:\ProgramData.clamwin\db) using forfiles. But it seems not working. I am using Windows Server 2012 R2 Standard.
FORFILES /P \\10.0.1.223\ClamAV_DB /M daily.cld /D -0 /C "cmd /c del #PATH"
FORFILES /P C:\ProgramData\.clamwin\db -M daily.cld /D 0 /C "cmd /c copy #PATH \\10.0.1.223\ClamAV_DB

Got it working below.
net use A: \\10.0.1.43\ClamAV_DB
forfiles /P A:\ /M daily.cld /C "cmd /c del #PATH"
forfiles /P C:\ProgramData\.clamwin\db -M daily.cld /C "cmd /c copy #PATH A:\

Update:
Because its UNC path, use PushD. Something like this.
PushD "\\10.0.1.223\ClamAV_DB" &&(
FORFILES -M daily.cld /D -0 /C "cmd /c del #file"
) & PopD
FORFILES -P "C:\ProgramData\.clamwin\db" -M daily.cld /D -0 /C "cmd /c copy #file \\10.0.1.223\ClamAV_DB"

Related

I want to delete a stale files which is >365 days along with print the list of files are deleted in cmd via batchfile(.bat)

forfiles /p "C:\Testing" /s /m *.* /D -365 /C "cmd /c del #path"
above code just deleting the files which is >365 and didn't show/print anything

How to delete files in a location except executable files

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"

Deleting old files in Windows 10 using forfiles

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.

How to use forfiles to delete files without extension

I am using forfiles to delete files older than 7 days from a specific directory using the following script found elsewhere on this forum:
forfiles -p "H:\E-drev" -m *.* /D -7 /C "cmd /c del #path"
This Works fine except I have some files with no extension eg. a file named TA07l. This file is not deleted. I have tried using #fname instead of #path but this does not help.
Any advice would be greatly appreciated.
The following should work:
forfiles /P "H:\E-drev" /M * /D -7 /C "cmd /C if #isdir==FALSE if #ext==\"\" del #path"
You have to use * instead of *.* because otherwise it will search every files that contain a dot .
Update with few examples between * and *. and *.*:
copy nul _onefilewithoutext
copy nul _onefilewith.ext
mkdir _oneFolder
dir /b /a-d *.
_onefilewithoutext
Forfiles command
forfiles /M *. /C "cmd /C echo #relpath"
Error: File Type "*." not found.
forfiles /M * /C "cmd /C echo #relpath"
".\_onefilewith.ext"
".\_onefilewithoutext"
".\_oneFolder"
forfiles /M *.* /C "cmd /C echo #relpath"
".\_onefilewith.ext"
forfiles /M * /C "cmd /C if #isdir==FALSE echo #relpath"
".\_onefilewith.ext"
".\_onefilewithoutext"
forfiles /M * /C "cmd /C if #isdir==FALSE if #ext==\"\" echo #relpath"
".\_onefilewithoutext"

How to use multiple commands in batch using forfiles command?

#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"

Resources