Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a file called SRED.txt and a folder which contains hundreds of subfolders. In each folder is another folder called SRED where this SRED.txt file should be saved. What I want to do is loop through each folder and copy the SRED.txt file into the corresponding SRED folder.

How can I accomplish this with Powershell?

share|improve this question

Get the SRED directories recursively, then copy the file for each directory.

dir  c:\Projects SRED -Recurse|
    where {$_.PSIsContainer}|
    foreach { copy C:\Path\To\SRED.txt $_.FullName }

With the new -Directory parameter in PowerShell v3:

dir  c:\Projects SRED -Recurse -Directory|
    foreach { copy C:\Path\To\SRED.txt $_.FullName }
share|improve this answer
    
Thanks so much for your help with this. – JasonR Jan 4 '13 at 13:56
    
I will let you know how it goes. – JasonR Jan 4 '13 at 14:00
    
There seems to be a problem with the Foreach loop. Do you know of any decent resources for me to help learn this? I'm not sure how the syntax of Powershell goes. – JasonR Jan 4 '13 at 15:00
    
PowerShell has an excellent built-in help system. To see all of the help for any CmdLet, type help <CmdLet name> -full. For Foreach-Object: help foreach -full. To see help for additional topics type help about* which will give a list of 'about' topics such as help about_Command_Syntax. If you hover over the PowerShell tag in your question then click on the 'info' link on the popup, there are some links to resources about PowerShell. – Rynant Jan 4 '13 at 18:37

I don't think you need something like PS for this. Open a Command Prompt window and navigate (using cd) to the top-level folder (which contains all your project folders). Make sure the SRED.txt file is present in this top-level folder. Now run the following command:

for /f "delims=" %d in ('dir SRED /ad /b /s') do @xcopy SRED.txt "%d"
share|improve this answer
    
Thanks so much for your help with this. – JasonR Jan 4 '13 at 13:59
    
I will let you know how its goes. – JasonR Jan 4 '13 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .