Windows 批处理文件检查时间和日期然后重命名

Windows batch file check time and date then rename

我需要将今天的日期 paymentpagecalls20200128.txtWithdrawalConfirm20200128.txt 重命名为 paymentpagecalls20200129_*time*.txtWithdrawalConfirm20200129_*time*.txt 每 8 小时或如果可能 12:00am/6 :00am/10:00PM.

我有这个代码来获取我的时间(只是将它复制到我的一些搜索中)

set "destination=C:\Users\JBP-Admin\Desktop\Pugad\Forback_UP\Destination"
set day=0
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "result=%yyyy%%mm%%dd%"
echo %result%
for /r "C:\Users\JBP-Admin\Desktop\Pugad\For Rename" %%G in (*%result%.txt) do (
ren "%%~fG" "C:\Users\JBP-Admin\Desktop\Pugad\For Rename"
if exist "C:\Users\JBP-Admin\Desktop\Pugad\For Rename%%~nxG" (
    echo File "%%~fG" renamed successfully.
    ) else (
    echo File "%%~fG" renamed failed.
   )
)
pause  

如何插入部件以检查时间然后重命名?

分解正在发生的事情

  • 从所述目录中检索符合给定条件的所有 .txt 文件
  • 我们解析文件名并提取现有日期并附加新日期
  • 重命名文件

The output of this which is an ".exe" can be scheduled in task scheduler to executed at given interval based on given condition.

Go to TaskScheduler > Action > Create Basic Task > Set Trigger, Action > Finish

    using System;
    using System.IO;
    using System.Linq;


    namespace Test
    {

      public class Program
      {

        static void Main(string[] args)
        {
            string FolderPath = @"C:\Users\John\Documents";
            DirectoryInfo di = new DirectoryInfo(FolderPath);

            var files = di.EnumerateFiles("*.txt")
           .Where(s => s.Name.Contains("Withdrawal")
            || s.Name.Contains("payment")).ToList();

            var Currentfile1 = files[0].FullName;
            var Currentfile2 = files[1].FullName;

            //Parsing files
            var Newfile1 = Currentfile1.Substring(0, Currentfile1.Length - 2);
            var Newfile2 = Currentfile2.Substring(0, Currentfile2.Length - 2);

            // Append new date
             Newfile1 = Newfile1 + DateTime.Now.ToString("yyyyMMdd") + ".txt";
             Newfile2 = Newfile2 + DateTime.Now.ToString("yyyyMMdd") + ".txt";

            //Rename
            File.Move(Currentfile1, Newfile1);
            File.Move(Currentfile2, Newfile2);


        }  
    }
}