copy one folder

I want to write a script to check the folder for a server, and then another folder to check it in the folder, and if this folder exists, copy that folder to the other server from that server!
For example, when we run the script, we will use the name of the first folder, then if there is a folder name that you have in that folder, and if there is one to copy it, if it does not exist, it says there is no folder.

And what is the question? :wink:

2b|!2b

I think you mean:

$2b -or !$2b

:slight_smile:

Cosmic, can you give us specific examples of what you’re talking about, including folder paths/names, server names, etc…

Ex:

If folder “C:\blah” exists on Server1 & folder blah2 exists in it (i.e. C:\blah\blah2 exists), then copy folder blah2 from Server1 to another folder called remote_blah on Server2 (\Server2\remote_blah).

Something like that.

how can I writing a script that say one special folder(testfolder2) exist in a path:d:\dokument\test\2017 on server? and then copy this folder to 10 diffrent server
catalog names are not the same all the time and each time we have another name! next time testfolder3 becomes! I thought to use then first and when I write the folder names then look for folder in the path! $foldername = Read-Host “Enter folder to check”

You can check the existence of a file system object with Test-Path. You can copy file system objects with Copy-Item. And you can repeat a particular action with one of the in Powershell available loop constructs like Foreach or Foreach-Object.

We cannot write scripts for you. If you have a specific question to a particular piece of code you wrote you’re welcome to post this here and we can try to help you with this.
If you’re looking for prewritten scripts you can try here: https://gallery.technet.microsoft.com/scriptcenter or here: https://www.powershellgallery.com/

$folder1 = Read-Host “Enter folder to check”
$foldername = “D:\CosmicInst\R8$folder1”
#set folder you want to check for

$folder2 = Read-Host “Enter folder to check”
$foldername2 = “D:\CosmicInst\R8$folder1$folder2”

$source = “D:\CosmicInst\R8$folder1$folder2\Products\Manualhandling\IX3”
$destination = “C:\Users\baa065sa\Powershell skript$Destination”

$path = Test-Path $source
If ($path){Copy-Item $source -Destination “$destination” -Recurse}

Please format your code as code!
You create variables and you don’t use it. What for? And then you create a variable and use this variable to create it. If that really works (I did not try to test this) it will probably produce unpredictable results. Something like this should actually work:

$folder1 = Read-Host “Enter folder to check”
$folder2 = Read-Host “Enter folder to check”
$source = “D:\CosmicInst\R8$folder1$folder2\Products\Manualhandling\IX3”
$destination = “C:\Users\baa065sa\Powershell skript”
If (Test-Path $source){
Copy-Item $source -Destination “$destination” -Recurse
}

I will check if folder1 exist or not exist in sourc! And I will check If folder1 exist or not exist in distans!
if not exist I will create folder1 and folder2 in destans! distans iz 10 diffrent servers!

$folder1 = Read-Host "Enter folder to check"
$foldername1 = "D:\test\R8\$folder1"
If (test-path $foldername1) {Write-Host "folder exists on Server." -ForegroundColor Green}
Else {Write-Host "folder NOT exist on Server." -ForegroundColor Red}

		

$folder2 = Read-Host "Enter folder to check"
$foldername2 = "D:\test\R8\$folder1\$folder2" 
If (test-path $foldername2) {Write-Host "folder exists on Server." -ForegroundColor Green}
Else {Write-Host "folder NOT exist on Server." -ForegroundColor Red}


$serverlist1 = Get-Content 'C:\Users\test\Powershell skript\testservrar2.txt'
foreach ($Servers in $serverlist1)

{
$newfolder = New-Item "\\$serverlist1\C$\Ctest\$folder1\$folder2" -ItemType Dir
$source = "D:\test\R8\$folder1\$folder2\Products\Manualhandling\IX3" 	
$destination = $newfolder	

$path = Test-Path $source 			
If ($path){Copy-Item $source -Destination $destination -Recurse} 
}

If you want to limit the selection of the folder to a specific selection you could provide a list of selectable folders for the user. Then you don’t have to check the existence afterwards.

How can I write a script to check again and again that the directory is available or not?

What do you mean by “available”? Does this question still relate to your initial question?

$folder1 = Read-Host "Enter folder to check"
$foldername1 = "D:\test\R8\$folder1"
If (test-path $foldername1) {Write-Host "folder exists on Server." -ForegroundColor Green}
Else {Write-Host "folder NOT exist on Server." -ForegroundColor Red}

for example, when I write testfolder to check! If testfolder does not exist I will write again testfolder1 to check! if testfolder1 is not available I will write again!

Why? How often would you like to check the existence of a folder? If it’s not there and you’re not gonna create it it does not make much sense to check it again and again and again and again.

If you wnat to wait for another process to create the folder you can use a loop. A while loop or an do loop

The network path was not found. why ?

What network path? What are you talking about?

Is it really that hard to show a little empathy? You get free help here, so I don’t think it’s too much to ask for a little courtesy to show.

44/5000
When I run the script, I get the error below!

Copy-Item : The network path was not found.
At C:\Users\baa065sa\Powershell skript\kopiering.ps1:35 char:12
+ If ($path){Copy-Item $source -Destination $destination -Recurse}
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (\\server.l...L_8.1.1_09_005P:String) [Copy-Item], I
    + FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.CopyItemCommand
$serverlist1 = Get-Content 'C:\Users\baa065sa\Powershell skript\Levtestservrar2.txt'
foreach ($Servers in $serverlist1)

{

$newfolder = "\\$serverlist1\C$\Cosmicinst\$folder1\$folder2"
If (test-path $newfolder) {Write-Host "Katalogen finns redan i IX3 LEVTEST." -ForegroundColor yellow
exit
}
else{
$source = "D:\CosmicInst\R8\$folder1\$folder2\Products\Manualhandling\IX3" 	
$destination = $newfolder	

$path = Test-Path $source 			
If ($path){Copy-Item $source -Destination $destination -Recurse}