Recursive Folder/File Copy Powershell Script - Robocopy won't stop copying once done with list - powershell

I'm trying to come up with a PS script that reads folder names from a txt file and recursively finds the folders/files in a source directory and then uses robocopy to copy to a destination directory, keeping the folder names, structure and timestamps. It looks like (in the log output) the script goes down the list and copies the folders and files over just great, but when it gets to the end of the list, it begins copying EVERYTHING else in the source directory.
The txt file data looks like:
400033
400042
400045
400047
400058
etc..
The 'script' looks like:
foreach ($line in
[System.IO.File]::ReadLines("C:\temp\filecopy\TEST_400210_400033.txt")) {
robocopy "J:\testSRC\$line" "S:\testDEST\$line" /DCOPY:DAT /E /R:3 /W:15 /log+:C:\temp\logs\TEST_ROBOCOPY_filecopy_Log1.txt /v /tee
}
Can anyone point me in the right direction please?
I've tried using Powershell's Copy-Item in a whole different script and was able to get that script to work but it's way too slow. Trying robocopy now. It reads and copies the files it is supposed to in the order it's supposed to, but once it's copied the list it starts a whole copy of the entire directory instead of stopping at the files in the text file.

Is there an extra line at the end of the file?
400033
400042
400045
400047
400058
[Blank Line]
i.e. it runs through all the file names then the last line will translate to:
$line = ""
Therefore:
robocopy "J:\testSRC\" "S:\testDEST\" /DCOPY:DAT /E /R:3 /W:15 /log+:C:\temp\logs\TEST_ROBOCOPY_filecopy_Log1.txt /v /tee
e.g. robocopy the root folder: robocopy "J:\testSRC\"
EDIT:
Try outputting the file to the screen to verify the input file is correct and listing the expected directories.
foreach ($line in
[System.IO.File]::ReadLines("C:\temp\filecopy\TEST_400210_400033.txt")) {
Write-Host "J:\testSRC\$line"
}

Related

Get path for all files in folder exclude 1 file. Command Line

I am trying get path all files and folder in indicated path and except folder Sql as a list. Only folders in the indicated location, no subfolders and their contents.
I am trying this:
powershell Get-ChildItem bin\Release\* -Exclude Sql -Name
But it return only name without path. I need bin\Release\ + Name or full path.
I was trying to:
for /f "usebackq tokens=*" %a in (`dir /b/n "bin\Release\*"`) do echo bin\Release\%~a
But don't know how change echo to list, and how remove folder Sql.
I will be grateful for your help, I cannot cope with it myself.

copy "this folder" to network drive (Robocopy or PowerShell)

Have a folder structure on local drive like this
and a similar structure on network drive like
C:\Documents\Projects\
\\myServer\Storage\Projects\
For each new project I create locally folders like
\Projects\abc\Subfolder1
\Projects\abc\Subfolder2
I need PowerShell or Batch file. The batch is stored in folder abc and it shall perform
Robocopy C:\Documents\Projects\abc \\myServer\Storage\Projects\abc
(of course some switches are reasonable)
Next project, for example xyz
\Projects\abc\Subfolder1
\Projects\abc\Subfolder2
I expect
Robocopy C:\Documents\Projects\xyz \\myServer\Storage\Projects\xyz
Each time I create new project I copy my sample folder structure that is supposed to contain the batch so I can quickly do copy for the project to network,
I think this can be solved by using variables for local and server base path or by doing som
I don't know which Robocopy parameters you need, but this could be a start. This assumes that you place this script in each \Projects\abc directory and run it from there.
$DestinationShare = '\\myServer\Storage'
$SourcePath = $PSScriptRoot
$DestinationPath = $PSScriptRoot -replace ".*?(?<path>\\Projects\\.*)","$DestinationShare`${path}"
robocopy $SourcePath $DestinationPath
This could be turned into a function that would have less static coding.
It is going to be simpler to just robocopy the entire Projects directory whenever it is required.
In batch you could do something like this:
#echo off
set "src=%~dp0"
set "dst=\\myServer\Storage"
for /d %%d in (%src%.) do set "name=%%~nd"
robocopy %src% %dst%\%name%\ /mir
%~dp0 is the directory in which the script resides. The for loop then extracts the name of that directory from the full path.

Copying folder structure to location that doesn't exist

I want to copy a folder, complete with subdirectories, files and files within subdirectories, preserving the structure and create them in a new location that did not previously exist. This is my PowerShell code
Copy-Item c:\development\powershell\folderone\* c:\development\powershell\foldertwo -recurse -Container
Copy-Item c:\development\powershell\folderone\* c:\development\powershell\folderthree -recurse -Container
foldertwo exists and is empty, folderthree does not exist.
If I run the script, the structure is created correctly in foldertwo, however folderthree gets created, but contains only all the files from the entire substructure, all at the root folderthree level. It has not recreated the subfolders within folderone, just put all the files at the root level of folderthree.
What have I done wrong?
Here's a very basic, but fully working and tested example, building on your confirmation above that I understood the issue at-hand:
$folderlist = ("foldertwo", "folderthree")
foreach ($folder in $folderlist)
{
if (!(Test-Path "C:\Development\PowerShell\$folder"))
{
mkdir ("C:\Development\PowerShell\$folder") | Out-Null
}
Copy-Item c:\development\powershell\folderone\* c:\development\powershell\$folder -recurse -Container
}
From what I understand, the question is about recreating the folder structure from [source] to [destination]. As using CmdLets is kind of overkill (and performance loss), I suggest simple batch command that may also be ran in powershell.
xcopy [source] [destination] /T /E
xcopy is a function to copy file and directory trees.
Help provides us info on usefult parameters on the case:
/T Creates directory structure, but does not copy files. Does not
include empty directories or subdirectories. /T /E includes
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.

Robocopy - make a copy of single file in same directory

Using robocopy is it possible to make a copy of a file in same directory?
Something like this...
robocopy c:\temp\file.txt c:\temp\file_copy.txt
COPY could be used. Use /Y to confirm an overwrite.
COPY /Y "C:\temp\file.txt" "C:\temp\file_copy.txt"
I was pulling my hair out to try and figure this problem out. I finally found my own solution and maybe it will help you too.
I noticed that the syntax used to select the entire directory could be used to select a single file.
ROBOCOPY "*" "Directory source" "Directory Output unc path or non"
The above code will copy everything from the directory source folder to the directory output path.
Let's say you only wanted to copy 1 file from the directory source named "test.txt"
In order to do that use the following code:
ROBOCOPY "*test.txt" "Directory source" "Directory Output unc path or non"
Thats about it. It works really well and will only copy the file name you want.
Alternatively you can use
ROBOCOPY "*.txt" "Directory source" "Directory Output unc path or non"
to copy out all text documents from the directory source.
Similarly this will also work with any .ext
.zip .exe .txt .pdf etc..
I signed up to answer this question with a better method. Let me know if I succeeded.

Robocopy copy contents of current folder

How would you translate this xcopy command into Robocopy:
xcopy *.* "C:\DestinationFolder\"
Keeping in mind that the current folder where the command is run changes dynamically (and hence the source folder is unknown in advance).
Thanks.
robocopy . "c:\dest"
Note you don't need to specify a wildcard in robocopy, by default it copies everything unless you use the /xf /xd flags to exclude certain files.
Robocopy DOES support wildcards.
You're expecting > robocopy SOURCE DEST but type > robocopy *.txt c:\folderdest\ to copy the current folder. If you look at the output from robocopy it will show "Files : *.txt" and "Source = c:\folderdest"
So in fact you can do > robocopy WILDCARD SOURCE DEST. If you want to use the CURRENT folder you need to use . (as has been mentioned here). So you would use > robocopy *.txt . c:\folderdest\.
Screenshot: http://i.stack.imgur.com/Xyxt4.png
As an addition:
If robocopy is started from an administrator console, the current folder "." will point to Windows\system32.
You can use the following commands at the top of your batch file to fix this:
#setlocal enableextensions
#cd /d "%~dp0"