2
votes

I am rather new to Powershell and have a question regarding the error I'm receiving. After browsing through stack overflow I have found that users have made errors in spelling and the like and so I haven't found a suitable answer to my problem.

I have one script that runs a backup of some data and compresses it and stores it as:

yyyyMMddsometext.7z

I have another script to get the latest backup (if it was created) and copy it to another location

I am receiving an error

copy-item cannot bind argument to parameter 'path' because it is null

Does this mean that the file is non-existent or is it an error in any of the below?

$c = $textBox.Text
$a = (Get-Date).AddDays(-1).ToString($c)                                                        
$b = Get-ChildItem "C:\BackupsRP1" -Filter *.7z | Where-Object BaseName -like "*$a*" 
Copy-Item $b -Destination "C:\Users\user\Desktop"

Above the code is a simple GUI for the user to input the date in the format yyyyMMdd and it will locate the file one less day than the user inputs and copy it to the location.

Thank you, J

2
The error states that $b doesn't hold any value. This is probaly because your filter doesnt't find a matching file, either because the file doesn't exist or the filter is wrong. Write $a to the console and make sure the filename is correct.Paxz
@Paxz Thank you for clarity. If the location that the file is located could be in any number of folders from monday to sunday would it still find the file (if it exists) and copy it? Or would I need to specify to search in each folder?jasony4
What's the value of $c?marsze
$c is a user input date in the format yyyyMMdd because the files I'm locating are backed up daily and we need to get the file that was backed up the day before the day we specified. So a GUI pops up saying "input the date in the format yyyyMMdd" and that is inserted in the value $c @marszejasony4
@jasony4 Should be fine then I guess.marsze

2 Answers

1
votes

$b might contain multiple files or even non at all, depending what your filter finds.

The correct why to do it:

# This will copy each of the files that
Get-ChildItem "C:\BackupsRP1" -Filter *.7z | where BaseName -like "*$a*" | Copy-Item -Destination "C:\Users\user\Desktop" -PassThru

This will copy all items that match the filter and output the copied files to the console afterwards.

Also, make sure that $a really contains what you want. (I cannot know since I don't know what is in your textbox.)

0
votes

You have to make sure the values in the variables are as expected, You can add logging for debugging this.

$c = $textBox.Text
$c > c:\temp\Debug.log
$a = (Get-Date).AddDays(-1).ToString($c)                                                        
$a >> c:\temp\Debug.log
$b = Get-ChildItem "C:\BackupsRP1" -Filter *.7z | Where-Object BaseName -like "*$a*" 
$b >> c:\temp\Debug.log
Copy-Item $b.FullName -Destination "C:\Users\user\Desktop"

$b will contain FileInfo object, you have to select the fullname property(the full path of the file) from the object.