Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

powershell copy and rename specific folders

Posted on 2009-04-02
3
Medium Priority
?
1,617 Views
Last Modified: 2012-06-27
Hi,
I am a Powershell newbie and I am trying to make copies of specific folders and rename them to include the date in the same root folder. The are other folders in that folder that contain a previous date.
Example:

RootFolder
      Test1
      Test2
      1_1_09 Test1
      1_1_09 Test2

I want to achieve this:

RootFolder
      Test1
      Test2
      1_1_09 Test1
      1_1_09 Test2
      <current_date> Test1
      <current_date> Test2

How can I accomplish this?

Thank you!
0
Comment
Question by:mtnnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:mtnnet
ID: 24049066
I have found a script that I can modify to do what I need, but I was wondering if there is another solution.
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_22791544.html?sfQueryTermInfo=1+10+copi+folder+powershel
0
 
LVL 9

Accepted Solution

by:
Sander Stad earned 1500 total points
ID: 24057560
I tweaked my own script a little so it suites your needs. What it does is it looks up all the folder in a specific given directory. It checks if the directory doesn't start with a number. If a result is found it renames the directory.

I assumed that the dateformat was day_month_year. You can edit this by using the "d" for the day, "M" for the month and the "y" for the year.

The script asks for a parameter. Your commandline could look like this:
powershell Myscript.ps1 -folder C:\MyfolderThatHasToBeRenamed

Here is the script

Param($folder = $(Throw "No folder given"))
 
Function RenameFolder([string]$folder){
	# Generate the lookup array
	$arrNumbers = 1..31
	# Create the dateformat
	$date = Get-Date -format "d_M_yy"
	
	# Lookup all the folders in the given directory
	$arrFolders = Get-ChildItem $folder | Where {$_.psIsContainer -eq $true} | Sort-Object {$_.FullName} -Descending
	
	# Loop through the array
	Foreach($item In $arrFolders){
		# Convert the item to a string
		[string]$i = $item
		# Get the first character from teh string
		$sub = $i.Substring(0, 1)
		
		# If the substring is not in the number array than continue
		If($arrNumbers -notcontains $sub){
			# get the old folder
			$old = $item.FullName
			# create the new folder
			$new = $folder + "\" + $date + " " + $i
			# rename the folder
			Move-Item $old $new
		
		}
	}
}
 
# Test if the given folder exists
If(Test-Path $folder){
	RenameFolders $folder
}Else{
	Throw "The path to the folder doesn't exist"
}

Open in new window

0
 

Author Comment

by:mtnnet
ID: 24060433
sstad,
Thanks for replying and taking the time to modify your script.
This script will be used to copy VMs and it will be scheduled so I can't have user imput and  I don't need to move the contents, just copy them.  If I'm not mistaking move-item $old $new would move the whole old folder to the new folder.

I have also come up with another  simpler script because I also have other folders that do not start with numbers in the root folder. (I know, I didn't mention that previously)

I will give you points for answering most of the problem and giving me some ideas on how to improve my script.

Thank You!

$date = Get-Date -format "MM_dd_yyyy"
 
$arrSource = @("test1","test2")
$arrTarget = @("$date copy_of_test1","$date copy_of_test2" )
 
for ($i=0; $i -le $arrSource.Count-1; $i++)
{
New-Item $arrTarget[$i] -itemType directory
Copy-Item ("C:\Test\" + $arrSSource[$i] + "\*") $arrTarget[$i]
}

Open in new window

0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question