0

I searched, i googled.. about to smash my head on the table

how come this will not work?

move-Item  $path$file $targetdir

it gives me an error Move-Item : An object at the specified path C:\Repository\test.csv does not exist.

now if i debug this and i output using

write-output move-Item  $path$file $targetdir

and take that output and paste it (file name with path and destination) it works!

and trust me the file is there. =\


Code below

    $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
7
  • have you tried just putting double quotes around "$path$file" to invoke string interpolation? Commented Sep 24, 2014 at 16:57
  • @TheMadTechnician Correct I have, same error. Commented Sep 24, 2014 at 16:58
  • What happens if you do Test-Path "$path$file"? I am taking into account your last line in your question Commented Sep 24, 2014 at 17:01
  • @TheMadTechnician Output = True without the " and with Commented Sep 24, 2014 at 17:02
  • I just tried it, and was unable to replicate the issue. At this point we need more information. What version of PowerShell? Are $path, $file, and $targetdir strings? FileInfo objects? PSCustom objects? I used those variables, set them as strings, and ran your command without issues. $path='c:\temp\' $file='list.csv' and $targetdir='c:\temp\test' worked without errors (and moved the file) Commented Sep 24, 2014 at 17:06

4 Answers 4

2

Basics are:

  • Test-Path before you move it (file and destination)
  • Move the file, ensure you have permission (force it to move)

so:

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"
}

If you loop over a collection you have to use the ".FullName" property of the object:

Get-ChildItem $path | ForEach-Object { Move-Item $_.FullName $targetdir -Force }
Sign up to request clarification or add additional context in comments.

Comments

1

Does the target directory already exist? I believe Move-Item will fail if the target directory doesn't exist. If that's the case, you can simply test for existence of the directory beforehand and then create as necessary.

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

1 Comment

Yes the directory is there works fine if i dont use Variables but use exact strings @kris powell
0

This worked for me. Thank you @TheMadTechnician. Hopes this helps everyone

$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

1 Comment

Glad you got it working, just for the sake of maybe learning a technique or two take a look at what I just added to my answer and maybe there'll be a thing or two that you want to incorporate into your script sometime.
0

How about this then:

ForEach($File in $List){
    Join-Path $path $file | Move-Item -Dest $Targetdir
}

Edit: Also... your creation of list.txt, it bothered me so I had to comment. Format-Table should be used for formatting text, not for selecting a value to output to a file. There's a better way to do that, consider this alternative:

Get-ChildItem "$path*.csv" | Select -ExpandProperty Name | Out-File $pathlist.txt

Since you say that $path = 'C:\test\' you are adding extra backslashes in there that may cause issues for some commands.

Edit2: Ok, if that doesn't work, why not work with the files themselves instead of outputting to a file, importing from that file, and then working with things.

$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

Then if you really want a list of those file names just pipe $FileList to Select and then to Out-File

$FileList | Select -ExpandProperty Name | Out-File 'C:\Test\list.txt'

Here, look through this and see if there's anything you like. I made a few changes, such as declaring paths at the beginning for everything, I moved the WebClient object creation outside of the loop, and changed how things are displayed on screen. Plus I skip the entire exporting to text file and re-importing it.

$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
        }   

}

2 Comments

Look at the full code above that i created. Let me know if this helps. This will help understand it better. im basically uploading after i get the list and moving the files into an Uploaded folder.
I think i was over thinking it =| and it got messy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.