Home > Blockchain >  How to create a batch file with output to a log file
How to create a batch file with output to a log file

Time:03-22

I want to create Batch file which runs from Task Scheduler

Requirements:

  1. less than 30 days old files should move from main folder to Archive folder.
  2. then the moved archive files should be deleted.
  3. it should store all the log entries.

I have scripted steps 1 & 2, (from web searches), but inserting log file command is quite difficult for me, as I have no knowledge on batch scripts.

Script for steps 1 & 2:

forfiles /p "D:\Test" /m *.* /s /d -30 /c "cmd /c move @file E:\Test1"
forfiles /p "E:\Test1" /c "cmd /c del @path"

Can someone please show me how I can create log files using the above 2 commands.

The log file should store all files name which it moved & deleted, date and time, and success or failed.

CodePudding user response:

It makes absolutely no sense to first move files from drive D: to drive E: which requires copying the data of each file, and then delete each file on drive E:. It is much faster to delete the files older than 29 days directly on drive D:.

The batch file below can be used for the file deletion task with logging success or error of each file deletion.

@echo off
if "%~1" == "" goto RunForFiles
del /A /F %1 2>nul
(if not exist %1 (echo            
  • Related