回答済み Outputting directory info to a file in csv format

  • 2012年6月4日 18:32
     
      コードあり

    I am working on creating a report on a file structure so it can be opened in excel and analyzed.

    The folder structure is set up like this

    Share
        User
             Machine1
                  File1, 2, 3, 4, 5..etc

            Machine 2
                  File 1, 2, 3, 4, 5.. etc

        User
            Machine
                 File 1,2,3,4,5 etc..

    Under each user folder there will be 1 to many machine folders, and under each machine folder there will be 0 to many files.  I would like to create a csv file that has the following:

    user,machine,filedate,filesize,file, pathtofile

    I have a script that does all of the heavy lifting - walks through the folders and files and gets the information I want - however - I cannot seem to get it to output the way I want it to.  I have tried Out-File, ConvertTo-Csv - but the only thing that seems to get me close is write-host and manually put commas in between each variable.  I am thinking that there has to be a better way to do that than to use write-host and >>

    Thanks

    sb

    # get all of the users folders
    $users = Get-ChildItem -Path $backuppath | Where-Object {$_.psIsContainer -eq $true}
    
    # loop through the users folders
    $users | ForEach-Object {
    	# get the machines
    	$user = $_;
    	$machines = Get-ChildItem -LiteralPath $backuppath\$user;
    	
    	$machines | ForEach-Object {
    		# loop through all of the machines for pst files
    		$machine = $_;
    		$pstfiles = Get-ChildItem -LiteralPath $backuppath\$user\$machine -Filter *.pst -Recurse  | Where-Object {$_.Name.ToUpper() -notlike 'MAILBOX' -and $_.psIsContainer -eq $false}
    	}
    	
    	$pstfiles | ForEach-Object {
    		 Write-Host $user.Name$machine.Name $_.name $_.lastwritetime$_.length$_.DirectoryName 
    	}
    
    }

すべての返信

  • 2012年6月4日 21:17
     
     回答済み コードあり

    You are making much to big of a project out of this:

    This is very close to what you want

    #You are making much to big of a project out of this:
    Get-ChildItem e:\test2| ?{$_.PSIsContainer} | 
        ForEach-Object{
            Get-ChildItem $_.Fullname -recurse |
                where-Object{-not $_.PSIsContainer} |
                    Select-Object `
                        @{N='User';E={$_.Directory.Parent.Direcroty.Name}}, `
                        @{N='Machine';E={$_.Directory.Name}}, `
                        Name, Fullname, LastWriteTime

    In the abovwe we pull all folders undet teh top level and then get all files recursively.  Printing the inheritance of each file back two gernerations will give you the nesting.  The current folder is teh 'Machine' and the parent of that folder is the 'User'

    Sorry I mucked up the first post.

    This will work but only if the folder strructure is EXACTLY as you have stated.  any variateions will cause unusual inclusions due to failure of the initial assumptions.

    ¯\_(ツ)_/¯