Powershell script for comparing two directories based on file names only

Satfactor

I have two disks which has the same directory structure. C:\Files and D:\Files

Both C:\Files and D:\Files have multiple directories under them but have the same name etc, but the files inside them differ in extension. In C:\Files they are *.csv and in D:\Files, a process monitors the files (copied from C:\) and once it is done changes the files to *.processed.

I want a script that would do that copy. I.e copy files from C:\Files to D:\Files which have not been processed by comparing only the file names.

Bill Dinger

You want something like this. The property you want to compare on is called BaseName which powershell helpfully adds for you to the IO.FileSystemInfo class (FileInfo.BaseName exists in PowerShell but not in straight .NET). BaseName is just the name of the file, and doesn't contain any of the extensions that you don't care about.

$sourceFiles = Get-ChildItem C:\files -Recurse -File -Filter "*.csv"
$destinationFiles = Get-ChildItem D:\files -Recurse -File 

foreach($sourceFile in $sourceFiles) {
  $exists = $destinationFiles | Where-Object {$_.BaseName -eq $sourceFile.BaseName}
  if(!$exists) {
    Copy-Item $sourceFile.fullname -Destination "D:\Files\$($sourceFile.BaseName)"
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Powershell script for comparing two directories based on file names only

From Dev

Comparing names of two files in different directories

From Dev

Creating directories using file names in powershell

From Dev

Create QTreeWidget directories based on file paths/names?

From Dev

List only file names in directories and subdirectories in bash

From Dev

Comparing the contents of two directories

From Dev

Comparing the contents of two directories

From Dev

Comparing files in two different directories

From Dev

Comparing two directories to produce output

From Dev

Shell script to create directories and files from a list of file names

From Dev

cleaner script to recursively replace a string in all directories and file names

From Dev

Python: Create File Directories based on Dictionary Key names

From Dev

Python: Create File Directories based on Dictionary Key names

From Dev

Untar files to destination directories based on tar file names

From Dev

Create directories based on names in txt file with added strings

From Dev

Moving large number of files into directories based on file names in linux

From Dev

moving files into different directories based on their names matching with another file

From Dev

How to diff file names in two directories (without writing to intermediate files)?

From Dev

How to find directories containing two different file names?

From Dev

How to create multiple directories based on file names and change the file names in linux?

From Dev

Comparing two files in a script

From Dev

find command in bash script resulting in "No such file or directory" error only for directories?

From Dev

comparing two files based on a column and append elements in common to a file

From Dev

Comparing each file in two directories and copying it to another if it differs from its counterpart

From Dev

Comparing variable with File names bash

From Dev

Comparing file names in driectory with database

From Dev

Rename file names in multiple directories

From Dev

Check a PowerShell script line against a txt file containing computer names

From Dev

Comparing two dataframes based on a condition

Related Related

  1. 1

    Powershell script for comparing two directories based on file names only

  2. 2

    Comparing names of two files in different directories

  3. 3

    Creating directories using file names in powershell

  4. 4

    Create QTreeWidget directories based on file paths/names?

  5. 5

    List only file names in directories and subdirectories in bash

  6. 6

    Comparing the contents of two directories

  7. 7

    Comparing the contents of two directories

  8. 8

    Comparing files in two different directories

  9. 9

    Comparing two directories to produce output

  10. 10

    Shell script to create directories and files from a list of file names

  11. 11

    cleaner script to recursively replace a string in all directories and file names

  12. 12

    Python: Create File Directories based on Dictionary Key names

  13. 13

    Python: Create File Directories based on Dictionary Key names

  14. 14

    Untar files to destination directories based on tar file names

  15. 15

    Create directories based on names in txt file with added strings

  16. 16

    Moving large number of files into directories based on file names in linux

  17. 17

    moving files into different directories based on their names matching with another file

  18. 18

    How to diff file names in two directories (without writing to intermediate files)?

  19. 19

    How to find directories containing two different file names?

  20. 20

    How to create multiple directories based on file names and change the file names in linux?

  21. 21

    Comparing two files in a script

  22. 22

    find command in bash script resulting in "No such file or directory" error only for directories?

  23. 23

    comparing two files based on a column and append elements in common to a file

  24. 24

    Comparing each file in two directories and copying it to another if it differs from its counterpart

  25. 25

    Comparing variable with File names bash

  26. 26

    Comparing file names in driectory with database

  27. 27

    Rename file names in multiple directories

  28. 28

    Check a PowerShell script line against a txt file containing computer names

  29. 29

    Comparing two dataframes based on a condition

HotTag

Archive