Dave Thieben
Dave Thieben

Reputation: 5427

script to automate copying a file?

A client needs an updated copy of a file propagated to about 160 variations of the same folder structure on a Windows box. can someone help me with a script to automate this? I'm a developer, but a n00b with scripts.

the folder structure looks like:

and I need to copy the same file to the 'components' folder.

thanks.

Upvotes: 0

Views: 3903

Answers (4)

foxidrive
foxidrive

Reputation: 41234

This should work in a batch file - or from the command line if you reduce all %% to %

@echo off
for /d /r "c:\root" %%a in (*) do if /i "%%~nxa"=="components" copy "d:\folder\file.txt" "%%a"

Upvotes: 0

Carl479
Carl479

Reputation: 519

Use Batch "ROBOCOPY"

I use it to update my removable drive (USB)

Syntax:

ROBOCOPY "C:\ From Dir" "C:\To Dir" [options] For more info, see http://ss64.com/nt/robocopy.html

Very good for script noobs

Upvotes: 0

jamason1983
jamason1983

Reputation: 441

Here is a powershell solution I cooked up that should do what you are asking. You willneed to edit the SourceDir, Dest, and the Filter to get the file or files you want.

$SourceDir = "C:\Source"
$Dest = "C:\Root"
$Files = gci -Path $SourceDir -Recurse -Filter "*.txt" |?{$_.Directory -ne $null}

$Index = ($Source.Length)
$SSFile = $UpdatedPath.Substring($Index)
$Index = $SSFile.LastIndexOf("\")
$ChildDir = $SSFile.Substring(0,$Index)
$Root = Dir -Path $Dest|?{$_.Directory -eq $null}
foreach ($File in $Files){
    foreach ($Folder in $Root){
        $FolderPath = $Folder.FullName
        $DropPath = ("{0}{1}" -f$FolderPath, $ChildDir)
        Copy-Item -Path $File.FullName -Destination $DropPath
    }
}

Upvotes: 2

Tamara Wijsman
Tamara Wijsman

Reputation: 12348

As a developer, just enumerate the folders on the first level and execute a copy command for each.

Pseudo code:

for each directory in /root

    copy file to /root/directory/components

Upvotes: 1

Related Questions