PowerShell移动项目$filename

PowerShell移动项目$filename,powershell,Powershell,我搜索,我谷歌。。就要把我的头砸在桌子上 这怎么会行不通呢 move-Item $path$file $targetdir 这给了我一个错误 移动项目:指定路径C:\Repository\test.csv上的对象 不存在 现在,如果我调试这个并使用 write-output move-Item $path$file $targetdir 然后获取输出并粘贴它(带有路径和目标的文件名),这样就可以了 相信我,档案就在那里=\ 代码如下 $path = 'C:\test\' $Ti

我搜索,我谷歌。。就要把我的头砸在桌子上

这怎么会行不通呢

move-Item  $path$file $targetdir
这给了我一个错误 移动项目:指定路径C:\Repository\test.csv上的对象 不存在

现在,如果我调试这个并使用

write-output move-Item  $path$file $targetdir
然后获取输出并粘贴它(带有路径和目标的文件名),这样就可以了

相信我,档案就在那里=\


代码如下

    $path = 'C:\test\'
$TimeStamp = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
$LogFile = Get-Date -Format "MM_dd_yyyy"
$targetdir = "C:\test\Uploaded\"

#Get-ChildItem -path $path\* -Include *.csv | foreach-object {$_.Fullname} | Format-Table name -hidetableheaders  | Out-File $path\list.txt

Get-ChildItem -path $path\* -Include *.csv | Format-Table name -hidetableheaders  | Out-File $path\list2.txt
get-content C:\test\list2.txt | where {$_ -ne ""} | out-file C:\test\list.txt
Remove-Item  C:\test\list2.txt

$list = get-content C:\test\list.txt 


foreach ($file in $list)
{
    $ftp = "ftp://REMOVED/$file"
    "ftp url: $ftp"
    $webclient = New-Object System.Net.WebClient
    $uri = New-Object System.Uri($ftp)
    "Uploading $file..."

    $succeeded = $true;
    &   {
    trap { $script:succeeded = $false; continue }
    $webclient.UploadFile($uri, $path+$file)
        }
if ($succeeded) 
        { 

        echo $file 'Was successfully uploaded!' $Timestamp >> logfile$LogFile.log
        move-Item  -path $path$file -destination $targetdir
        #test-path $path$file
        } 

    else 
        { 

        echo $file 'Was not successfully uploaded, will retry later' $Timestamp >> logfile$LogFile.log

        }   


}

exit

目标目录是否已经存在?我相信如果目标目录不存在,移动项目将失败。如果是这种情况,您只需事先测试目录的存在性,然后根据需要创建

If (!(Test-Path -Path $targetdir)) {
    New-Item -ItemType directory -Path $targetdir
}

目标目录是否已经存在?我相信如果目标目录不存在,移动项目将失败。如果是这种情况,您只需事先测试目录的存在性,然后根据需要创建

If (!(Test-Path -Path $targetdir)) {
    New-Item -ItemType directory -Path $targetdir
}

目标目录是否已经存在?我相信如果目标目录不存在,移动项目将失败。如果是这种情况,您只需事先测试目录的存在性,然后根据需要创建

If (!(Test-Path -Path $targetdir)) {
    New-Item -ItemType directory -Path $targetdir
}

目标目录是否已经存在?我相信如果目标目录不存在,移动项目将失败。如果是这种情况,您只需事先测试目录的存在性,然后根据需要创建

If (!(Test-Path -Path $targetdir)) {
    New-Item -ItemType directory -Path $targetdir
}
那么这个怎么样:

ForEach($File in $List){
    Join-Path $path $file | Move-Item -Dest $Targetdir
}
编辑:同时。。。你创建的list.txt让我很烦恼,所以我不得不发表评论。格式表应用于格式化文本,而不是用于选择要输出到文件的值。有一个更好的方法,考虑这个替代方案:

Get-ChildItem "$path*.csv" | Select -ExpandProperty Name | Out-File $pathlist.txt
既然您说
$path='C:\test\'
,那么您在其中添加了额外的反斜杠,这可能会导致某些命令出现问题

Edit2:好的,如果这不起作用,为什么不处理文件本身,而不是输出到一个文件,从该文件导入,然后处理东西呢

$path='c:\test\'
$TargetDir = 'c:\test\NewDir'
$FileList = Get-ChildItem $path*.csv
If(!(Test-Path $TargetDir)){New-Item -ItemType Directory -Path $TargetDir|Out-Null}
$FileList | Move-Item -Destination $TargetDir
然后,如果您真的想要这些文件名的列表,只需通过管道$FileList进行选择,然后将其输出到文件

$FileList | Select -ExpandProperty Name | Out-File 'C:\Test\list.txt'
来,看看这个,看看有没有你喜欢的东西。我做了一些更改,例如在开始时为所有内容声明路径,将WebClient对象创建移到循环之外,并更改了屏幕上的显示方式。另外,我跳过了整个导出到文本文件并重新导入的过程

$path = 'C:\test'
$ftpaddr = 'ftp://ftp.example.com/uploads'
$TimeStamp = Get-Date -Format "MM/dd/yyyy hh:mm:ss tt"
$LogFile = Get-Date -Format "MM_dd_yyyy"

$LogDir = "C:\Test\Logs"
If(!(test-path $LogDir)){New-Item -ItemType Directory -Path $LogDir | Out-Null}
$targetdir = 'C:\test\Uploaded'
If(!(test-path $targetdir)){New-Item -ItemType Directory -Path $targetdir | Out-Null}

$list = Get-ChildItem -path $path\* -Include *.csv

$webclient = New-Object System.Net.WebClient

"ftp url: $ftpaddr"

foreach ($file in ($list|select -ExpandProperty Name))
{
    $uri = New-Object System.Uri(("$ftpaddr/$file"))

    Write-Host "Uploading $file... " -NoNewline -ForegroundColor White

    $succeeded = $true
    &   {
    trap { $script:succeeded = $false; continue }
    $webclient.UploadFile($uri, "$Path\$file")
        }

if ($succeeded) 
        {
        Write-Host "Success!" -ForegroundColor Green
        "$Timestamp`t$File was successfully uploaded!" | Out-File "$logdir\logfile$LogFile.log" -Append
        move-Item  -path "$path\$file" -destination $targetdir
        } 

    else 
        { 
        Write-Host "Failed! Will retry later." -ForegroundColor Red
        "$Timestamp`t$File was not successfully uploaded, will retry later" | Out-File "$logdir\logfile$LogFile.log" -Append
        }   

}
那么这个怎么样:

ForEach($File in $List){
    Join-Path $path $file | Move-Item -Dest $Targetdir
}
编辑:同时。。。你创建的list.txt让我很烦恼,所以我不得不发表评论。格式表应用于格式化文本,而不是用于选择要输出到文件的值。有一个更好的方法,考虑这个替代方案:

Get-ChildItem "$path*.csv" | Select -ExpandProperty Name | Out-File $pathlist.txt
既然您说
$path='C:\test\'
,那么您在其中添加了额外的反斜杠,这可能会导致某些命令出现问题

Edit2:好的,如果这不起作用,为什么不处理文件本身,而不是输出到一个文件,从该文件导入,然后处理东西呢

$path='c:\test\'
$TargetDir = 'c:\test\NewDir'
$FileList = Get-ChildItem $path*.csv
If(!(Test-Path $TargetDir)){New-Item -ItemType Directory -Path $TargetDir|Out-Null}
$FileList | Move-Item -Destination $TargetDir
然后,如果您真的想要这些文件名的列表,只需通过管道$FileList进行选择,然后将其输出到文件

$FileList | Select -ExpandProperty Name | Out-File 'C:\Test\list.txt'
来,看看这个,看看有没有你喜欢的东西。我做了一些更改,例如在开始时为所有内容声明路径,将WebClient对象创建移到循环之外,并更改了屏幕上的显示方式。另外,我跳过了整个导出到文本文件并重新导入的过程

$path = 'C:\test'
$ftpaddr = 'ftp://ftp.example.com/uploads'
$TimeStamp = Get-Date -Format "MM/dd/yyyy hh:mm:ss tt"
$LogFile = Get-Date -Format "MM_dd_yyyy"

$LogDir = "C:\Test\Logs"
If(!(test-path $LogDir)){New-Item -ItemType Directory -Path $LogDir | Out-Null}
$targetdir = 'C:\test\Uploaded'
If(!(test-path $targetdir)){New-Item -ItemType Directory -Path $targetdir | Out-Null}

$list = Get-ChildItem -path $path\* -Include *.csv

$webclient = New-Object System.Net.WebClient

"ftp url: $ftpaddr"

foreach ($file in ($list|select -ExpandProperty Name))
{
    $uri = New-Object System.Uri(("$ftpaddr/$file"))

    Write-Host "Uploading $file... " -NoNewline -ForegroundColor White

    $succeeded = $true
    &   {
    trap { $script:succeeded = $false; continue }
    $webclient.UploadFile($uri, "$Path\$file")
        }

if ($succeeded) 
        {
        Write-Host "Success!" -ForegroundColor Green
        "$Timestamp`t$File was successfully uploaded!" | Out-File "$logdir\logfile$LogFile.log" -Append
        move-Item  -path "$path\$file" -destination $targetdir
        } 

    else 
        { 
        Write-Host "Failed! Will retry later." -ForegroundColor Red
        "$Timestamp`t$File was not successfully uploaded, will retry later" | Out-File "$logdir\logfile$LogFile.log" -Append
        }   

}
那么这个怎么样:

ForEach($File in $List){
    Join-Path $path $file | Move-Item -Dest $Targetdir
}
编辑:同时。。。你创建的list.txt让我很烦恼,所以我不得不发表评论。格式表应用于格式化文本,而不是用于选择要输出到文件的值。有一个更好的方法,考虑这个替代方案:

Get-ChildItem "$path*.csv" | Select -ExpandProperty Name | Out-File $pathlist.txt
既然您说
$path='C:\test\'
,那么您在其中添加了额外的反斜杠,这可能会导致某些命令出现问题

Edit2:好的,如果这不起作用,为什么不处理文件本身,而不是输出到一个文件,从该文件导入,然后处理东西呢

$path='c:\test\'
$TargetDir = 'c:\test\NewDir'
$FileList = Get-ChildItem $path*.csv
If(!(Test-Path $TargetDir)){New-Item -ItemType Directory -Path $TargetDir|Out-Null}
$FileList | Move-Item -Destination $TargetDir
然后,如果您真的想要这些文件名的列表,只需通过管道$FileList进行选择,然后将其输出到文件

$FileList | Select -ExpandProperty Name | Out-File 'C:\Test\list.txt'
来,看看这个,看看有没有你喜欢的东西。我做了一些更改,例如在开始时为所有内容声明路径,将WebClient对象创建移到循环之外,并更改了屏幕上的显示方式。另外,我跳过了整个导出到文本文件并重新导入的过程

$path = 'C:\test'
$ftpaddr = 'ftp://ftp.example.com/uploads'
$TimeStamp = Get-Date -Format "MM/dd/yyyy hh:mm:ss tt"
$LogFile = Get-Date -Format "MM_dd_yyyy"

$LogDir = "C:\Test\Logs"
If(!(test-path $LogDir)){New-Item -ItemType Directory -Path $LogDir | Out-Null}
$targetdir = 'C:\test\Uploaded'
If(!(test-path $targetdir)){New-Item -ItemType Directory -Path $targetdir | Out-Null}

$list = Get-ChildItem -path $path\* -Include *.csv

$webclient = New-Object System.Net.WebClient

"ftp url: $ftpaddr"

foreach ($file in ($list|select -ExpandProperty Name))
{
    $uri = New-Object System.Uri(("$ftpaddr/$file"))

    Write-Host "Uploading $file... " -NoNewline -ForegroundColor White

    $succeeded = $true
    &   {
    trap { $script:succeeded = $false; continue }
    $webclient.UploadFile($uri, "$Path\$file")
        }

if ($succeeded) 
        {
        Write-Host "Success!" -ForegroundColor Green
        "$Timestamp`t$File was successfully uploaded!" | Out-File "$logdir\logfile$LogFile.log" -Append
        move-Item  -path "$path\$file" -destination $targetdir
        } 

    else 
        { 
        Write-Host "Failed! Will retry later." -ForegroundColor Red
        "$Timestamp`t$File was not successfully uploaded, will retry later" | Out-File "$logdir\logfile$LogFile.log" -Append
        }   

}
那么这个怎么样:

ForEach($File in $List){
    Join-Path $path $file | Move-Item -Dest $Targetdir
}
编辑:同时。。。你创建的list.txt让我很烦恼,所以我不得不发表评论。格式表应用于格式化文本,而不是用于选择要输出到文件的值。有一个更好的方法,考虑这个替代方案:

Get-ChildItem "$path*.csv" | Select -ExpandProperty Name | Out-File $pathlist.txt
既然您说
$path='C:\test\'
,那么您在其中添加了额外的反斜杠,这可能会导致某些命令出现问题

Edit2:好的,如果这不起作用,为什么不处理文件本身,而不是输出到一个文件,从该文件导入,然后处理东西呢

$path='c:\test\'
$TargetDir = 'c:\test\NewDir'
$FileList = Get-ChildItem $path*.csv
If(!(Test-Path $TargetDir)){New-Item -ItemType Directory -Path $TargetDir|Out-Null}
$FileList | Move-Item -Destination $TargetDir
然后,如果您真的想要这些文件名的列表,只需通过管道$FileList进行选择,然后将其输出到文件

$FileList | Select -ExpandProperty Name | Out-File 'C:\Test\list.txt'
来,看看这个,看看有没有你喜欢的东西。我做了一些更改,例如在开始时为所有内容声明路径,将WebClient对象创建移到循环之外,并更改了屏幕上的显示方式。另外,我跳过了整个导出到文本文件并重新导入的过程

$path = 'C:\test'
$ftpaddr = 'ftp://ftp.example.com/uploads'
$TimeStamp = Get-Date -Format "MM/dd/yyyy hh:mm:ss tt"
$LogFile = Get-Date -Format "MM_dd_yyyy"

$LogDir = "C:\Test\Logs"
If(!(test-path $LogDir)){New-Item -ItemType Directory -Path $LogDir | Out-Null}
$targetdir = 'C:\test\Uploaded'
If(!(test-path $targetdir)){New-Item -ItemType Directory -Path $targetdir | Out-Null}

$list = Get-ChildItem -path $path\* -Include *.csv

$webclient = New-Object System.Net.WebClient

"ftp url: $ftpaddr"

foreach ($file in ($list|select -ExpandProperty Name))
{
    $uri = New-Object System.Uri(("$ftpaddr/$file"))

    Write-Host "Uploading $file... " -NoNewline -ForegroundColor White

    $succeeded = $true
    &   {
    trap { $script:succeeded = $false; continue }
    $webclient.UploadFile($uri, "$Path\$file")
        }

if ($succeeded) 
        {
        Write-Host "Success!" -ForegroundColor Green
        "$Timestamp`t$File was successfully uploaded!" | Out-File "$logdir\logfile$LogFile.log" -Append
        move-Item  -path "$path\$file" -destination $targetdir
        } 

    else 
        { 
        Write-Host "Failed! Will retry later." -ForegroundColor Red
        "$Timestamp`t$File was not successfully uploaded, will retry later" | Out-File "$logdir\logfile$LogFile.log" -Append
        }   

}

这对我有用。谢谢@TheMadTechnician。希望这对大家都有帮助

$TimeStamp = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
$LogFile = Get-Date -Format "MM_dd_yyyy"

$path='C:\test\'
$targetDir = 'C:\test\Uploaded\'
$fileList = Get-ChildItem $path*.csv
If(!(Test-Path $TargetDir)){New-Item -ItemType Directory -Path $TargetDir|Out-Null}
$fileList | Select -ExpandProperty Name | Out-File 'C:\test\list.txt'

$list = get-content C:\test\list.txt 
foreach ($file in $list)
{
    $ftp = "ftp://REMOVED/$file"
    "ftp url: $ftp"
    $webclient = New-Object System.Net.WebClient
    $uri = New-Object System.Uri($ftp)
    "Uploading $file..."

    $succeeded = $true;
    &   {
    trap { $script:succeeded = $false; continue }
    $webclient.UploadFile($uri, $path+$file)
        }
if ($succeeded) 
        { 

        echo $file 'Was successfully uploaded!' $Timestamp >> logfile$LogFile.log
        move-Item  -path $path$file -destination $targetdir$Timestamp"_"$file
        #test-path $path$file
        } 

    else 
        { 

        echo $file 'Was not successfully uploaded, will retry later' $Timestamp >> logfile$LogFile.log

        }   


}

exit

这对我有用。谢谢@TheMadTechnician。希望这对大家都有帮助

$TimeStamp = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
$LogFile = Get-Date -Format "MM_dd_yyyy"

$path='C:\test\'
$targetDir = 'C:\test\Uploaded\'
$fileList = Get-ChildItem $path*.csv
If(!(Test-Path $TargetDir)){New-Item -ItemType Directory -Path $TargetDir|Out-Null}
$fileList | Select -ExpandProperty Name | Out-File 'C:\test\list.txt'

$list = get-content C:\test\list.txt 
foreach ($file in $list)
{
    $ftp = "ftp://REMOVED/$file"
    "ftp url: $ftp"
    $webclient = New-Object System.Net.WebClient
    $uri = New-Object System.Uri($ftp)
    "Uploading $file..."

    $succeeded = $true;
    &   {
    trap { $script:succeeded = $false; continue }
    $webclient.UploadFile($uri, $path+$file)
        }
if ($succeeded) 
        { 

        echo $file 'Was successfully uploaded!' $Timestamp >> logfile$LogFile.log
        move-Item  -path $path$file -destination $targetdir$Timestamp"_"$file
        #test-path $path$file
        } 

    else 
        { 

        echo $file 'Was not successfully uploaded, will retry later' $Timestamp >> logfile$LogFile.log

        }   


}

exit

这对我有用。谢谢@TheMadTechnician。希望这对大家都有帮助

$TimeStamp = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
$LogFile = Get-Date -Format "MM_dd_yyyy"

$path='C:\test\'
$targetDir = 'C:\test\Uploaded\'
$fileList = Get-ChildItem $path*.csv
If(!(Test-Path $TargetDir)){New-Item -ItemType Directory -Path $TargetDir|Out-Null}
$fileList | Select -ExpandProperty Name | Out-File 'C:\test\list.txt'

$list = get-content C:\test\list.txt 
foreach ($file in $list)
{
    $ftp = "ftp://REMOVED/$file"
    "ftp url: $ftp"
    $webclient = New-Object System.Net.WebClient
    $uri = New-Object System.Uri($ftp)
    "Uploading $file..."

    $succeeded = $true;
    &   {
    trap { $script:succeeded = $false; continue }
    $webclient.UploadFile($uri, $path+$file)
        }
if ($succeeded) 
        { 

        echo $file 'Was successfully uploaded!' $Timestamp >> logfile$LogFile.log
        move-Item  -path $path$file -destination $targetdir$Timestamp"_"$file
        #test-path $path$file
        } 

    else 
        { 

        echo $file 'Was not successfully uploaded, will retry later' $Timestamp >> logfile$LogFile.log

        }   


}

exit

这对我有用。谢谢@TheMadTechnician。希望这对大家都有帮助

$TimeStamp = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
$LogFile = Get-Date -Format "MM_dd_yyyy"

$path='C:\test\'
$targetDir = 'C:\test\Uploaded\'
$fileList = Get-ChildItem $path*.csv
If(!(Test-Path $TargetDir)){New-Item -ItemType Directory -Path $TargetDir|Out-Null}
$fileList | Select -ExpandProperty Name | Out-File 'C:\test\list.txt'

$list = get-content C:\test\list.txt 
foreach ($file in $list)
{
    $ftp = "ftp://REMOVED/$file"
    "ftp url: $ftp"
    $webclient = New-Object System.Net.WebClient
    $uri = New-Object System.Uri($ftp)
    "Uploading $file..."

    $succeeded = $true;
    &   {
    trap { $script:succeeded = $false; continue }
    $webclient.UploadFile($uri, $path+$file)
        }
if ($succeeded) 
        { 

        echo $file 'Was successfully uploaded!' $Timestamp >> logfile$LogFile.log
        move-Item  -path $path$file -destination $targetdir$Timestamp"_"$file
        #test-path $path$file
        } 

    else 
        { 

        echo $file 'Was not successfully uploaded, will retry later' $Timestamp >> logfile$LogFile.log

        }   


}

exit
基本内容包括:

  • 之前的测试路径