3

I have a folder named source. It's structure is like the following.

source\a.jpg
source\b.jpg
source\c.xml
source\d.ps1
source\subdir1\a.xml
source\subdir2\b.png
source\subdir3\subsubdir1\nothing.img

I want to list all the relative paths of folders, sub-folders and files in a text file say, out.txt. For above the output I expect is:

source\a.jpg
source\b.jpg
source\c.xml
source\d.ps1
source\subdir1\a.xml
source\subdir2\b.png
source\subdir3\subsubdir1\nothing.img
source\subdir1
source\subdir2
source\subdir3
source\subdir3\subsubdir1

You can see that the output includes individual folders and sub-folders too.

Note: I am in a folder just outside the source folder. I mean for example I am in fold folder which contains source folder -> fold/source but if your solution includes putting the script inside the source folder, thats fine too. Both solutions are fine. This may be easy but I am not familiar with powershell but can at least run scripts from it if given.

EDIT1: Okay, in the "duplicate question" the answer is for relative paths of individual files. But I also want the folders and sub-folders.

EDIT2: Okay, I wrote a command:

(gci -path source -recurse *.|Resolve-path -relative) -replace "","" | out-file -FilePath output.txt -Encoding ascii

Now, this command gives me the relative name of only subdirectory inside the source(in the actual source folder of mine with a different name; source is a dummy name obviously!). What should I change in this code to get other names of files inside the subdirectory in source.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Manish Kumar Sharma
  • 12,982
  • 9
  • 58
  • 105
  • possible duplicate of [How to convert absolute path to relative path in powershell?](http://stackoverflow.com/questions/12396025/how-to-convert-absolute-path-to-relative-path-in-powershell) – Bacon Bits May 27 '15 at 02:26
  • @BaconBits : No that is not duplicate. That question converts a "given" absolute path to relative path. Mine asks for something else. – Manish Kumar Sharma May 27 '15 at 02:31
  • Actually, it is exactly what you need to solve your problem. You just need to use it in a loop for all of the files/folders, and use `Get-ChildItem`. I would suggest starting with `Get-Help Get-ChildItem -Full`, then put some code together and come back if you have code that you need help with. We aren't here to write your code for you, but we will help you fix your code if you have some that throws errors that you don't understand. Things you need: `Get-ChildItem`, `ForEach`, `Set-Content`, and the code from the other 'duplicate' question's answer. – TheMadTechnician May 27 '15 at 03:47
  • @TheMadTechnician : See the edit2 – Manish Kumar Sharma May 27 '15 at 04:31

3 Answers3

3

This is clean one line answer, with no loops on my part to write, which does the job perfectly. I produced it by luck "playing around" a bit.

(gci -Path source -Recurse *.*|Resolve-Path -Relative) -replace "\.\\","" |
  Out-File -FilePath output.txt -Encoding ascii
Rob Thomas
  • 19
  • 4
Manish Kumar Sharma
  • 12,982
  • 9
  • 58
  • 105
1

Not very clean but this might be an option.

(Get-ChildItem -Recurse -Path "C:\ABC\source\").FullName|ForEach{[Regex]::Replace($_,'^C:\\ABC\\','')}
Peter Hahndorf
  • 10,767
  • 4
  • 42
  • 58
hysh_00
  • 819
  • 6
  • 12
0

I would probably do something like this:

$source = 'C:\path\to\source'

$parent  = [IO.Path]::GetDirectoryName($source) -replace '\\+$'
$pattern = '^' + [regex]::Escape($parent) + '\\'

Get-ChildItem -Path $source -Recurse | % { $_.FullName -replace $pattern }
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328