0

I am trying to copy, and rename multiple files to miltiple folders. The folders will be labeled sequentially like the files. For example I need to copy Reports.txt change the name to Reports_NW01_20120219.txt to a folder called NW01 then the same process for NW02-NW18. The folder and file names increase by 1. I am new to powershell so please spare me. I have pasted what I have below. I am attempting to read in the date and then append it to the file name. I am at a complete loss now. So any help is appreciated:

$date = read-host "Enter date of the report YYYYMMDD"
write-host $date  

for ($Counter = 1; $Counter -le 1; $Counter++) 
{     

    #Destination for files 
    $DropDirectory = "C:\Users\dh0520\Documents\Powershell Staging\"  
    #Current Location of the files
    $file = Get-ChildItem -Path "C:\Users\dh0520\Documents\Powershell Test\NW01\Reports.txt" 

    if ($Counter  -lt 10) {

        $Destination = $DropDirectory+'NW0' + $Counter  
        $file = Get-ChildItem -Path "C:\Users\dh0520\Documents\Powershell Test\NW0" + $Counter + "\Reports.txt" 
        Copy-Item $file.FullName ($Destination + $file.BaseName + "_NW0" + $Counter + "_" + $date +".txt")

    }

    If ($Counter -ge 10) { 

        $Destination = $DropDirectory+'NW' + $Counter 
        $file = Get-ChildItem -Path "C:\Users\dh0520\Documents\Powershell Test\NW" + $Counter + "\Reports.txt" 
        Copy-Item $file.FullName ($Destination + $file.BaseName + "_NW" + $Counter + "_" + $date +".txt")

    }   

}
1
  • What's the error message/problem? Commented Feb 20, 2014 at 10:08

1 Answer 1

1

I assume you are getting errors with the get-childItem. It is because you cant build the path like that. Try it this way:

$file = Get-ChildItem -Path "C:\test\NW0$($Counter)\Reports.txt"

and this way:

$file = Get-ChildItem -Path "C:\test\NW$($Counter)\Reports.txt"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.