Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help getting recursive directory info

Status
Not open for further replies.

kmcferrin

MIS
Jul 14, 2003
2,938
US
Hey all, I'm new to Powershell (but who isn't, eh?), but I've been writing all sorts of management scripts in VBScript for years now, and I need help with a script. I have a VBScript that crawls a directory tree and, checks for files that haven't been accessed in over a year, and then outputs the name and path of all of these files to a text file. The main reason that I'm converting it to Powershell is that PS doesn't appear to be having trouble with foreign characters, specifically Japanese, whereas I can't get VBScript to do anything but crash when it gets to Japanese characters.

At any rate, I've just gotten started and below is all of the code that I have so far:

Code:
$target = $args[0]
$age = $args[1]

function crawltree($folder)
{
    $dirs = get-childitem $target -Recurse | Where {$_.psIsContainer -eq $true}
    $dirs | select-object FullName -verbose | out-file -filepath results.txt
}

crawltree($target)

I'm probably making this far too complicated, but the issue that I have at the moment is that when it puts the output to a text file I get the same text as I would on the screen rather than the full text. For example, if I have a particularly long path in the filename, when it gets to character 117 or so on that line it just substitutes "..." for the remainder of the name. Since this output file will be parsed by an archive process later, I actually do need the full path and filenames. Anyone know how I can make it output all of the data instead of doing a "yadda yadda yadda" to me?
 
Out-file formats file contents to look like console output. This causes the output to be truncated just as it is in a console window in most circumstances. For example, if you run the following command:
PS> Get-Command | Out-File -FilePath c:\temp\output.txt

The output will look like this:
CommandType Name Definition
----------- ---- ----------
Cmdlet Add-Content Add-Content [-Path] <String[...
Cmdlet Add-History Add-History [[-InputObject] ...
...

To get output that does not force line wraps to match the screen width, you can use the Width parameter to specify line width. Because Width is a 32-bit integer parameter, the maximum value it can have is 2147483647. Type the following to set the line width to this maximum value:
Get-Command | Out-File -FilePath c:\temp\output.txt -Width 2147483647
 
Ok so I just gave an explantion without a possible solution - my bad!!!
Instead of outputing a table what about outputting in list format, say:
Code:
$dirs | select-object FullName -verbose | Format-list | out-file -filepath results.txt

Just a suggestion, like you said we are all new at this...
 
OK... even better if you want to keep the Table format.
example:
Code:
 get-command | format-table -wrap -autosize | out-file -filepath c:\output.txt
Your example:
Code:
$dirs | select-object FullName -verbose | Format-table -wrap -autosize | out-file -filepath results.txt
 
Thanks for the help, it's still not working the way that I want it but at least I know more now.

I didn't realize that by default the output is in a table. That's why the columns are truncated. Outputting them results as a list gives me the entire file name/path, but it puts in line breaks at the width of the display and appends "FullName : " to the front of each line. I tried playing with the "-width" switch, but it makes each line that wide regardless of the length of the actual data, so I get a ton of whitespace. That might be workable if I could figure out the actual length of each line before I write it.

Don't get me wrong, Powershell looks pretty powerful, but this gets sooo frustrating. Microsoft has a tendency to try to make things easier for people by anticipating what they want and making it difficult to do other than what they think it is that you want to do. I don't want formatted text, I just want plain text outputted to a text file with a line break at the end of each line.

It VBScript it's really simple. I would just do:

Code:
Set objFile = objFSO.OpenTextFile(strOutputFile, ForAppending, TristateTrue)
objFile.Writeline objFileName.Path

Event the export cmdlets don't do that. Your options are CSV and XML. Why not plain old text?
 
From what you described (VBScript) and building on the command example, what about something like this?
Code:
Get-Command | Select-Object -Property CommandType,Name,Definition | Format-List | Out-File -FilePath C:\results.txt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top