Thursday, 18 March 2021

PowerShell Script: Copy Several files with same name to a folder without overriding with a differentiator

Powershell Script Description:

PowerShell script that will go through a directory called "C:\Deploy"

that has several subfolders with a file named "index.html in each of the subfolders and copy all the files named "index.html to a new folder called "C:\AllIndex" but not overwrite.

The files after copying would be named index001.html, index002.html, index003.html...



#source dir

$src = "C:\Deploy"

#destination dir 
$destn = "C:\AllIndex"

 

#Get the list of index.html
$index_html_list = (Get-ChildItem -Path "$src" -Name "index.html" -Recurse -Force)

 

# initialize counter
$counter = 1

 

#run in loop for all index.html 
foreach ($html in $index_html_list){
$counter = $counter.ToString('000')

#copy each index.html to new file in destination dir 
Copy-Item -Path "$src\$html" -Destination "$destn\index$counter.html"
#increment counter
$counter = [int]$counter + 1

}

 

No comments:

Post a Comment