Robocopy并获取复制的文件名

时间:2015-02-10 04:02:44

标签: powershell batch-file robocopy

我正在使用robocopy只同步更新的文件,但我还需要复制文件名。

我还要进一步使用复制的文件名在其他文件中创建一个条目。

我无法找到只显示复制文件名的开关,批处理文件的任何powershell脚本也没问题。

2 个答案:

答案 0 :(得分:0)

user182904,

我修改了我的答案以满足您的要求。

尝试以下cmd文件。

1)它将从robocopy创建一个日志文件,只复制文件夹\文件。

2)然后它会将该日志文件剥离为仅复制的文件。

@echo off

rem source folders for robocopy
set source="c:\test\files"
set destin="c:\test\files2"

rem logfile names
set sourcelogfile=log-from-robocopy.txt
set destinlogfile=log-just-files.txt

rem robocopy command to output files copied
robocopy %source%  %destin% /e  /LOG:"%sourcelogfile%" /NP /NS /NC /NDL /TEE /NJH /NJS

rem for loop through robocopy output and stip it down to just file names into new logfile
for /f "tokens=*" %%i in (%sourcelogfile%) do (
    for /f "delims=" %%f in ("%%i") do (
        echo %%~nxf >> %destinlogfile%
    )
)

rem end of code

您可以将选项添加到Robocopy行,但是您需要在该行末尾添加选项,以输出脚本其余部分将解析的日志文件。

希望有所帮助。

所有robocopy开关的详细信息可以在这里找到:

来源:http://ss64.com/nt/robocopy.html

答案 1 :(得分:0)

此函数解析所有RoboCopy文本输出并将其输出为一个很好的对象。

像这样使用:

$RoboCopyOutput = robocopy $source $destination /ROBOSWITCHES

Parse-RoboCopyOutput $RoboCopyOutput

或者这个:

Parse-RoboCopyOutput (robocopy e t /s)

(显示错误处理)

我无法将所有忍者正则表达式技能称为我自己的技能:几年前我在网上找到了一些东西并随着时间的推移而扩展了它。抱歉,我现在找不到原始文章,或者我发布了一个链接。

我的RoboCopy版本检查仅查找我在我的环境中遇到的版本并且并非详尽无遗,因此请注意这一点。找到你的版本的日期格式并将其添加到switch语句应该很容易。

    Function Parse-RoboCopyOutput ($RoboCopyOutput)
{
    #Get RoboCopy version info to determine the date format
    switch ((Get-Command robocopy).FileVersionInfo.ProductVersion)
    {
        {$_ -eq 'XP027'}            {$RobocopyDateFormat = 'ddd MMM dd HH:mm:ss yyyy' ; break}  #Vista / Windows 7 / Server 2008
        {$_ -eq '6.2.9200.16384'}   {$RobocopyDateFormat = 'dd MMMM yyyy HH:mm:ss' ; break}     #Windows 8
        {$_ -eq '6.3.9600.16384'}   {$RobocopyDateFormat = 'dd MMMM yyyy HH:mm:ss' ; break}     #Windows 8.1
        default                     {throw "Unknown robocopy version."}
    }

    #Find the line numbers of the '----' spacers in the robocopy output
    $null,$HeaderStart,$HeaderEnd,$FooterStart = $RoboCopyOutput | Select-String  "----" | Foreach-Object {$_.linenumber}

    #Create the object to store all the parsed robocopy output information
    $RoboObject = New-Object PSObject

    #Grab the first section of the robocopy output by line number
    $RoboCopyOutput[$HeaderStart..$HeaderEnd] | Foreach-Object `
    {
        Switch -regex ($_)
        {
            'Started :(.*)'     {
                                    Add-Member -InputObject $RoboObject -Name StartTime -Value `
                                        ([datetime]::ParseExact($matches[1].trim(),$RobocopyDateFormat,$null)) -MemberType NoteProperty
                                }
            'Source :(.*)'      {
                                    Add-Member -InputObject $RoboObject -Name Source -Value ($matches[1].trim()) -MemberType NoteProperty
                                }
            'Dest (.)(.*)'      {
                                    Add-Member -InputObject $RoboObject -Name Destination -Value ($matches[2].trim()) -MemberType NoteProperty
                                }
            '^    Files :(.*)'  {
                                    Add-Member -InputObject $RoboObject -Name FilesToCopy -Value ($matches[1].trim()) -MemberType NoteProperty
                                }
            'Exc Files :(.*)'   {
                                    Add-Member -InputObject $RoboObject -Name ExcFiles -Value ($matches[1].trim()) -MemberType NoteProperty
                                }
            'Exc Dirs :(.*)'    {
                                    Add-Member -InputObject $RoboObject -Name ExcDirs -Value ($matches[1].trim()) -MemberType NoteProperty
                                }
            'Options :(.*)'     {
                                    Add-Member -InputObject $RoboObject -Name Options -Value ($matches[1].trim()) -MemberType NoteProperty
                                }
        }
    }

    #Grab the second section of the robocopy output by line number (if the copy has completed).
    if ($FooterStart)
    {
        $RoboCopyOutput[$FooterStart..($RoboCopyOutput.Count -1)] | Foreach-Object `
        {
            Switch -regex ($_)
            {
                'Ended :(.*)'       {
                                        Add-Member -InputObject $RoboObject -Name StopTime `
                                            -Value ([datetime]::ParseExact($matches[1].trim(),$RobocopyDateFormat,$null)) -MemberType NoteProperty
                                    }
                'Speed :(.*) Bytes' {
                                        Add-Member -InputObject $RoboObject -Name BytesPerSecond -Value ($matches[1].trim()) -MemberType NoteProperty
                                    }
                '(Total.*)'         {
                                        $Columns = $_.Split() | Where-Object {$_}
                                    }
                'Dirs :(.*)'        {
                                        $Fields = $matches[1].Split() | Where-Object {$_}

                                        $Dirs = New-Object PSObject

                                        0..5 | Foreach-Object `
                                        {
                                            Add-Member -InputObject $Dirs -Name $Columns[$_] -Value $Fields[$_] -MemberType NoteProperty

                                            Add-Member -InputObject $Dirs -Name 'toString' -MemberType ScriptMethod `
                                                -Value {[string]::Join(" ",($this.psobject.Properties | Foreach-Object {"$($_.name):$($_.value)"}))} -force
                                        }

                                        Add-Member -InputObject $RoboObject -Name Directories -Value $Dirs -MemberType NoteProperty
                                    }
                'Files :(.*)'       {
                                        $Fields = $matches[1].Split() | Where-Object {$_}

                                        $Files = New-Object PSObject

                                        0..5 | Foreach-Object `
                                        {
                                            Add-Member -InputObject $Files -Name $Columns[$_] -Value $Fields[$_] -MemberType NoteProperty

                                            Add-Member -InputObject $Files -Name 'toString' -MemberType ScriptMethod `
                                                -Value {[string]::Join(" ",($this.psobject.Properties | Foreach-Object {"$($_.name):$($_.value)"}))} -force
                                        }

                                        Add-Member -InputObject $RoboObject -Name Files -Value $files -MemberType NoteProperty
                                    }
                'Bytes :(.*)'       {
                                        $Fields = $matches[1].Split() | Where-Object {$_}

                                        $Fields = $Fields | Foreach-Object {$New=@();$i=0}{if ($_ -match '\d') {$New += $_;$i++} else {$New[$i-1] = ([double]$New[$i-1]) * "1${_}B"}}{$New}

                                        $Bytes = New-Object PSObject

                                        0..5 | Foreach-Object `
                                        {
                                            Add-Member -InputObject $Bytes -Name $Columns[$_] -Value $Fields[$_] -MemberType NoteProperty

                                            Add-Member -InputObject $Bytes -Name 'toString' -MemberType ScriptMethod `
                                                -Value {
                                                    [string]::Join(" ",($this.psobject.Properties | Foreach-Object {"$($_.name):$($_.value)"}))
                                                    } -force
                                        }

                                        Add-Member -InputObject $RoboObject -Name Bytes -Value $bytes -MemberType NoteProperty
                                    }
                }
            }

        #If the copy completed, calculate elapsed time
        if ($RoboObject.StopTime)
        {
            $ElapsedTime = ($RoboObject.StopTime) - ($RoboObject.StartTime)

            Add-Member -InputObject $RoboObject -Name ElapsedTime -Value $ElapsedTime -MemberType NoteProperty
        }
    }
    else
    {
        #Grab the RoboCopy error
        $RoboCopyOutput[($HeaderEnd +1)..($RoboCopyOutput.Count -1)] | ForEach-Object `
        {           
            $ErrorMessage += $_.ToString()          
        }

        Add-Member -InputObject $RoboObject -Name ErrorMessage -Value $ErrorMessage -MemberType NoteProperty
    }

    #Output
    $RoboObject
}