PowerShell to find and copy a file with wildcard

%3CLINGO-SUB%20id%3D%22lingo-sub-1933889%22%20slang%3D%22en-US%22%3EPowerShell%20to%20find%20and%20copy%20a%20file%20with%20wildcard%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1933889%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EI%20need%20to%20copy%20a%20file%20which%20is%20located%20under%20a%20dynamically%20created%20subdirectory.%3C%2FP%3E%3CP%3EDirectory%3A%20C%3A%5CUsers%5CStandardUser%5CAppData%5CRoaming%5CMozilla%5CFirefox%5CProfiles%5Cdynamically-created-directory%5CmyDesiredFile.sqlite%3C%2FP%3E%3CP%3E%24SourceDir%20%3D%22C%3A%5CUsers%5CStandardUser%5CAppData%5CRoaming%5CMozilla%5CFirefox%5CProfiles%22%3CBR%20%2F%3E%24DestinationDir%20%3D%20%22C%3A%5CFireFox%5C%22%3C%2FP%3E%3CP%3E%23%20Wildecard%20for%20filter%3CBR%20%2F%3E%24FileNameAndExtension%20%3D%20%22places.sqlite%22%3C%2FP%3E%3CP%3E%23%20Copy%20File%3CBR%20%2F%3ECopy-Item%20%24SourceDir%20%24DestinationDir%20-Filter%20%24FileNameAndExtension%20-Recurse%3C%2FP%3E%3CP%3EProblem%3A%20using%20the%20above%20PS%20settings%2C%20I%20am%20getting%20whole%20*PROFILE*%20ddirectory%20but%20I%20want%20to%20copy%20only%20file%20as%20defined%20in%20FileNameAndExtension%20variable%20...%20possible%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1933889%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1943844%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20to%20find%20and%20copy%20a%20file%20with%20wildcard%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1943844%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F882489%22%20target%3D%22_blank%22%3E%40_OSD_%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%3C%2FP%3E%3CP%3EYou%20can%20use%20the%20following%20command%3C%2FP%3E%3CP%3EYou%20can%20use%20Join-Path%20to%20build%20the%20path%20and%20include%20the%20random%20folder%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24x%3Djoin-path%20('C%3A%5CUsers%5CFaris%5CAppData%5CRoaming%5CMozilla%5CFirefox%5CProfiles')%20-ChildPath%20(Get-childItem%20-Path%20'C%3A%5CUsers%5CFaris%5CAppData%5CRoaming%5CMozilla%5CFirefox%5CProfiles')%5B0%5D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EPlease%20note%20that%20this%20will%20fetch%20the%20first%20directory%2C%20if%20there%20are%20multiple%20directories%20then%20you%20need%20to%20build%20small%20criteria%20to%20define%20which%20one%20to%20retrieve.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E------------%3C%2FP%3E%3CP%3EIf%20this%20answer%20helped%2C%20please%20click%20on%26nbsp%3B%3CSTRONG%3EBest%20Response%26nbsp%3B%3C%2FSTRONG%3Eand%20also%20a%26nbsp%3B%3CSTRONG%3ELike%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

Hi,

I need to copy a file which is located under a dynamically created subdirectory.

Directory: C:\Users\StandardUser\AppData\Roaming\Mozilla\Firefox\Profiles\dynamically-created-directory\myDesiredFile.sqlite

$SourceDir ="C:\Users\StandardUser\AppData\Roaming\Mozilla\Firefox\Profiles"
$DestinationDir = "C:\FireFox\"

# Wildecard for filter
$FileNameAndExtension = "places.sqlite"

# Copy File
Copy-Item $SourceDir $DestinationDir -Filter $FileNameAndExtension -Recurse

Problem: using the above PS settings, I am getting whole *PROFILE* ddirectory but I want to copy only file as defined in FileNameAndExtension variable ... possible?

 

 

 

2 Replies

@_OSD_ 

Hi

You can use the following command

You can use Join-Path to build the path and include the random folder

$x=join-path ('C:\Users\Faris\AppData\Roaming\Mozilla\Firefox\Profiles') -ChildPath (Get-childItem -Path 'C:\Users\Faris\AppData\Roaming\Mozilla\Firefox\Profiles')[0]

Please note that this will fetch the first directory, if there are multiple directories then you need to build small criteria to define which one to retrieve.

 

 

------------

If this answer helped, please click on Best Response and also a Like

 

 

@_OSD_  I would try something like the code below, assuming there are multiple profile directories and you want to replicate that in the destination directory.

 

$SourceDir = 'C:\Users\StandardUser\AppData\Roaming\Mozilla\Firefox\Profiles'
$DestinationDir = 'C:\FireFox\'
$FileNameAndExtension = 'places.sqlite'

$SQLiteFiles = Get-ChildItem -Path "$SourceDir*\$FileNameAndExtension"

foreach($SQLiteFile in $SQLiteFiles){
    
    $ParentFolder = $SQLiteFile.Directory -replace [regex]::Escape("$SourceDir")

    New-Item -ItemType Directory -Name "$DestinationDir$ParentFolder"
    Copy-Item -Path $SQLiteFile -Destination "$DestinationDir$ParentFolder"

}

 

Hopefullly I have understood what you were after. ;)