我搜索了一下,我用Google搜索了......我的头撞在了桌子上
为什么这不起作用?
move-Item $path$file $targetdir
它给了我一个错误 Move-Item:指定路径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
答案 0 :(得分:1)
目标目录是否已存在?我相信如果目标目录不存在,Move-Item将失败。如果是这种情况,您可以预先测试目录是否存在,然后根据需要进行创建。
If (!(Test-Path -Path $targetdir)) {
New-Item -ItemType directory -Path $targetdir
}
答案 1 :(得分:1)
基础是:
这样:
echo $targetdir
echo "$path$file"
if (!(Test-Path $targetdir)) {
New-Item -ItemType directory $targetdir
}
if(Test-Path "$path$file") {
Move-Item "$path$file" $targetdir -Force
} else {
echo "file does not exist"
}
如果你循环一个集合,你必须使用" .FullName"对象的属性:
Get-ChildItem $path | ForEach-Object { Move-Item $_.FullName $targetdir -Force }
答案 2 :(得分:0)
那么:
ForEach($File in $List){
Join-Path $path $file | Move-Item -Dest $Targetdir
}
编辑:另外......你创建了list.txt,这让我感到困扰,所以我不得不发表评论。 Format-Table应该用于格式化文本,而不是用于选择输出到文件的值。有更好的方法,考虑这个替代方案:
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传递给Select,然后传递给Out-File
$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
}
}
答案 3 :(得分:0)
这对我有用。谢谢@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