-3

I am using Get-ChildItem to navigate through a directory with the intention of recording file paths to a text file. However, I want to create the text files dynamically based on whether I'm in a specific folder or not. For example, let's say this is my file structure:

+--Root
   +--LaunchFromHere
      +--File1
         +--file_I_want1.doc
      +--File2 
         +--file_I_want2.doc

If my script starts in LaunchFromHere, I want to traverse File1 and File2 recursively, eventually arriving at the file_I_want.doc's. However, I want to record their file paths in different .txt files, each with the name of the file that I found it in. Thus when I find file_I_want1.doc, it's file path will be recorded in a text file called File1.txt.

Here's my problem: I can't find out how to get the filename of the file I'm currently inside in the script.

Based on similar questions on the site, this is my code:

$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$file_title = $directorypath.split('\')[-1]    

Get-ChildItem -include *.doc, -recurse | Select -Expand FullName |
        ForEach-Object {   
            $invocation = (Get-Variable MyInvocation).Value
            $directorypath = Split-Path $invocation.MyCommand.Path
            $file_title = $directorypath.split('\')[-1]
            "-----
            "Directory path: `t" + $directorypath
            "Current file: `t`t" + $file_title  
            "----"
        } 

My output only ever displays the file path where I ran the script, hence in this case:

PS C:\Root\LaunchFromHere
----
Directory path:    C:\Root\LaunchFromHere
Current file:      LaunchFromHere
---

And the title of my .txt is always LaunchFromHere.txt Edit: I have removed Out-File, but I do want my results to save to a text file.

So, how do I get the current name of the file that I'm traversing during the execution of the script?

gsamerica
  • 153
  • 1
  • 3
  • 18
  • 4
    It's clearly not possible that the code you posted gave you that output, because `Out-File` terminates your pipeline. You'll never get to the `ForEach-Object`. – Ansgar Wiechers May 16 '17 at 20:57
  • You're correct, in my original code I had other print statements that were printing out results that I didn't include for simplicity. Nevertheless, with `Out-File` removed, the output I get is still what I described. – gsamerica May 17 '17 at 12:45
  • To expand on my comment, what I mean is that the print statements in my personal code that I didn't include were the cause of the output I was receiving despite the Out-File. However, with the comments and Out-File removed, I'm still getting the output I described. – gsamerica May 17 '17 at 14:30

1 Answers1

0

The following code snippet could help:

Push-Location "C:\Root\LaunchFromHere"
Get-ChildItem -include *.doc -recurse | 
    ForEach-Object {
        $directorypath = $_.DirectoryName
        $file_title    = $_.Name
        (   "-----",
            "Directory path:`t$directorypath",
            "Current file:  `t$file_title",
            "---"
        ) | Out-File "$(Join-Path $directorypath "$($_.BaseName).txt")"
    } 
Pop-Location

It saves e.g. file_I_want1.txt in the same directory where the file_I_want1.docfile is located:

PS D:\PShell> Get-Content C:\Root\LaunchFromHere\File1\file_I_want1.txt
-----
Directory path: C:\Root\LaunchFromHere\File1
Current file:   file_I_want1.doc
---
PS D:\PShell>
JosefZ
  • 28,460
  • 5
  • 44
  • 83