Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Write files in directory tree

Reply
 
Old 08-19-2007   #1 (permalink)
ArjanS


 
 

Write files in directory tree

For a project I'd like to do the following:

- Run a script from the top to the bottom of a directory tree
- In each folder of that tree a textfile must be created
- The name of the textfile has to be the current path of the folder

This way I would end up with a directory tree where each folder has a
textfile in it named after the location of the folder.

At this moment I started learning Powershell, and I think this could easily
be done. I started trying with the new-item command in combination with
recurse but it didn't work out.

Anyone any idea how to script this in powershell ? TIA



My System SpecsSystem Spec
Old 08-19-2007   #2 (permalink)
Shay Levi


 
 

Re: Write files in directory tree

You cant write text file with path charecters in it. Some charecters are
not allowed as file names,
you need to find another naming scheme for the text files.

As for the recurse operation, that can easily be done.

dir <path> -recurse | where {$_.psiscontainer} | foreach {new-item -type
file -path $_.fullname -name "yourname.txt"}

Shay
http://scriptolog.blogspot.com



> For a project I'd like to do the following:
>
> - Run a script from the top to the bottom of a directory tree
> - In each folder of that tree a textfile must be created
> - The name of the textfile has to be the current path of the folder
> This way I would end up with a directory tree where each folder has a
> textfile in it named after the location of the folder.
>
> At this moment I started learning Powershell, and I think this could
> easily be done. I started trying with the new-item command in
> combination with recurse but it didn't work out.
>
> Anyone any idea how to script this in powershell ? TIA
>



My System SpecsSystem Spec
Old 08-19-2007   #3 (permalink)
dreeschkind


 
 

RE: Write files in directory tree

Oneliner:

ls -rec -force | ?{$_.psiscontainer} | %{$_.fullname >
"$($_.fullname)\path.txt"}

--
greetings
dreeschkind

"ArjanS" wrote:

> For a project I'd like to do the following:
>
> - Run a script from the top to the bottom of a directory tree
> - In each folder of that tree a textfile must be created
> - The name of the textfile has to be the current path of the folder
>
> This way I would end up with a directory tree where each folder has a
> textfile in it named after the location of the folder.
>
> At this moment I started learning Powershell, and I think this could easily
> be done. I started trying with the new-item command in combination with
> recurse but it didn't work out.
>
> Anyone any idea how to script this in powershell ? TIA
>
>

My System SpecsSystem Spec
Old 08-19-2007   #4 (permalink)
dreeschkind


 
 

RE: Write files in directory tree

Ah, missed the fact that you wanted the path as the name of the file.
Try this instead:

ls -rec -force | ? {$_.psiscontainer} | % {ni -path
"$($_.fullname)\$($_.fullname -replace '\\','-' -replace ':','+')" -type file}

It replaces the backshlashes and the colon so the filename is valid.

--
greetings
dreeschkind

"ArjanS" wrote:

> For a project I'd like to do the following:
>
> - Run a script from the top to the bottom of a directory tree
> - In each folder of that tree a textfile must be created
> - The name of the textfile has to be the current path of the folder
>
> This way I would end up with a directory tree where each folder has a
> textfile in it named after the location of the folder.
>
> At this moment I started learning Powershell, and I think this could easily
> be done. I started trying with the new-item command in combination with
> recurse but it didn't work out.
>
> Anyone any idea how to script this in powershell ? TIA
>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Directory Tree Iterations General Discussion
Vista Won't Let Me Create XP-Style Directory Tree for Old Template Files Vista file management
Hard Drive Directory Tree Vista file management
How to open a file for read/write access in Program Files directory Vista security
Directory tree? Vista installation & setup


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46