deleting files only in sub folder using .batch - batch-file

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

Related

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

After deleting folders older than X days I get ERROR

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.

Batch file (ForFiles multiple conditional commands for logic)?

I am just getting into Bat files.
I am trying to delete old folders on a network shared drive but skip 2 of the containing folders by name.
Basically I need to all files that I make daily and always keep 2 old files.
Code that deletes all files that are older than 3 days:
PushD "\\****-****\build" &&(
ForFiles /D -3 /C "CMD /C if #ISDIR==TRUE echo RD #FILE &RD /S #FILE
) & PopD
And I was thinking something like this: if NOT #FNAME == %name%. I don't totally understand the process, am I able to have two conditions in the forFiles? do I have to have /c before?
PushD "\\****-****\build" &&(
ForFiles /D -3 /C "CMD /C if NOT #FNAME == %name% if #ISDIR==TRUE echo RD #FILE &RD /S #FILE
) & PopD
I can't seem to get it, would you mind helping me out?
Thanks!
Yes, nesting if commands in their then branches is the way how-to have logical AND. Note proper quoting in next code snippet:
#ECHO ON >NUL
#SETLOCAL enableextensions
set "name=SO"
set "nam2=SU"
pushd "D:\VB_scripts"
#rem all directories
ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE echo #FILE"
#rem all directories except "SO"
ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE if not #FNAME=="""%name%""" echo #FILE"
#rem all directories except "SO" and "SU"
ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE if not #FNAME=="""%name%""" if not #FNAME=="""%nam2%""" echo #FILE"
popd
#ENDLOCAL
Output:
==>D:\bat\SO\31346676.bat
==>set "name=SO"
==>set "nam2=SU"
==>pushd "D:\VB_scripts"
==>ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE echo #FILE"
"Class Pack"
"Oldies"
"SO"
"SU"
"WMI"
==>ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE if not #FNAME=="""SO""" echo #FILE"
"Class Pack"
"Oldies"
"SU"
"WMI"
==>ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE if not #FNAME=="""SO""" if not #FNAME=="
""SU""" echo #FILE"
"Class Pack"
"Oldies"
"WMI"
==>popd
It is a little bit messy but I was able to do it on a local directory:
#echo off
mkdir Temp\Temp
REM Copy all old file to Temp dir
forfiles -p "%cd%" -m *.* /D -2 /C "cmd /c xcopy #path %cd%\Temp"
REM copy a random file from Temp dir to Temp/Temp dir and then delete it
FOR %%A in (%cd%\Temp\*) do (
COPY "%%A" %cd%\Temp\Temp\
DEL "%%A"
GOTO :Second
)
:Second
Rem copy second random file from Temp dir
FOR %%A in (%cd%\Temp\*) do (
COPY "%%A" %cd%\Temp\Temp\
GOTO :Del
)
:Del
Rem delete all old files from local dir
forfiles -p "%cd%" -m *.* /D -2 /C "cmd /c del #path"
Rem Copy back two random old files to local dir
xcopy %cd%\Temp\Temp\* %cd%
Rem remove Temp dir
rmdir /s /q Temp

Forfiles - spaces in folder path

I am running a batch file and I have one forfiles command in it
FORFILES -p%spinputarchrootpath% -m*.csv -d-365 -c"CMD /C DEL #FILE"
%spinputarchrootpath% variable maps to a folder location (Y:\Temp Documents\testfolder).
Now the above command is throwing an error because of the space in the folder name (Temp Documents).
How to handle this space? I have tried putting quotes around %spinputarchrootpath% variable but it is not working.
I'd the same problem and found the solution.
I think your folder-variable of the folder you wish to empty has a backslash at the end.
This will NOT work:
echo J|forfiles /P "C:\temp files\" /S /M * /D -7 /C "cmd /c del /F /S /Q #path"
... but this works (without backslash)
echo J|forfiles /P "C:\temp files" /S /M * /D -7 /C "cmd /c del /F /S /Q #path"
Regards
Tino
Enclose the path in quotes:
FORFILES -p "%spinputarchrootpath%" -m *.csv -d -365 -c "CMD /C DEL #FILE"
Note, there's a space between -p and "%spinputarchrootpath%". Without a space in this case it won't work.
As a work around first change directories to the folder you want, and then execute forfiles without the /p parameter.
CD %spinputarchrootpath%
FORFILES -m*.csv -d-365 -c"CMD /C DEL #FILE"
Check post:
How to Tell FORFILES to Execute Command on Path?
The problem lies in the part:
-c"CMD /C DEL #FILE"
Use:
-c"CMD /C DEL ^0x22#FILE^0x22"
to put extra double quotes around the file

Resources