How to record purged files into a log file - batch-file

I am using the following code in a batch file to purge any files older than 60 days. I would like the log file to print the file names that have been purged for record keeping purposes. How can I do that?
setlocal EnableDelayedExpansion
set logpath=C:\Temp\Archive
REM if not exist %logpath% md %logpath%
set FILEAGE=60
set archlog=%logpath%\Accurate_ARCHIVE.txt
set inputdir=C:\Temp
REM Set the date
FOR /f "tokens=2,3,4 delims=/ " %%a in ('date/t') do (set DD=%%a) & (set MM=%%b) & (set YYYY=%%c)
REM * Purge files older than %FILEAGE% days from disk.
forfiles /p "%inputdir%" /s /m *.* /d -%FILEAGE% /c "cmd /c del #path"
ECHO Folder %inputdir% was processed on %DD%-%MM%-%YYYY% >> %archlog%
ECHO Files older than %FILEAGE% days have been purged. >> %archlog%
exit %errorlevel%
I am expecting the log file to print the names of the files purged.

Add echo #path to the del #path command.
Something like below (untested).
"cmd /c (del #path) & (>> %archlog% echo #path)"
A more advanced version incorporating comments by Aschipf and Compo.
The 0x22 puts doupble quots around the filename and prevents problems with space and other special characters in the filename.
Deleted files are reported and files that could not be deleted are reported prefixed with NOT DELETED:.
"cmd /c if #isdir==FALSE (del #path) & (>> 0x22%archlog%0x22 if exist #path (echo NOT DELETED: #path) else (echo #path))"

Related

Get all subfolders and find which are modified today using batch file

I have a folder MyFolder and it contains 3 sub-folders say A,B and C.
I want to copy those sub-folders which are modified today.
The command for /D %%A in ("D:\MyFolder*") do ( echo **%%~fA )** provides me all 3 sub-folders, now I am calling another loop and passing %%~fA(which is current folder in loop) to get to know is there any modifications made in this folder or not. But I am getting echo off.
set LocalFolder=D:\Backup\1
for /D %%A in ("D:\MyFolder\*") do (
echo %%~fA
for /F %%N in ('forfiles /S /P "%%~fA" /M "*" /D +0 /C "cmd /C if #isdir==FALSE echo _" ^| find /C "_"') do set "NUMBER=%%N"
echo %NUMBER%
if %%N GTR 0 xcopy "%%~fA\*.*" "%LocalFolder%\" /s/e/k/f/c
)

How to I delete from all Sub Directories apart from a select few

I would like to create 1 batch file that deletes a type of file (i.e. ".bak") with different date ranges situated in different sub folders that are in the same patent folder, but I can list the minority of folders.
I have managed to create a FOR loop to look in certain directories (which the directories are set in a variable), deleting any '*.bak' file over a certain age with the below script:
SET "WeeklyBAKLocation=Folder20 Folder21 Folder25 Folder28"
SET WeeklyBAK=7
SET DailyBAK=3
FOR %%x IN (%WeeklyBAKLocation%) DO forfiles /p "%%x" /s /m *.bak /d -%WeeklyBAK% /c "cmd /c DEL #path /q"
<SECOND FOR LOOP below HERE>
The above works deleting .bak files older than 7 days in those folders, my question is how can I reverse that and delete '.bak' files in every other directory that's older than 3 days, without deleting the ones kept from the first query?
I tried nesting FOR and FORFILES, using findstr /v to ignore them as below (but it would ignore each directory for that 1 loop, and then delete it for the next loop wouldn't it, so eventually it would not have worked and is massively inefficient?)
SET "WeeklyBAKLocation=Folder20 Folder21 Folder25 Folder28"
SET WeeklyBAK=7
SET DailyBAK=3
<First FOR LOOP above HERE>
FOR %%x IN (%WeeklyBAKLocation%) DO FOR /F "delims=*" %%G IN ('dir /b /s "%~dp0" ^| findstr /v "\%%x"') DO forfiles /p "%%G" /s /m *.bak /d -%DailyBAK% /c "cmd /c ECHO #path /q"
Essentially can I ignore SOME directories at once when walking through them to find .bak files to delete only ones that are 3 days old.
Many thanks in advance.
FOR /F "delims=*" %%G IN (
'dir /b /s /AD "%~dp0" ^| findstr /v "\%%x"') DO (
set "zapme=Y"
FOR %%x IN (%WeeklyBAKLocation%) DO if /i "%%x"=="%%G" set "zapme="
if defined zapme forfiles /p "%%G" /s /m *.bak /d -%DailyBAK% /c "cmd /c ECHO #path /q"
)
read the directory list in basic form (note /ad) and for each directory, set zapme to something then check whether the directory is not to be processed and set zapme to nothing if it's one of those directories. The zapme flag can then be tested for being set or not set. If it's set, go ahead and process the directory.
FOR /F "delims=*" %%G IN (
'dir /b /AD "%sourcedir%"') DO (
set "zapme=Y"
FOR %%x IN (%WeeklyBAKLocation%) DO if /i "%%x"=="%%G" set "zapme="
if defined zapme ECHO forfiles /p "%sourcedir%\%%G" /s /m *.bak /d -%DailyBAK% /c "cmd /c ECHO #path /q"
)
...happens every time I don't actually test it...

How to delete all empty folders which are older than 2 days?

I make a Script which deletes all empty Folders with Subfolders in a Path.
Now i have to make, if a folder was created 2 days ago and its empty it should be deleted with the other empty folders that are older than 2 Days.And if not it should be not deleted.
And i also need/want to make that deleted Folder are written in a Log.
I made that with the 5 Filetypes but i dont know how this schould work with the Folders.
Im really new to Batch, so i dont know what i should do.
I checked Google but the Results did not match with my problem.
I hope someone can help me.
Here is my Code that i´ve written so far:
#echo off
::start path / Variables
set startdir="C:\Users"
::Initialize the Variable
set /a loop=0
::Directory c:\temp\ will be created, if the folder not exists
if not exist c:\temp\ md c:\temp\
::Create Logfile for Deleted Filetypes in C:\Log\LOG_Useless_File_Killer.txt
echo ----------------------------------- >> C:\Log\LOG_Useless_File_Killer.txt
echo Logfile from: %date% at %time% >> C:\Log\LOG_Useless_File_Killer.txt
echo. >> C:\Log\LOG_Useless_File_Killer.txt
::this 5 Filetypes are going to be deleted immediately
del C:\Users\Thumbs.db /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt
del C:\Users\desktop.ini /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt
del C:\Users\*.DS_Store /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt
del C:\Users\*._DS_Store /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt
del C:\Users\*.desktop /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt
::Writes the directorys in c:\temp\tmp.txt.
dir /AD /b /s %startdir% > c:\temp\tmp.txt
::at goto start it will be start again
:start
::the Variable %loop% is increased by 1
set /a loop =%loop%+1
::at 5 --> goto exit
if %loop%==5 goto exit
::Under 5 --> goto start
else goto start
::deletes every empty folder which is written in C:\temp\tmp.txt
for /F "delims=" %%i in (c:\temp\tmp.txt) do rd "%%i"
::--> goto start and begins again
goto start
::%loop% has reached 5 --> exit
:exit
::Console window will be closed
exit
pause
exit
Pradeep is absolutely right about the best way to go is for files.
For deleting folders, try this:
FORFILES -p "" /D -15 /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
/D is for number of days, you can play with command parameters to meet exact requirement.
You can also use environment variables too so you can easily only delete files on the user that is currently logged on. For example, you can use %HOMEPATH%\Desktop to get to the desktop of the current user. More environment variables here.

batch create symlinks of all files/folders in a directory

I have a directory with many files and folders and I'd like to make symlinks of all files and folders in that directory to another folder but exclude one folder
Any suggestions?
#echo off
set source=c:\source\directory
set target=c:\target\directory
set exclude=DoNotLinkThisDirectory
forfiles /P "%source%" /C "cmd /c if #isdir==TRUE (if not #file==\"%exclude%\" mklink /d \"%target%\#file\" #path ) else ( mklink \"%target%\#file\" #path )"
EDIT - Updated to allow "easily" add of multiple excludes, using /G:file if findstr command filter the file/folder list
#echo off
set "source=c:\source\directory"
set "target=c:\target\directory"
set "exclude=%temp%\exclude.txt"
(
rem exclude files/dires with these strings into full path
echo .txt
echo pipe.cmd
rem escaped backslash and initial and final quotes to avoid partial matches
echo "c:\\source\\directory\\something.txt"
rem exclude thisNot file/directory from source directory
echo "%source:\=\\%\\thisNot"
)> "%exclude%"
forfiles /P "%source%" /C "cmd /c (echo #path|findstr /i /v /g:"%exclude%" >nul) && if #isdir==TRUE (mklink /d \"%target%\\\"#file #path) else (mklink \"%target%\\\"#file #path)"
del "%exclude%" > nul

delete batch script not working

#ECHO OFF
Set LOG="C:\Temp\Copy_Delete.log"
::##############################################
::Begin Deleting
::##############################################
Set Sourcedir="C:\test"
Echo %date% %time%: "Deleting from %sourcedir% >> %LOG%
FORFILES /P "%Sourcedir%" /D +0 /C "cmd /c del #path %Sourcedir%" >> %LOG%
When i executed this ,delete didn't work and in the log file it shows
C:\test*, Are you sure (Y/N)? and at the command prompt it gives this message "Fri 11/08/2013 16:11:43.28: "Deleting from "C:\test" >> "C:\Temp\Copy_Delete.log"
what could be the issue here.
Try passing quiet flag to del command.
del /Q #path %Sourcedir%" >> %LOG%
This worked under Windows 7 Pro:
#ECHO OFF
Set LOG="C:\Temp\Copy_Delete.log"
::##############################################
::Begin Deleting
::##############################################
Set Sourcedir="C:\test"
Echo %date% %time%: Deleting from %sourcedir% >> %LOG%
FORFILES /P "%Sourcedir%" /D +0 /C "cmd /c IF #ISDIR==FALSE DEL #path" >> %LOG%
It deletes all files modified today in C:\test .

Resources