Home Forums Chat Forum File list to txt file from CMD promt. Help please.

  • This topic has 32 replies, 9 voices, and was last updated 8 years ago by DezB.
Viewing 33 posts - 1 through 33 (of 33 total)
  • File list to txt file from CMD promt. Help please.
  • makecoldplayhistory
    Free Member

    Hi all,

    I have a very basic knowledge of CMD prompt.

    I want to use something like the following but excluding X,Y,Z folders, printing listing the contents of the rest of the folders (without file data, including sub-directories).

    dir /s /b > print_No_Data.txt

    Thanks

    Cougar
    Full Member

    Reckon you’ll struggle to do that exclusion in a one-liner. I’d probably be wrapping the DIR commands in a FOR loop in a .cmd script (or cracking out Powershell). The cmd FOR is quite powerful but the syntax is nuts. Here:

    http://ss64.com/nt/for.html

    When you say “without file data” do you mean the /brief output above, or do you just want the folder structure? /ad will list only directories.

    Pyro
    Full Member
    makecoldplayhistory
    Free Member

    Yes, just the brief data Cougar. I’ll have a better read of that link but there’s a brief break in the rain – time to ride home now.

    Pyro. Not sure if I missed something in that link Pyro but yes, taking that

    dir > filename.txt but extending with /s to include subfolders, b for brief data and also getting it to exclude specific folders in its output.

    DezB
    Free Member

    Ahem…, where’s the exclusions?

    Yeah Powershell. Let me have a go, I’m learning it 🙂

    Cougar
    Full Member

    Ahem…, where’s the exclusions?

    The most awesome bit about that is the commands on that kbase page are a more simplistic version of what’s actually in the OP. (-:

    Yeah Powershell. Let me have a go, I’m learning it

    Me too. So far my approach has been to write any scripts I need in .cmd script and then redo it as PS. I’m sure I’m missing the point somewhere here.

    DezB
    Free Member

    My approach is to Google the shite out of it 🙂 (Which the OP could’ve done really, but I’m bored)

    DezB
    Free Member

    well that was easy 🙂

    Cougar
    Full Member

    Are you prepared to share your answer with the class, young Dez?

    DezB
    Free Member

    Get-Childitem -recurse source-folder -File | where-object {$_.PSParentPath -notmatch “exclude_folder|exclude_folder2“} > filename.txt

    I’m sure there’s a posh Powershell way to pipe to a file, but why bother eh? 😉

    BigEaredBiker
    Free Member

    Get-ChildItem -Exclude 'directory' -Recurse | Select-Object -ExpandProperty FullName | Out-File 'C:\Temp\Results.txt'

    I think you could set a variable with multiple values for ‘directory’ but I am typing this from Ubuntu and haven’t got round to installing PoSH yet…

    NOTE: The whole thing might just fail 😛

    Ah, yeah like what he said…

    GrahamS
    Full Member

    Do you have grep?

    If so you could use it to filter out the folders you don’t want like this:

    dir /b /s | grep -Ev "(MyFolder1|BadFolder|IgnoreThis)" > file.txt

    DezB
    Free Member

    Yeah, mine actually gives more file info than he wanted, looking back at his post. So…

    Very similar Graham, I reckon.

    IHN
    Full Member

    Get-Childitem -recurse source-folder -File | where-object {$_.PSParentPath -notmatch “exclude_folder|exclude_folder2”} > filename.txt

    Ooh, I love it when you talk dirty.

    BigEaredBiker
    Free Member

    Ha, I think between the answers he should be able to figure it out now 😀

    Cougar
    Full Member

    I’ve codified your code markup for you where applicable.

    (amateurs 🙂 )

    EDIT: I broke it in the process, it really doesn’t like mixing code and italics. Here’s Dez’s:

    Get-Childitem -recurse source-folder -File | where-object {$_.PSParentPath -notmatch "exclude_folder|exclude_folder2"} > filename.txt

    (Muphry’s Law in action)

    Cougar
    Full Member

    Do you have grep?

    If so you could use it to filter out the folders you don’t want like this:

    Not on Windows (did anything ever come of bash for Windows?)

    I don’t think that would work anyway; it’d filter out the folders, but not the contents of those folders.

    DezB
    Free Member

    I can’t find the PowerShell equivalent to /b 🙁

    GrahamS
    Full Member

    Not on Windows (did anything ever come of bash for Windows?)

    I asked because I just ran that on my Windows box 😀

    (We have cygwin[/url] installed on all our development machines)

    I don’t think that would work anyway; it’d filter out the folders, but not the contents of those folders.

    It does because /b give you the full path on every line so it contains the folder name.

    Only issue would be if you have files/folders with the same names further down the tree, because they’d get filtered too. But you can fix that with a better regex.

    Cougar
    Full Member

    I can’t find the PowerShell equivalent to /b

    Isn’t it just a -name switch?

    makecoldplayhistory
    Free Member

    Thanks for all replies. I’ve just got home. Lots to do before I get time to properly look at the posts but it’s clearly not as simple as I imagined it was.

    It’s these threads where I remember how little I know about computers 🙂

    When I said I have a very basic knowledge of using CMD prompt, it would have been better to say, I know ping, -n, -t, cd, dir, print, shutdown, diskpart, list, select basically, how to create a bootable usb drive, how to navigate to a file/folder and how to check if the internet’s working (google’s pingable).

    Thanks again

    BigEaredBiker
    Free Member

    Ok, well I am sure this is solvable in PoSH using mine and Dez’ previous answers but if you must do it from Windows CMD

    try;

    dir /b /s | findstr /V "bad poor" > results.txt

    to exclude folders bad and poor

    Cougar
    Full Member

    It does because /b give you the full path on every line so it contains the folder name.

    Doh, of course it does! That makes it a bit easier.

    Cougar
    Full Member

    it’s clearly not as simple as I imagined it was.

    It’s not as complex as I thought it was, as I was misremembering how /b and /s worked together. See BigEaredBiker’s post, that looks good to me.

    DezB
    Free Member

    Isn’t it just a -name switch?

    Tried -file or -name and then the exclusion doesn’t work. I shall not give up, even if I have been superseded!

    BigEaredBiker
    Free Member

    DezB,

    Did you figure it out? I just combined your PoSH solution with mine and it works (there is probably a neater way)

    Get-Childitem -recurse /home/user -File | where-object {$_.PSParentPath -notmatch “Dropbox|Music”} | Select-Object -ExpandProperty FullName | Out-File filename.txt

    This dumped the contents of my /home/user directory excluding the Dropbox and Music directories to filename.txt.

    This was on the latest PowerShell Alpha on Ubtuntu 16.04 but should work exactly the same in Windows…

    DezB
    Free Member

    No, I went home 🙂

    Like it!
    I’ll have a look tomorrow – I need more challenges like this, best way to learn.

    makecoldplayhistory
    Free Member

    Thank you all. Worked perfectly.

    One day STW won’t come through and god knows what will happen to the universe!

    retro83
    Free Member

    Also you’d have to be sure that the folder names you are excluding cannot possibly come up in any of the filenames else they’ll be excluded too. 🙂

    GrahamS
    Full Member

    Yeah I mentioned that. Easy solved by being a bit more specific with the regular expression.

    e.g. by giving the full paths:

    dir /b /s | grep -Ev "C:\\Users\\makecoldplayhistory\\(secretstuff|pron|grot)"

    deadkenny
    Free Member

    Cougar – Moderator 
    Not on Windows (did anything ever come of bash for Windows?)

    Yes, it’s in the Anniversary Update of Windows 10 (64bit only).

    https://msdn.microsoft.com/en-us/commandline/wsl/install_guide

    Really quite neat too. Runs Ubuntu binaries without recompiling. It can even run gui stuff so long as you’ve got an X server running somewhere.

    Cougar
    Full Member

    Oh cool. I’ll have a play with that later. Ta.

    DezB
    Free Member

    I almost feel like I’ve achieved something this week 😆

Viewing 33 posts - 1 through 33 (of 33 total)

The topic ‘File list to txt file from CMD promt. Help please.’ is closed to new replies.