2

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

6
  • 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. Commented Dec 18, 2018 at 11:25
  • @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? Commented Dec 18, 2018 at 11:32
  • What's the value of $c? Commented Dec 18, 2018 at 11:35
  • $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 @marsze Commented Dec 18, 2018 at 11:38
  • @jasony4 Should be fine then I guess. Commented Dec 18, 2018 at 11:41

2 Answers 2

1

$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.)

Sign up to request clarification or add additional context in comments.

2 Comments

Going to try this will get back to you. Thanks for all the help!
@jasony4 Another possible pitfall: Are the files in the top level of the folder or any sub folders? Even I still forget -Recurse sometimes.
0

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.

2 Comments

Thank you! Very helpful tool. I have found that the file didn't exist which is why I was getting an error returned. The file that needs to be copied could be in any one of the folders labelled from Sunday to Monday. Do I need to create loops to search through each folder or if I specify to BackupsRP1 will it search all folders within that directory?
You can use -Recurse with -Include Monday,Tuesday,...,sunday for Get-ChildItem

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.