Tag Archives: powershell move-item overwrite existing file

Move files to another directory Based on Filename with Powershell

Where this code required?

In many scenario, we will got the requirement for moving the specific files from the large number of files from one location to another location based some filter  on the file name. This code will helps with same situation. The below code will split the filename before “-” and compare the existing list “filterLists” to find 

STEP #1

Declare the souce and target path locations.

$srcpath = “C:\Powershelltest\Source”
$dstpath = “C:\Powershelltest\Destination”

STEP #2

Fetch all the child file list to make the loop to start the validation of each files.

$fileList = Get-ChildItem -Path $srcpath -Force -Recurse
foreach ($file in $fileList)
{
}

STEP #3

Second level of loop for validating whether filename is mathing with our input

foreach($filelist in $filterLists)
{
#Our Logic
}

Complete Code

############################################################
#Project : Move files to another directory by File name filter
#Developer : Thiyagu S (dotnet-helpers.com)
#Tools : PowerShell 5.1.15063.1155 
#E-Mail : mail2thiyaguji@gmail.com 
############################################################
#set Source and Destination folder location
$srcpath = "C:\Powershelltest\Source"
$dstpath = "C:\Powershelltest\Destination"
#Set the files name which need to move to destination folder
$filterLists = @("70022333","70022867")

#Get all the child file list with source folder
$fileList = Get-ChildItem -Path $srcpath -Force -Recurse
#loop the source folder files to find the match
foreach ($file in $fileList)
{
#checking the match with filterlist
foreach($filelist in $filterLists)
{
#$key = $file.BaseName.Substring(0,8)
#Spliting value before "-" for matching with filterlists value
$splitFileName = $file.BaseName.Substring(0, $file.BaseName.IndexOf('-'))

if ($splitFileName -in $filelist)
{
$fileName = $file.Name

Move-Item -Path $($file.FullName) -Destination $dstpath
}
}
}

Output

After Execution

What do you think?

I hope you have an idea of  Move files to another directory Based on Filename with Powershell. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.

How to move files from one location to another location using PowerShell

In this article, Let me show you how to move files from one location to another location using PowerShell script. The Move-Item cmdlet moves an item, including its properties, contents, and child items, from one location to another location. The locations must be supported by the same provider.

Move-Item : Cannot create a file when that file already exists.

Example:1 Moving single files/folders

In the PowerShell window, type the command below and press ENTER. After the –path parameter, type the path of the file on your local PC that you want to copy, and after the –destination parameter, type the path of the destination folder. In the example below, I’m moving a file called URL_List.txt from the Source File Path folder to the Destination File Path folder.

$_SourcePath = "D:\BLOG\Power Shell\Examples\Source File Path\URL_List.txt"
$_DestinationPath = "D:\BLOG\Power Shell\Examples\Destination File Path"

Move-item –path $_SourcePath –destination $_DestinationPath

OR we can directly mention the path of files location without -path Keyword

Move-item $_SourcePath $_DestinationPath

Example:2 Moving all the item which created today.

From below code, we getting all files and folder which created today and move from “Source File Path” folder to “Destination File Path” folder.

get-childitem -Path "D:\BLOG\Power Shell\Examples\Source File Path" -Recurse |
Where-Object {$_.CreationTime -gt (Get-date).Date} |
move-item -destination "D:\BLOG\Power Shell\Examples\Destination File Path"

Also, by default, Get-ChildItem does not move hidden files. To move hidden files, use the Force parameter with Get-ChildItem.

Example:3 Moving the files based on File Type (extension)

I had created $_FileType array which contains the list of file types that need to move. After execution, all .html and .txt files will move from source to destination folder. Please refer to the snapshots to under stand the before and after the script execution

#location of starting directory
$_sourcePath ="C:\Users\dotnet-helpers\Desktop\SourcePath"
#location where files will be copied to 
$_destinationPath = "C:\Users\dotnet-helpers\Desktop\DestinationPath";
#Array of extension that need to move from source path
$_FileType= @("*html*", "*.*txt")

Get-ChildItem -recurse ($_sourcePath) -include ($_FileType) | move-Item -Destination ($_destinationPath)

OUTPUT:

Before Execution: Source Folder/Destination Folder

After Execution: Source Folder/Destination Folder

 

What do you think?

I hope you have an idea of how to move the files from one location to another location using the Powershell script. I would like to have feedback from the readers of my posts. Your valuable feedback, question, or comments about this article are always welcome.