1

Thanks to a couple people here I am able to connect to my WD NAS through command line.

Now I want to get the titles of the the movies I have stored there. I know about the dir /b > file.csv command, but this doesn't seem to work for my structure. I have a folder named Data (which I have X: mapped to this spot "\MYNAS\Data") and within the Data folder I have other folders such as Action, Drama, Documentary etc. So when I use the dir /b > file.csv command, it gives me the folder names inside Data and not the movies inside the folders.

Is there an easy way to pull the files from within each folder and make them all output to one CSV instead of going through and making a sperate csv for each folder?

I would prefer to solve this with command prompt because I am the most familiar with it, but I am open to using powershell as well.

Andrea Thacker
  • 3,440
  • 1
  • 25
  • 37
Mason Evans
  • 121
  • 3
  • 4
  • 12
  • possible duplicate of [Recursive directory listing in dos](http://stackoverflow.com/questions/2376801/recursive-directory-listing-in-dos) – Matt Jul 15 '15 at 16:39

1 Answers1

0

If you want to use the dir command in cmd.exe, you'll need the /S switch to go through the folder recursively, and possibly /A:-D to exclude directories and junctions from the result:

dir X:\Data /A:-D /S /B > files.csv

You could also use PowerShell, the equivalent cmdlet would be Get-ChildItem:

Get-ChildItem X:\Data -File -Recurse | select FullName | Out-File "files.csv"

As @Matt notes, this statement only works in PowerShell Version 3.0 or above. In earlier versions, the -File parameter doesn't work, so you'll have to filter directories out manually:

Get-ChildItem X:\Data -Recurse | Where-Object {-not $_.PSIsContainer} | Select FullName | Out-File "files.csv"

You can expand the Where-Object filter to exclude files with certain extensions with the Extension property:

$ExcludeExtensions = ".exe",".pdf"
Get-ChildItem X:\Data -Recurse | Where-Object {(-not $_.PSIsContainer) -and ($ExcludeExtensions -notcontains $_.Extension)} | Select FullName | Out-File "files.csv"
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Note that you would need PowerShell 3.0 for that to work. – Matt Jul 15 '15 at 16:40
  • I tried the first suggestion you gave me and once I omitted the \Data part it worked, except it is now giving me the full path of each file. So for example, instead of it just saying 'District 9', it is showing it as 'X:/Data/Distrcit9'. Any ideas for just getting the name and not the full location? – Mason Evans Jul 15 '15 at 16:55
  • @MasonEvans Sorry, but I think the full path output when using `/s` is by design. With PowerShell you can `Select Name` instead of `Select FullName` to just get the file name – Mathias R. Jessen Jul 15 '15 at 17:00
  • Okay thanks, and one last thing, what is the 'A:' for in that first example? – Mason Evans Jul 15 '15 at 17:02
  • `/A` is the `attribute` switch, see `dir /?` for more info – Mathias R. Jessen Jul 15 '15 at 17:06
  • I marked this as solved; however I've changed my mind and want to filter out the directories with powershell. I am getting this error when I use your second powershell suggestion (using powershell < 3.0) http://i.imgur.com/OL9SCUc.png; however this next picture proves I do have it mounted as X: http://i.imgur.com/tzLBwDq.png Anything I'm missing? I've also changed the script to say just X: instead of X:\Data since I think I have X: going straight into that Data folder already, but with or without the \Data it still gives me the same error. – Mason Evans Jul 17 '15 at 14:34
  • @MasonEvans if you're running PowerShell as administrator, you also need to map the drive "as administrator". Please see: http://serverfault.com/questions/673349/access-already-mapped-network-drive-in-powershell – Mathias R. Jessen Jul 17 '15 at 14:38
  • Awesome, now to filter out certain file types (.exe) I added the follwing: ? { $_.Name -notlike "*.exe" } however it is now showing the info inside the powershell console and not outputting the info into a csv. Here is what the code fully looks like: http://i.imgur.com/VUqmpXN.png Did I place it in the correct spot? EDIT: Nevermind, I got it. I forgot to add the pipe after the -notlike section. Thanks for your help Mathias. – Mason Evans Jul 17 '15 at 15:10
  • @MasonEvans Yeah, you need a pipe between the second `?` statement and `Select`. I've updated the answer with another example showing how to exclude multiple extensions – Mathias R. Jessen Jul 17 '15 at 15:20