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:
- root
- one
- layouts
- components
- help
- two
- layouts
- components
- help
- three
- layouts
- components
- help
- ... x 160
- one
and I need to copy the same file to the 'co开发者_开发百科mponents' folder.
thanks.
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
}
}
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
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
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"
精彩评论