File search and copy script

This topic contains 4 replies, has 2 voices, and was last updated by  Jack 1 year ago.

  • Author
  • #72617


    Basically I have a list of file names in a text file. The files exist in two different locations, lets say locationA and locationB. The files could be sitting in different sub-folders within the folder root.

    What I need to do is find each file listed in the text file. Search for the file in locationA then find the file in locationB, more than likely a different folder structure then over write the file in the same location where it exists in locationB with the file in locationA.

    This is what I've got so far but it doesn't seem to be overwriting the files. Any help would be much appreciated. I guess I might have to do a query on the date modified date/time and if the source is greater to perform the copy then?

    #Read in file names
    $Filelist = 'C:\File_list.txt'
    #Source and Destination Paths
    $Src ='C:\Testing\Folder2'
    $Dst ='C:\Testing\Folder1'
    #Search source path for any files listed in file_names.txt
    Get-ChildItem $Srce -Recurse -Include (Get-Content $Filelist) | ForEach-Object {
        #Search destination path for file of the same name
        $Dstfile = Get-ChildItem $Dst -Recurse -Filter $_.Name
        #Copy file to file location in destination source
        Copy-Item $_.FullName $Dstfile.FullName -Confirm
  • #72619

    Don Jones

    WOW that is not going to run quickly if those locations contain a lot of files.

    I'm not sure why an additional check of date/time would matter, here, unless your goal is to "not overwrite newer files." Certainly, doing a check is not going to "make" PowerShell overwrite the file if it isn't already doing so.

    I suspect your problem is the exact problem everyone always has with PowerShell script bugs 😉 which is that you don't really KNOW what's in all of your variables. You're assuming, but you haven't checked. For example, in the top of your script you use $src, but later you use $srce. So it's possible your script is just doing nothing.

    I would, as a practice, build my scripts with verbose output. I'd start by setting $VerbosePreference='Continue' and then do stuff like:

    Write-Verbose "Retrieving $src"

    Before actually using the variable, to see what's in it. I'd do that each time a variable got populated with something, so that I could confirm it now contained what I thought it would.

    • #72629


      Hi Don, thanks for the update. The tree structure isn't very big and it isn't intended to grow that big so this shouldn't be a problem. Also it is the only way I can think about doing it.
      I tried to dump out the variables but the variable followed by the property isn't writing out. .Name or .FullName. Also $src and $srce was a type but thanks for highlighting.
      I think its back to the drawing board. If you can think of a simpler way for me to do this any ideas would be appreciated.

  • #72634

    Don Jones

    Inside double quotes you can't refer to variable property names without a subexpression.

    Write-Verbose "$($ is the content"

    Don't give up :). You're close, but you need to learn these debugging skills anyway, so this is a great time to do it.

    • #72637


      hmmmmmm thanks for the encouragement Don.
      I tried that outside of the for statement and it doesn't return anything within the verbose call but at least I get "VERBOSE:" to show the execution is running but there is nothing in the variable.
      If I attempt it within the for statement I don't even get "VERBOSE:" prompt which indicates, to me, it can't function within the for statement?
      I am a beginner when it comes to this so if this should be common sense I do apologise. I don't even know if this is the right way to go about this but hopefully I'll get there in the end.

You must be logged in to reply to this topic.