none
How to recursively search for a file in a directory with dynamic folders name RRS feed

  • Question

  • Hi,

    I have a process that creates dynamically creates folder in a directory. The folder contains different files but I need to write a script to search the folders for a particular file name and copy the file to a different location. Since the process creates the folders with dynamic folder names like (81392, 81393, 81394...), it's kinda difficult to hardcode the path because I wouldn't know what exact path the folder the file will be created into. Is there a way I could search for the file in the directory? I tried using wildcard for the search but it didn't work.

    $sourcefilename = "file.001"

    $checksourcefile = get-childitem -path $sourcefilelocation\* -include($sourcefilename) | where-object {!($_PSiscontainer)}

    In the $sourcefilelocation, a lot of folders get created everyday with dynamic folders name and the file I would like to copy is placed in one of the dynamic folders.

    Wednesday, April 11, 2018 3:09 PM

Answers

  • If you know there will be 81* file names, try this

    get-childitem -path "$sourcefilelocation\81*\*" -include($sourcefilename)

    or even this get-childitem -path "$sourcefilelocation\*\*" -include($sourcefilename)

    • Proposed as answer by Evgeny Fedorov Thursday, April 12, 2018 10:12 AM
    • Marked as answer by Dejip Thursday, April 12, 2018 6:33 PM
    Thursday, April 12, 2018 10:11 AM

All replies

  • What is "crates dynamically creates" a folder mean? 

    If you have folders with different names then you either have to have a pattern to match or something like a createtime to find a folder.

    Your question is very vague and confusing.  Your use of Get-ChildItem is quite odd and somewhat incorrect.

    To search for a file in a folder just name the file:

    Get-ChildItem "$sourcefilelocation\$sourcefilename"

    To recursively search through a set of subfolders:

    Get-ChildItem "$sourcefilelocation\$sourcefilename" -Recurse

    Nothing else is required. 

    Start here:

    help Get-ChildItem -online

    Before wasting a large amount of time you need to learn basic PowerShell.

    Learn PowerShell  

    Script requests
    PowerShell Documentation

    PowerShell Style Guidelines


    \_(ツ)_/

    Wednesday, April 11, 2018 3:28 PM
  • Thanks for your response, I was basically saying the file I'm trying to copy is not directly under the $sourcefilelocation directory, hence why I can't use $sourcefilelocation\$sourcefilename. My main issue is that I wouldn't know the folder name because they are dynamically created and numbered.

    The structure of the folder is like this: $sourcefilelocation\.....\$sourcefilename while the "..." would be a folder labeled numerically.

    Wednesday, April 11, 2018 3:47 PM
  • Nevermind, I figured it out. Thanks for your help though.

    Wednesday, April 11, 2018 3:56 PM
  • This will find all files with that name in any subfolder of the initial path.

    Get-ChildItem "$sourcefilelocation\$sourcefilename" -Recurse


    \_(ツ)_/

    Wednesday, April 11, 2018 3:58 PM
  • If you know there will be 81* file names, try this

    get-childitem -path "$sourcefilelocation\81*\*" -include($sourcefilename)

    or even this get-childitem -path "$sourcefilelocation\*\*" -include($sourcefilename)

    • Proposed as answer by Evgeny Fedorov Thursday, April 12, 2018 10:12 AM
    • Marked as answer by Dejip Thursday, April 12, 2018 6:33 PM
    Thursday, April 12, 2018 10:11 AM
  • I will try to explain this again.  A "recursive" search will search for the file in all subfolders.  This has always been true of Windows and was true in DOS.

    In PowerShell:

    dir c:\windows\test.txt -recurse

    In CMD.EXE:

    dir /S c:\windows\test.txt

    This will start the search in c:\windows and recursively search for "test.txt" in all subfolders.  There is no need for wildcards or an "Include" parameter.  Windows is designed to work like this.


    \_(ツ)_/


    • Edited by jrv Thursday, April 12, 2018 6:38 PM
    Thursday, April 12, 2018 6:37 PM