Powershell - 将变量中存储的日期与System.DateTime进行比较 [英] Powershell - Comparing a Date Stored in a variable with a System.DateTime

查看:107
本文介绍了Powershell - 将变量中存储的日期与System.DateTime进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我是PowerShell 1.0版的新手。 我正在尝试根据特定文件类型的最新日期将文件从一个目录复制到另一个目录。  以下是我的编程步骤:


1. 通过LastWriteTime对目录进行排序。


2.   将对象限制为文件扩展名等于".txt"的对象。


3 获取列表顶部的第一个文件(这将是具有最新LastWriteTime的文件)。


4. 提取LastWriteTime值并将其放入Variable $ T。


5. 将目录中所有文件的CreationDate与$ T中的值进行比较。


6. 如果Creation大于或等于$ T中的值,则将文件复制到另一个目录。


这是我的代码:


$ T =(dir | sort -prop LastWriteTime | Where-Object {$ _。Extension -eq" .txt"} | select


-last 1 | Select-Object LastWriteTime)

foreach(Get-ChildItem C中的$ i:\ db_backups \Week1 \ CLEARAFR)

{

    if($ i.CreationTime -ge $ T)

    {

        Copy-Item $ i.FullName C:\ Test <
    }
}


 


当我尝试运行此代码时,对于它尝试比较的每个文件,我收到以下消息:


在C:\ PowerShell \ testing.ps1:4 char:29

+     ; if($ i.LastWriteTime -ge <<<<< $ T)

'-ge'运算符失败:无法比较" 2/4/2011 3:04:下午44点" to"@ {LastWriteTime = 3/15/2010 12:51:15 PM}"。错误:
$
"无法转换"@ {LastWriteTime = 3/15/2010 12:51:15 PM}" to"System.DateTime"。"。


有人可以帮我指点正确的方向吗? 谢谢!!


 


 

解决方案

是否必须是PowerShell?  :)&NBSP;这是一个适用于您的VB函数 - 我将[尝试]明天获取PowerShell等效代码。


 


 
function CopyFilesIfNewer (strsourcefolder,strdestfolder)
if fso.FolderExists(strsourcefolder))< span style ="color:Blue;">然后
CopyFilesIfNewer = -1
退出 功能
end 如果

如果 fso.FolderExists(strdestfolder))然后
wscript.echo "创建目标文件夹"& strdestfolder
fso.CreateFolder(strdestfolder)
End 如果

dim osourcefolder,odestfolder

set osourcefolder = fso.GetFolder(strsourcefolder)
set odestfolder = fso.GetFolder(strdestfolder)

dim osourcefile,osubfolder

for 每个 osourcefile osourcefolder.files
CopyFileIfNewer osourcefile.path,strdestfolder& " \" & osourcefile.name
Next

对于 每个 osubfolder osourcefolder.SubFolders
'WScript .Echo"CopyFilesIfNewer" &安培; osubfolder.path& "," &安培;最大的文件夹" \" &安培; osubfolder.Name
CopyFilesIfNewer osubfolder.path,strdestfolder& " \" & osubfolder.Name
下一步
end 功能

功能 CopyFileIfNewer(strsourcefile,strdestfile)
如果 fso.FileExists(strsourcefile)然后
CopyFileIfNewer = 0
退出 功能
结束 如果

if fso.FileExists(strdestfile)然后
on 错误 resume 下一页
wscript.echo "复制"""" & strsourcefile& """到"""& strdestfile& """ ;;目标文件不存在"
fso.CopyFile strsourcefile,strdestfile, True
if err.number<> 0 然后
wscript.echo " !!!!错误复制"& strsourcefile& " (" & Err.Number& "" & err.Description& ")"
end if
CopyFileIfNewer = 1
on 错误 goto 0
退出 function
end if

If (fso.GetFile(strdestfile).DateLastModified< fso.GetFile(strsourcefile).DateLastModified)然后
on 错误 resume 下一步
wscript.echo "覆盖"""" & strdestfile& """更新的"""& strsourcefile& """"
if (fso.GetFile(strdestfile) ).Attributes 1)然后
fso.GetFile(strdestfile).Attributes = fso.GetFile(strdestfile).Attributes -1
end if
fso.CopyFile strsourcefile,strdestfile, true
if err.number<> 0 然后
wscript.echo " !!!!错误复制"& strsourcefile& "(" & Err.Number& "" & err.Description& ")"
end 如果
CopyFileIfNewer = 1
on 错误 goto 0
退出 功能
end 如果
CopyFileIfNewer = 0
结束 功能


Hi!

I am VERY new to PowerShell, Version 1.0.  I am trying to copy files from one directory to another based on the most recent date of a specific file type.  Here are my programming steps:

1.  Sort a directory by LastWriteTime.

2.   Limit objects to those where the file extension is equal to ".txt".

3.  Get the first file at the top of the list (This will be the file with the latest LastWriteTime).

4.  Extract the LastWriteTime value and put it in Variable $T.

5.  Compare the CreationDate of ALL files in the directory to the value in $T.

6.  If the Creation is greater or equal to the value in $T, copy the file to another directory.

Here is my code:

$T = (dir | sort -prop LastWriteTime | Where-Object {$_.Extension -eq ".txt"} | select

-last 1 | Select-Object LastWriteTime)
foreach ($i in Get-ChildItem C:\db_backups\Week1\CLEARAFR)
{
    if ($i.CreationTime -ge $T)
    {
        Copy-Item $i.FullName C:\Test
    }
}

 

When I attempt to run this code, for each file that it tries to compare, I get the following message:

At C:\PowerShell\testing.ps1:4 char:29
+     if ($i.LastWriteTime -ge  <<<< $T)
The '-ge' operator failed: Could not compare "2/4/2011 3:04:44 PM" to "@{LastWriteTime=3/15/2010 12:51:15 PM}". Error:
"Cannot convert "@{LastWriteTime=3/15/2010 12:51:15 PM}" to "System.DateTime".".

Can someone please help point me in the right direction?  Thanks!!

 

 

解决方案

Does it have to be PowerShell?  :)  Here's a VB function that should work for you - I'll [try to] get the PowerShell equivalent code tomorrow.

 

function CopyFilesIfNewer(strsourcefolder, strdestfolder)
	if (not fso.FolderExists(strsourcefolder)) then
		CopyFilesIfNewer = -1
		exit function	
	end If
	
	If (not fso.FolderExists(strdestfolder)) Then
		wscript.echo "Creating Destination Folder " & strdestfolder
		fso.CreateFolder(strdestfolder)
	End If

	dim osourcefolder, odestfolder

	set osourcefolder = fso.GetFolder(strsourcefolder)
	set odestfolder = fso.GetFolder(strdestfolder)
	
	dim osourcefile, osubfolder

	for each osourcefile in osourcefolder.files
		CopyFileIfNewer osourcefile.path, strdestfolder & "\" & osourcefile.name
	Next
	
	For Each osubfolder In osourcefolder.SubFolders
		'WScript.Echo "CopyFilesIfNewer " & osubfolder.path & ", " & strdestfolder & "\" & osubfolder.Name
		CopyFilesIfNewer osubfolder.path, strdestfolder & "\" & osubfolder.Name
	Next
end function

Function CopyFileIfNewer(strsourcefile, strdestfile)
	if not fso.FileExists(strsourcefile) then
		CopyFileIfNewer = 0
		exit function
	end if
	
	if not fso.FileExists(strdestfile) then
		on error resume next
		wscript.echo "Copying """ & strsourcefile & """ to """ & strdestfile & """; destination file did not exist"
		fso.CopyFile strsourcefile, strdestfile, True
		if err.number <> 0 then
			wscript.echo "!!!! Error Copying " & strsourcefile & " (" & Err.Number & " " & err.Description & ")"
		end if
		CopyFileIfNewer = 1
		on error goto 0
		exit function
	end if
	
	If (fso.GetFile(strdestfile).DateLastModified < fso.GetFile(strsourcefile).DateLastModified) then
		on error resume Next
		wscript.echo "Overwriting """ & strdestfile & """ with newer """ & strsourcefile & """"
		if (fso.GetFile(strdestfile).Attributes and 1) then
			fso.GetFile(strdestfile).Attributes = fso.GetFile(strdestfile).Attributes -1
		end if
		fso.CopyFile strsourcefile, strdestfile, true
		if err.number <> 0 then
			wscript.echo "!!!! Error Copying " & strsourcefile & "(" & Err.Number & " " & err.Description & ")"
		end If
		CopyFileIfNewer = 1
		on error goto 0
		exit function
	end If
	CopyFileIfNewer = 0
End Function


这篇关于Powershell - 将变量中存储的日期与System.DateTime进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆