How To Write New Checksums Files To Alternate Location

Welcome Forums General PowerShell Q&A How To Write New Checksums Files To Alternate Location

Viewing 9 reply threads
  • Author
    Posts
    • #242636
      Participant
      Topics: 1
      Replies: 4
      Points: 22
      Rank: Member

      I am using the following script to generate a checksum file for hundreds of files in several subdiretories.  The files are generated as expected in those directories. However I cannot figure out how to tell the files to be written to a different location that with their original source file. example write the files to c:\checksum\projects.

      I know this will be a simple question/answer for someone that uses powershell daily.

       

      $files = Get-childitem -Path c:\projects -recurse -include * -Exclude “*.md5”

      Foreach ($file in $files)
      {
      $hash = (Get-FileHash -Path $file.fullname -Algorithm SHA256).hash
      Add-Content -Path ($file.fullname + ‘.checksum’) -Value $hash
      }

       

    • #242654
      Participant
      Topics: 8
      Replies: 568
      Points: 2,171
      Helping Hand
      Rank: Community Hero

      Are you wanting all the files in the same folder or are you wanting to also have their original directory structure under c:\checksum\projects?

      This will dump all the checksum files in the c:\Checksum\Projects directory, so beware of name collisions.

      The previous example uses a foreach statement construct which will load everything into memory. It’s very fast, but if you have a large amount of files this could be an issue. The following example changes it to utilize the pipeline.

      I hope this helps

    • #242660
      Participant
      Topics: 8
      Replies: 568
      Points: 2,171
      Helping Hand
      Rank: Community Hero

      Are you wanting all the files in the same folder or are you wanting to also have their original directory structure under c:\checksum\projects?

      This will dump all the checksum files in the c:\Checksum\Projects directory, so beware of name collisions.

      The previous example uses a foreach statement construct which will load everything into memory. It’s very fast, but if you have a large amount of files this could be an issue. The following example changes it to utilize the pipeline.

      or even

      I hope this helps

    • #242681
      Participant
      Topics: 1
      Replies: 4
      Points: 22
      Rank: Member

      Actually I should have specified that originally. The same directory structure to avoid the issue of collisions.

    • #242705
      Participant
      Topics: 8
      Replies: 568
      Points: 2,171
      Helping Hand
      Rank: Community Hero

      It’s not pretty, and I’m sure there are better ways to do it, but this works assuming the targets are on the C: drive. If not, replace C: with the appropriate letter.

      or

    • #242723
      Participant
      Topics: 1
      Replies: 4
      Points: 22
      Rank: Member

      FANTASTIC! The last example works as I needed. I really appreciate it.

      One last question. Is there a way to set the date/time of the newly created checksum file to the same date/time of the original file it was created from?

    • #242804
      Participant
      Topics: 8
      Replies: 568
      Points: 2,171
      Helping Hand
      Rank: Community Hero

      I bet there is. Check this out and see what you can come up with. If you hit a snag after giving it a shot, let us know.
      https://www.ghacks.net/2017/10/09/how-to-edit-timestamps-with-windows-powershell/

    • #244115
      Participant
      Topics: 1
      Replies: 4
      Points: 22
      Rank: Member

      Thanks for all of the help.

      One least question. I have everything figured out except for one item. Can you provide me an example of how to skip writing the .checksum file if it already exists?

    • #244124
      Participant
      Topics: 8
      Replies: 568
      Points: 2,171
      Helping Hand
      Rank: Community Hero

      You don’t need to check that the file matches a specific criteria or is a certain age? If not, then we can test-path like we did for the folder and only if it’s not found would we calculate the hash and write it.

      or

    • #244139
      Participant
      Topics: 1
      Replies: 4
      Points: 22
      Rank: Member

      I was over complicating it. Thank you for the quick response. Works perfectly!

Viewing 9 reply threads
  • You must be logged in to reply to this topic.