-2

I am building a website with multiple folders that need a PHP script and JavaScript to organize the files correctly. I need to put the two files in a directory and all sub directories in that folder. I have tried to do this command

for /d %a in (c:\mp3filestructure) do copy /y c:\sortingscript\index.php  %a\

-minus the containers on the outside of the code. I have been doing this from a admin-cmd and it only copied to one folder! I am lost...

Matt
  • 45,022
  • 8
  • 78
  • 119
  • 1
    This is not PowerShell. Also what are you specifically trying to do and what exactly is not working? Thank you for including what you did try. – Matt Oct 16 '15 at 15:56
  • I am using cmd- I made a copy of the server tree in c:\mp3filestructure which has hundreds of directories and subdirectories. Then I made a folder c:\sortscript that has the two files I need in all the directories (index.php, and sorttable.js) now it worked for folders one level down with no spaces in the name but the sub directories were left untouched spaces or not, I have been trying this command and tweaking it slightly for /d %a in (c:\mp3filestructure\"*") do copy /y C:\sortscript\index.php %a\ – learningallican Oct 16 '15 at 16:09
  • I added the power-shell tag just in case I am using the wrong command line.. – learningallican Oct 16 '15 at 16:10
  • xist. At line:1 char:69 + gci C:\mp3filestructure -recurse | ?{$_.PSIsContainer} | %{copy-item <<<< C: \sortscript\index.php,C:\sortscript\sorttable.js $_} + CategoryInfo : ObjectNotFound: (C:\sortscript\sorttable.js:Stri ng) [Copy-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyI temCommand – learningallican Oct 16 '15 at 16:17
  • that is the error that I got in PowerShell...I am really new to all this so I need it spelled out. Plus I don't like to just copy and paste. I am really trying to learn so I like to understand every element of a command so that i can modify it later to fit my needs. Thanks for the help and the speed cause this site is not done until I finish this upload – learningallican Oct 16 '15 at 16:19
  • gci C:\mp3filestructure -recurse | ?{$_.PSIsContainer} | %{copy-item C:\sortscript\index.php,C:\sortscript\.sorttable.js $_} – learningallican Oct 16 '15 at 16:36

1 Answers1

1

In powershell it would look something like this:

$file1 = "PATH TO PHP SCRIPT"
$file2 = "PATH TO JAVASCRIPT"

foreach($directory in (get-childitem "PATH TO ROOT DIRECTORY YOU WANT TO COPY" -recurse| ?{$_.PSIsContainer}))
{
    copy-item $file1,$file2 $directory
}

The (get-childitem "PATH TO ROOT DIRECTORY YOU WANT TO COPY" -recurse | ?{$_.PSIsContainer}) line will get all directories under the root directory you are searching in. The -recurse switch says to go through the entire directory. When we say {$_.PSIsContainer} we are saying only return the objects that are directories, not files.

Example:

$file1 = "C:\phpScript.txt"
$file2 = "C:\javaScript.txt"

foreach($directory in (get-childitem "C:\CopyToDirectory" -recurse | ?{$_.PSIsContainer}))
{
    copy-item $file1,$file2 $directory
}

Or if you wanted to do it in one line, it would look like this:

get-childitem C:\CopyToDirectory -recurse | ?{$_.PSIsContainer} | %{copy-item C:\phpScript.txt,C:\javaScript.txt $_}

Here are the commands used

Get-ChildItem
Copy-Item
For-Each
A similar question

Community
  • 1
  • 1
AutomatedOrder
  • 501
  • 4
  • 14
  • You really shouldn't use aliases in code examples IMHO as at makes code very unreadable for people that are not very familiar with PS. – bluuf Oct 16 '15 at 17:13
  • GCI is an alias to Get-ChildItem, in other words it's another way to call the same command. I've updated the post without the aliases. – AutomatedOrder Oct 16 '15 at 18:13
  • You still have aliases for? And % – Matt Oct 16 '15 at 19:35