如何创建日志文件?

时间:2021-09-01 06:36:56

How to save a log .txt file of all deleted files with this code?

如何使用此代码保存所有已删除文件的日志.txt文件?

Also I need to change code to search for all backup folders in root directory subfolders.

此外,我需要更改代码以搜索根目录子文件夹中的所有备份文件夹。

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "BackupFolder=D:\backup"
set "LastDate="
for /F "delims=." %%I in ('dir "%BackupFolder%\????????.*" /AD /B /ON 2^>nul') do (
    if not "!LastDate!" == "%%I" (
        for /F "skip=2 delims=" %%D in ('dir "%BackupFolder%\%%I.*" /AD /B /O-D-N /TC') do rd /Q /S "%BackupFolder%\%%D"
        set "LastDate=%%I"
    )
)
endlocal

This code was written by Mofi and posted as answer on How to remove oldest folder(s) of a group of folders in a folder with several folder groups? He posted also an enhanced version logging the files before deletion, but it was not working for me properly.

此代码由Mofi编写并作为答案发布,如何删除具有多个文件夹组的文件夹中的一组文件夹中最旧的文件夹?他还发布了一个在删除之前记录文件的增强版本,但它对我来说无法正常工作。

I tried with the followed command >> but log.txt file has not been generated.

我尝试使用以下命令>>但尚未生成log.txt文件。

1 个解决方案

#1


1  

The attempt to use >> was not shown in the question. How about something like:

尝试使用>>没有显示在问题中。怎么样的:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "BackupFolder=D:\backup"
set "LastDate="
SET "LOGFILE=%TEMP%\%~n0.log"
for /F "delims=." %%I in ('dir "%BackupFolder%\????????.*" /AD /B /ON 2^>nul') do (
    if not "!LastDate!" == "%%I" (
        for /F "skip=2 delims=" %%D in ('dir "%BackupFolder%\%%I.*" /AD /B /O-D-N /TC') do (
            DIR /S /B "%BackupFolder%\%%D" >>"%LOGFILE%"
            rd /Q /S "%BackupFolder%\%%D"
            set "LastDate=%%I"
        )
    )
)
endlocal

#1


1  

The attempt to use >> was not shown in the question. How about something like:

尝试使用>>没有显示在问题中。怎么样的:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "BackupFolder=D:\backup"
set "LastDate="
SET "LOGFILE=%TEMP%\%~n0.log"
for /F "delims=." %%I in ('dir "%BackupFolder%\????????.*" /AD /B /ON 2^>nul') do (
    if not "!LastDate!" == "%%I" (
        for /F "skip=2 delims=" %%D in ('dir "%BackupFolder%\%%I.*" /AD /B /O-D-N /TC') do (
            DIR /S /B "%BackupFolder%\%%D" >>"%LOGFILE%"
            rd /Q /S "%BackupFolder%\%%D"
            set "LastDate=%%I"
        )
    )
)
endlocal