批处理脚本适用于Windows 7,但不适用于Windows Server 2008

美好的一天

我有以下批处理脚本循环通过包含.sql文件的文件夹。 当它find一个具有今天date时间戳的.sql文件时,它将该文件复制到一个新的目录中。

@echo off setlocal enableextensions enabledelayedexpansion set "currentDate=%date:~0,10%" for %%g in ("c:\mfa\*.sql") do ( set "fileDate=%%~tg" set "fileDate=!fileDate:~0,10!" if "!fileDate!"=="%currentDate%" ( copy "%%~fg" "c:\newLocation" ) ) 

我的问题:

这在Windows 7上效果很好,但在Windows Server 2008上效果不是很好。当我在Win7上回显文件variables时,它会给我保存在!fileDate中的时间戳! 值。 但是当我回声!fileDate! 在Windows Server 2008中,它返回: ECHOclosures。

即使我删除了delayedexpansion,这仍然不起作用。

为什么在Server 2008上无法正常工作?

====================更新 –

PowerShell错误

 The term 'test.ps1' is not recognized as the name of a cmdlet, function, script file, or operable pro elling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:9 + test.ps1 <<<< + CategoryInfo : ObjectNotFound: (test.ps1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException 

我同意MDMarra关于使用Powershell的评论。

我们在2014年你的平台是Windows 2008 Server …所以我强烈build议你考虑批次有点“过时”…

使用Powershell,如下所示:

 $today=Get-Date -format d $files=Get-ChildItem "C:\mfa" -Filter *.sql foreach ($file in $files) { $filename=$file.FullName $dateCreated=$file.CreationTime | Get-Date -format d $dateModified=$file.LastWriteTime | Get-Date -format d write-output "Today is : $today" write-output "Scanning file : $filename" write-output "Modified : $dateModified" write-output "Created : $dateCreated" if($dateCreated -eq $today) { copy-item $filename c:\newLocation } write-output "---------------------------------------------" } 

假设在创builddate进行比较。 要与上次修改date进行比较,请使用LastWriteTime而不是CreationTime