【问题标题】:Writing a batch utility to archive files on Windows Server 2008 R2编写批处理实用程序以在 Windows Server 2008 R2 上存档文件
【发布时间】:2014-07-21 13:28:06
【问题描述】:

我需要编写一个命令行(PowerShell 或 DOS)实用程序,它将 归档在文件夹中存在的指定日期之间创建的所有文件 X 并将它们作为.zip 包存储在同一文件夹中。

此实用程序将被安排到 Windows 调度程序中,其中将向其提供 tofrom 日期、store location 等参数,并且它将在指定的持续时间运行,例如每天中午 12:00。

是否可以将其写成批处理.bat 文件? Windows 中是否有任何内置功能可以压缩文件,或者我需要使用第三方程序,如 7-Zip 等。

我不是在寻找任何勺子喂食,而只是一个方向,任何人都可以指导我进行教程或其他内容。基于DOS 和基于PowerShell 的解决方案都适合我。

请帮帮我。

谢谢。

【问题讨论】:

  • Maven,你想要指定日期之间的文件,还是 2014-05-21 到 2014-05-31 之类的文件,包括指定的日期?
  • 您使用的是哪个第三方 zip 程序?

标签: batch-file powershell windows-server-2008 windows-server-2008-r2 archive


【解决方案1】:

我会选择 PowerShell。

使用 PowerShell,您可以使用 Get-Date 命令行开关和比较运算符轻松构建和比较日期。您可以尝试以下方法:

$folderPath = 'C:\Temp'

$date1 = Get-Date '2014-05-01'
$date2 = Get-Date '2014-05-31'

foreach( $item in (Get-ChildItem $folderPath) )
{
    if( $item.LastWriteTime -ge $date1 -and $item.LastWriteTime -le $date2 )
    {
        # Compression step
    }
}

至于压缩,您有几个选项,如question 中所述。

脚本参数可以查看this blog article

【讨论】:

  • 如果您希望它可以跨区域移植,您可以使用此代码 $tzOffset = Get-Date -UFormat '%Z' 获取本地时区偏移量,然后以这种方式初始化 $date1 和 $date2 $date1 = Get-Date "2014-05-01 10:00:00 $tzOffset"
  • 假设您的时区偏移量是 +02,只需这样做$date2 = Get-Date '2014-05-31 +02',它将使用一天开始的00:00:00 时间。
  • 问题是它不在日期之间。从第一个日期的 00:00:00 到最后一天开始的 00:00:00。最后一天不包括在内。它应该在要求的日期之间,或者使它们 both 包括在内。
  • 然后使用-lt 运算符代替-le
  • 这并不意味着它包含或不包含两个日期,这是处理此任务的合理方式。
【解决方案2】:

这是一个基于 Cristophe C 的 powershell 脚本构建的批处理文件

它在命令行上使用foldertwo dates 并将最后修改的文件名从date1 到date2 写入一个名为daterange.txt 的文本文件中

如果在问题中添加了更多详细信息,可以添加一些代码来压缩文件。

@echo off
if "%~3"=="" (
echo "%~0" "c:\folder" yyyy-mm-dd1 yyyy-mm-dd2
echo(
echo( This returns last-modified files in the folder from the two dates inclusive
echo( and puts them in a file called "daterange.txt"
echo(
echo( Requires Powershell V3+ (Powershell scripting also needs to be enabled^)
echo(
pause
goto :EOF
) 

set "file=%temp%\psdaterange.ps1"

(
echo( $folderPath = '%~1\'
echo( 
echo( $date1 = Get-Date '%~2'
echo( $date2 = Get-Date '%~3'
echo( 
echo( foreach( $item in (Get-ChildItem -file $folderPath^) ^)
echo( {
echo( if( $item.LastWriteTime -ge $date1 -and $item.LastWriteTime -lt $date2.AddDays(1^) ^)
echo(     {
echo(         Write-Host $folderPath$item
echo(     }
echo( }
) >"%file%"

powershell "%file%" > "daterange.txt"
del "%file%"

【讨论】:

    【解决方案3】:

    Powershell 5.0 中提供了 Compress-Archive 实用程序。

    # Create a zip file with the contents of C:\Stuff\
    Compress-Archive -Path C:\Stuff -DestinationPath archive.zip
    
    # Add more files to the zip file
    # (Existing files in the zip file with the same name are replaced)
    Compress-Archive -Path C:\OtherStuff\*.txt -Update -DestinationPath archive.zip
    
    # Extract the zip file to C:\Destination\
    Expand-Archive -Path archive.zip -DestinationPath C:\Destination
    

    有关详细信息,请参阅此答案: How to create a zip archive with PowerShell?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-11
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多