2

I am using a Powershell Script which should create a file that includes the Directory-Order (folder, subfolder, files, etc.):

$path = "golf.de/dgv" 
Get-ChildItem -Path $folder -recurse | sort Directory, Name| format-Table -auto $path, Directory, Name | Out-File C:\Users\J.Kammermeier\Desktop\Johannes\testtext.txt

until now the output looks like this

C:\Users\J.Kammermeier\Desktop\Johannes                        Test-Datei1.txt         
C:\Users\J.Kammermeier\Desktop\Johannes                        Test-Datei2.txt
C:\Users\J.Kammermeier\Desktop\Johannes\Sonstige Datein\Musik  WACKEN.txt 

but I need it in this order:

.../Johannes                         Test-Datei1.txt 

...Johannes\Sonstige Datein\Musik    WACKEN.txt 

How to achieve it?

4
  • Please provide us the script, I will make it work for you :) Commented Aug 4, 2014 at 10:16
  • $path = "golf.de/dgv" Get-ChildItem -Path $folder -recurse | sort Directory, Name| format-Table -auto $path, Directory, Name | Out-File C:\Users\J.Kammermeier\Desktop\Johannes\testtext.txt Commented Aug 4, 2014 at 10:18
  • So... you just want the "C:\Users\J.Kammermeier\Desktop\" part removed, that's it? Commented Aug 4, 2014 at 12:02
  • yes only the "Johannes" and the folder after "Johannes" should be left Commented Aug 4, 2014 at 12:08

1 Answer 1

5

You'll have to mangle the Directory property a bit, using Select-Object and calculated properties:

# Set the path and folder property
$path = "golf.de/dgv"
$folder = "C:\Users\J.Kammermeier\Desktop\Johannes"

# Get the name of the parent folder (the part we want to remove)
$basePath = (Get-Item $folder).Parent.FullName

# Retrieve the files
$files = Get-ChildItem -Path $folder -Recurse 

# Select the Name property and then two calculated properties, "Directory" and "Path"
$files = $files |Select-Object @{Name="BaseURL";Expression={"$path"}},
                               @{Name="Directory";Expression={$_.Directory.FullName.Substring($basePath.Length - 1)}},
                               Name

# Sort them
$files = $files |Sort-Object Directory, Name
# Formatted output to file
$files | Format-Table -AutoSize | Out-File C:\Users\J.Kammermeier\Desktop\Johannes\testtext.txt

From the details, I guess that you're trying to audit the files for a website, you could combine the Path and Directory properties and fix the back slashes with -replace:

@{Name="URLPath";Expression={"$path/" + $($_.Directory.FullName.Substring($basePath.Length - 1) -replace "\\","/")}}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.