Skip to content

Instantly share code, notes, and snippets.

@bageljp
Created June 3, 2013 10:36
Show Gist options
  • Select an option

  • Save bageljp/5697360 to your computer and use it in GitHub Desktop.

Select an option

Save bageljp/5697360 to your computer and use it in GitHub Desktop.
cycle delete file and folder.
@echo off
:: ============================================================================
::
:: 一定期間が経過したファイル/フォルダを削除する
::
:: 第1引数に対象ディレクトリのパスを指定(require)
:: 第2引数に何日前のファイル/フォルダを対象とするか、日数を指定(require)
::
:: example. E:\tempを対象に、3日前のファイル/フォルダを削除する場合
:: call cycledel.bat "E:\temp" 3 > E:\log\cycledel.log 2>&1
::
:: ============================================================================
:: ----------------------------------------------------------------------------
:: DEFINE
:: ----------------------------------------------------------------------------
set DIR_DEL=%1
set DAYS=%2
set date_tmp=%date:/=%
set time_tmp=%time: =0%
set yyyy=%date_tmp:~0,4%
set yy=%date_tmp:~2,2%
set mm=%date_tmp:~4,2%
set dd=%date_tmp:~6,2%
set hh=%time_tmp:~0,2%
set mi=%time_tmp:~3,2%
set ss=%time_tmp:~6,2%
set sss=%time_tmp:~9,2%
set DATETIME=%yyyy%%mm%%dd%%hh%%mi%%ss%
:: ----------------------------------------------------------------------------
:: LOG
:: ----------------------------------------------------------------------------
:: NORMAL PRINT
:: call test.bat > log.txt
:: NORMAL PRINT APPEND
:: call test.bat >> log.txt
:: ERROR PRINT
:: call test.bat 2 > log.txt
:: ERROR PRINT APPEND
:: call test.bat 2 >> log.txt
:: NORMAL AND ERROR PRINT
:: call test.bat > log.txt 2>&1
:: NORMAL AND ERROR PRINT OTHER FILE
:: call test.bat > log.txt 2 > error.txt
:: ----------------------------------------------------------------------------
:: MAIN
:: ----------------------------------------------------------------------------
echo ### START ###
echo %date% %time%
echo.
echo forfiles /P %DIR_DEL% /S /D -%DAYS% /C "cmd /c del /F /S /Q @path"
forfiles /P %DIR_DEL% /S /D -%DAYS% /C "cmd /c del /F /S /Q @path"
echo.
echo %date% %time%
echo ### END ###
exit /B %ERRORLEVEL%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment