Robocopy function copying unnecessary folders when using file parameter











up vote
0
down vote

favorite












I am trying to figure out why my function is pulling folders within a said directory when I am only specifying a specific file to be transferred.



The folders get copied but just the folders. The files within the folders do not get transferred.



I've tried figuring this out, but just cannot quite grasp why this issue is happening. Thanks!



function robocopy-job {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $Source,

[Parameter(Mandatory = $true)]
[string] $Destination,

[Parameter(Mandatory = $false)]
[string] $File
)

$robocopycmd = "robocopy ""$source"" ""$destination"" ""$File"" /mir /bytes"
$Staging = Invoke-Expression "$robocopycmd /l"
$totalnewfiles = $Staging -match 'new file'
$totalmodified = $Staging -match 'newer'
$totalfiles = $totalnewfiles + $totalmodified
$TotalBytesarray = @()
foreach ($file in $totalfiles) {
if ($file.substring(13, 13).trim().length -eq 9) {$TotalBytesarray += $file.substring(13, 15).trim() }
else {$TotalBytesarray += $file.substring(13, 13).trim()}
}
$totalbytes = (($TotalBytesarray | Measure-Object -Sum).sum)

$robocopyjob = Start-Job -Name robocopy -ScriptBlock {param ($command) ; Invoke-Expression -Command $command} -ArgumentList $robocopycmd

while ($robocopyjob.State -eq 'running') {
$progress = Receive-Job -Job $robocopyjob -Keep -ErrorAction SilentlyContinue
if ($progress) {
$copiedfiles = ($progress | Select-String -SimpleMatch 'new file', 'newer')
if ($copiedfiles.count -le 0) { $TotalFilesCopied = $copiedfiles.Count }
else { $TotalFilesCopied = $copiedfiles.Count - 1 }
$FilesRemaining = ($totalfiles.count - $TotalFilesCopied)
$Bytesarray = @()
foreach ($Newfile in $copiedfiles) {
if ($Newfile.tostring().substring(13, 13).trim().length -eq 9) { $Bytesarray += $Newfile.tostring().substring(13, 15).trim() }
else { $Bytesarray += $Newfile.tostring().substring(13, 13).trim() }
}
$bytescopied = ([int64]$Bytesarray[-1] * ($Filepercentcomplete / 100))
$totalfilebytes = [int64]$Bytesarray[-1]
$TotalBytesCopied = ((($Bytesarray | Measure-Object -Sum).sum) - $totalfilebytes) + $bytescopied
$TotalBytesRemaining = ($totalbytes - $totalBytesCopied)
if ($copiedfiles) {
if ($copiedfiles[-1].tostring().substring(13, 13).trim().length -eq 9) { $currentfile = $copiedfiles[-1].tostring().substring(28).trim() }
else { $currentfile = $copiedfiles[-1].tostring().substring(25).trim() }
}
$totalfilescount = $totalfiles.count
if ($progress[-1] -match '%') { $Filepercentcomplete = $progress[-1].substring(0, 3).trim() }
else { $Filepercentcomplete = 0 }
$totalPercentcomplete = (($TotalBytesCopied / $totalbytes) * 100)
if ($totalbytes -gt 2gb) { $BytesCopiedprogress = "{0:N2}" -f ($totalBytesCopied / 1gb); $totalbytesprogress = "{0:N2}" -f ($totalbytes / 1gb); $bytes = 'Gbytes' }
else { $BytesCopiedprogress = "{0:N2}" -f ($totalBytesCopied / 1mb); $totalbytesprogress = "{0:N2}" -f ($totalbytes / 1mb); $bytes = 'Mbytes' }
if ($totalfilebytes -gt 1gb) { $totalfilebytes = "{0:N2}" -f ($totalfilebytes / 1gb); $bytescopied = "{0:N2}" -f ($bytescopied / 1gb); $filebytes = 'Gbytes' }
else { $totalfilebytes = "{0:N2}" -f ($totalfilebytes / 1mb); $bytescopied = "{0:N2}" -f ($bytescopied / 1mb); $filebytes = 'Mbytes' }

Write-Progress -Id 1 -Activity "Copying files from $source to $destination, $totalfilescopied of $totalfilescount files copied" -Status "$bytescopiedprogress of $totalbytesprogress $bytes copied" -PercentComplete $totalPercentcomplete
Write-Progress -Id 2 -Activity "$currentfile" -status "$bytescopied of $totalfilebytes $filebytes" -PercentComplete $Filepercentcomplete
}
}
}









share|improve this question
























  • By the way, this only happens when trying to copy a specific file, not an entire directory.
    – rad_
    Nov 20 at 16:23















up vote
0
down vote

favorite












I am trying to figure out why my function is pulling folders within a said directory when I am only specifying a specific file to be transferred.



The folders get copied but just the folders. The files within the folders do not get transferred.



I've tried figuring this out, but just cannot quite grasp why this issue is happening. Thanks!



function robocopy-job {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $Source,

[Parameter(Mandatory = $true)]
[string] $Destination,

[Parameter(Mandatory = $false)]
[string] $File
)

$robocopycmd = "robocopy ""$source"" ""$destination"" ""$File"" /mir /bytes"
$Staging = Invoke-Expression "$robocopycmd /l"
$totalnewfiles = $Staging -match 'new file'
$totalmodified = $Staging -match 'newer'
$totalfiles = $totalnewfiles + $totalmodified
$TotalBytesarray = @()
foreach ($file in $totalfiles) {
if ($file.substring(13, 13).trim().length -eq 9) {$TotalBytesarray += $file.substring(13, 15).trim() }
else {$TotalBytesarray += $file.substring(13, 13).trim()}
}
$totalbytes = (($TotalBytesarray | Measure-Object -Sum).sum)

$robocopyjob = Start-Job -Name robocopy -ScriptBlock {param ($command) ; Invoke-Expression -Command $command} -ArgumentList $robocopycmd

while ($robocopyjob.State -eq 'running') {
$progress = Receive-Job -Job $robocopyjob -Keep -ErrorAction SilentlyContinue
if ($progress) {
$copiedfiles = ($progress | Select-String -SimpleMatch 'new file', 'newer')
if ($copiedfiles.count -le 0) { $TotalFilesCopied = $copiedfiles.Count }
else { $TotalFilesCopied = $copiedfiles.Count - 1 }
$FilesRemaining = ($totalfiles.count - $TotalFilesCopied)
$Bytesarray = @()
foreach ($Newfile in $copiedfiles) {
if ($Newfile.tostring().substring(13, 13).trim().length -eq 9) { $Bytesarray += $Newfile.tostring().substring(13, 15).trim() }
else { $Bytesarray += $Newfile.tostring().substring(13, 13).trim() }
}
$bytescopied = ([int64]$Bytesarray[-1] * ($Filepercentcomplete / 100))
$totalfilebytes = [int64]$Bytesarray[-1]
$TotalBytesCopied = ((($Bytesarray | Measure-Object -Sum).sum) - $totalfilebytes) + $bytescopied
$TotalBytesRemaining = ($totalbytes - $totalBytesCopied)
if ($copiedfiles) {
if ($copiedfiles[-1].tostring().substring(13, 13).trim().length -eq 9) { $currentfile = $copiedfiles[-1].tostring().substring(28).trim() }
else { $currentfile = $copiedfiles[-1].tostring().substring(25).trim() }
}
$totalfilescount = $totalfiles.count
if ($progress[-1] -match '%') { $Filepercentcomplete = $progress[-1].substring(0, 3).trim() }
else { $Filepercentcomplete = 0 }
$totalPercentcomplete = (($TotalBytesCopied / $totalbytes) * 100)
if ($totalbytes -gt 2gb) { $BytesCopiedprogress = "{0:N2}" -f ($totalBytesCopied / 1gb); $totalbytesprogress = "{0:N2}" -f ($totalbytes / 1gb); $bytes = 'Gbytes' }
else { $BytesCopiedprogress = "{0:N2}" -f ($totalBytesCopied / 1mb); $totalbytesprogress = "{0:N2}" -f ($totalbytes / 1mb); $bytes = 'Mbytes' }
if ($totalfilebytes -gt 1gb) { $totalfilebytes = "{0:N2}" -f ($totalfilebytes / 1gb); $bytescopied = "{0:N2}" -f ($bytescopied / 1gb); $filebytes = 'Gbytes' }
else { $totalfilebytes = "{0:N2}" -f ($totalfilebytes / 1mb); $bytescopied = "{0:N2}" -f ($bytescopied / 1mb); $filebytes = 'Mbytes' }

Write-Progress -Id 1 -Activity "Copying files from $source to $destination, $totalfilescopied of $totalfilescount files copied" -Status "$bytescopiedprogress of $totalbytesprogress $bytes copied" -PercentComplete $totalPercentcomplete
Write-Progress -Id 2 -Activity "$currentfile" -status "$bytescopied of $totalfilebytes $filebytes" -PercentComplete $Filepercentcomplete
}
}
}









share|improve this question
























  • By the way, this only happens when trying to copy a specific file, not an entire directory.
    – rad_
    Nov 20 at 16:23













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to figure out why my function is pulling folders within a said directory when I am only specifying a specific file to be transferred.



The folders get copied but just the folders. The files within the folders do not get transferred.



I've tried figuring this out, but just cannot quite grasp why this issue is happening. Thanks!



function robocopy-job {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $Source,

[Parameter(Mandatory = $true)]
[string] $Destination,

[Parameter(Mandatory = $false)]
[string] $File
)

$robocopycmd = "robocopy ""$source"" ""$destination"" ""$File"" /mir /bytes"
$Staging = Invoke-Expression "$robocopycmd /l"
$totalnewfiles = $Staging -match 'new file'
$totalmodified = $Staging -match 'newer'
$totalfiles = $totalnewfiles + $totalmodified
$TotalBytesarray = @()
foreach ($file in $totalfiles) {
if ($file.substring(13, 13).trim().length -eq 9) {$TotalBytesarray += $file.substring(13, 15).trim() }
else {$TotalBytesarray += $file.substring(13, 13).trim()}
}
$totalbytes = (($TotalBytesarray | Measure-Object -Sum).sum)

$robocopyjob = Start-Job -Name robocopy -ScriptBlock {param ($command) ; Invoke-Expression -Command $command} -ArgumentList $robocopycmd

while ($robocopyjob.State -eq 'running') {
$progress = Receive-Job -Job $robocopyjob -Keep -ErrorAction SilentlyContinue
if ($progress) {
$copiedfiles = ($progress | Select-String -SimpleMatch 'new file', 'newer')
if ($copiedfiles.count -le 0) { $TotalFilesCopied = $copiedfiles.Count }
else { $TotalFilesCopied = $copiedfiles.Count - 1 }
$FilesRemaining = ($totalfiles.count - $TotalFilesCopied)
$Bytesarray = @()
foreach ($Newfile in $copiedfiles) {
if ($Newfile.tostring().substring(13, 13).trim().length -eq 9) { $Bytesarray += $Newfile.tostring().substring(13, 15).trim() }
else { $Bytesarray += $Newfile.tostring().substring(13, 13).trim() }
}
$bytescopied = ([int64]$Bytesarray[-1] * ($Filepercentcomplete / 100))
$totalfilebytes = [int64]$Bytesarray[-1]
$TotalBytesCopied = ((($Bytesarray | Measure-Object -Sum).sum) - $totalfilebytes) + $bytescopied
$TotalBytesRemaining = ($totalbytes - $totalBytesCopied)
if ($copiedfiles) {
if ($copiedfiles[-1].tostring().substring(13, 13).trim().length -eq 9) { $currentfile = $copiedfiles[-1].tostring().substring(28).trim() }
else { $currentfile = $copiedfiles[-1].tostring().substring(25).trim() }
}
$totalfilescount = $totalfiles.count
if ($progress[-1] -match '%') { $Filepercentcomplete = $progress[-1].substring(0, 3).trim() }
else { $Filepercentcomplete = 0 }
$totalPercentcomplete = (($TotalBytesCopied / $totalbytes) * 100)
if ($totalbytes -gt 2gb) { $BytesCopiedprogress = "{0:N2}" -f ($totalBytesCopied / 1gb); $totalbytesprogress = "{0:N2}" -f ($totalbytes / 1gb); $bytes = 'Gbytes' }
else { $BytesCopiedprogress = "{0:N2}" -f ($totalBytesCopied / 1mb); $totalbytesprogress = "{0:N2}" -f ($totalbytes / 1mb); $bytes = 'Mbytes' }
if ($totalfilebytes -gt 1gb) { $totalfilebytes = "{0:N2}" -f ($totalfilebytes / 1gb); $bytescopied = "{0:N2}" -f ($bytescopied / 1gb); $filebytes = 'Gbytes' }
else { $totalfilebytes = "{0:N2}" -f ($totalfilebytes / 1mb); $bytescopied = "{0:N2}" -f ($bytescopied / 1mb); $filebytes = 'Mbytes' }

Write-Progress -Id 1 -Activity "Copying files from $source to $destination, $totalfilescopied of $totalfilescount files copied" -Status "$bytescopiedprogress of $totalbytesprogress $bytes copied" -PercentComplete $totalPercentcomplete
Write-Progress -Id 2 -Activity "$currentfile" -status "$bytescopied of $totalfilebytes $filebytes" -PercentComplete $Filepercentcomplete
}
}
}









share|improve this question















I am trying to figure out why my function is pulling folders within a said directory when I am only specifying a specific file to be transferred.



The folders get copied but just the folders. The files within the folders do not get transferred.



I've tried figuring this out, but just cannot quite grasp why this issue is happening. Thanks!



function robocopy-job {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $Source,

[Parameter(Mandatory = $true)]
[string] $Destination,

[Parameter(Mandatory = $false)]
[string] $File
)

$robocopycmd = "robocopy ""$source"" ""$destination"" ""$File"" /mir /bytes"
$Staging = Invoke-Expression "$robocopycmd /l"
$totalnewfiles = $Staging -match 'new file'
$totalmodified = $Staging -match 'newer'
$totalfiles = $totalnewfiles + $totalmodified
$TotalBytesarray = @()
foreach ($file in $totalfiles) {
if ($file.substring(13, 13).trim().length -eq 9) {$TotalBytesarray += $file.substring(13, 15).trim() }
else {$TotalBytesarray += $file.substring(13, 13).trim()}
}
$totalbytes = (($TotalBytesarray | Measure-Object -Sum).sum)

$robocopyjob = Start-Job -Name robocopy -ScriptBlock {param ($command) ; Invoke-Expression -Command $command} -ArgumentList $robocopycmd

while ($robocopyjob.State -eq 'running') {
$progress = Receive-Job -Job $robocopyjob -Keep -ErrorAction SilentlyContinue
if ($progress) {
$copiedfiles = ($progress | Select-String -SimpleMatch 'new file', 'newer')
if ($copiedfiles.count -le 0) { $TotalFilesCopied = $copiedfiles.Count }
else { $TotalFilesCopied = $copiedfiles.Count - 1 }
$FilesRemaining = ($totalfiles.count - $TotalFilesCopied)
$Bytesarray = @()
foreach ($Newfile in $copiedfiles) {
if ($Newfile.tostring().substring(13, 13).trim().length -eq 9) { $Bytesarray += $Newfile.tostring().substring(13, 15).trim() }
else { $Bytesarray += $Newfile.tostring().substring(13, 13).trim() }
}
$bytescopied = ([int64]$Bytesarray[-1] * ($Filepercentcomplete / 100))
$totalfilebytes = [int64]$Bytesarray[-1]
$TotalBytesCopied = ((($Bytesarray | Measure-Object -Sum).sum) - $totalfilebytes) + $bytescopied
$TotalBytesRemaining = ($totalbytes - $totalBytesCopied)
if ($copiedfiles) {
if ($copiedfiles[-1].tostring().substring(13, 13).trim().length -eq 9) { $currentfile = $copiedfiles[-1].tostring().substring(28).trim() }
else { $currentfile = $copiedfiles[-1].tostring().substring(25).trim() }
}
$totalfilescount = $totalfiles.count
if ($progress[-1] -match '%') { $Filepercentcomplete = $progress[-1].substring(0, 3).trim() }
else { $Filepercentcomplete = 0 }
$totalPercentcomplete = (($TotalBytesCopied / $totalbytes) * 100)
if ($totalbytes -gt 2gb) { $BytesCopiedprogress = "{0:N2}" -f ($totalBytesCopied / 1gb); $totalbytesprogress = "{0:N2}" -f ($totalbytes / 1gb); $bytes = 'Gbytes' }
else { $BytesCopiedprogress = "{0:N2}" -f ($totalBytesCopied / 1mb); $totalbytesprogress = "{0:N2}" -f ($totalbytes / 1mb); $bytes = 'Mbytes' }
if ($totalfilebytes -gt 1gb) { $totalfilebytes = "{0:N2}" -f ($totalfilebytes / 1gb); $bytescopied = "{0:N2}" -f ($bytescopied / 1gb); $filebytes = 'Gbytes' }
else { $totalfilebytes = "{0:N2}" -f ($totalfilebytes / 1mb); $bytescopied = "{0:N2}" -f ($bytescopied / 1mb); $filebytes = 'Mbytes' }

Write-Progress -Id 1 -Activity "Copying files from $source to $destination, $totalfilescopied of $totalfilescount files copied" -Status "$bytescopiedprogress of $totalbytesprogress $bytes copied" -PercentComplete $totalPercentcomplete
Write-Progress -Id 2 -Activity "$currentfile" -status "$bytescopied of $totalfilebytes $filebytes" -PercentComplete $Filepercentcomplete
}
}
}






powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 17:16









James C.

8,26722030




8,26722030










asked Nov 20 at 15:52









rad_

51




51












  • By the way, this only happens when trying to copy a specific file, not an entire directory.
    – rad_
    Nov 20 at 16:23


















  • By the way, this only happens when trying to copy a specific file, not an entire directory.
    – rad_
    Nov 20 at 16:23
















By the way, this only happens when trying to copy a specific file, not an entire directory.
– rad_
Nov 20 at 16:23




By the way, this only happens when trying to copy a specific file, not an entire directory.
– rad_
Nov 20 at 16:23

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396766%2frobocopy-function-copying-unnecessary-folders-when-using-file-parameter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396766%2frobocopy-function-copying-unnecessary-folders-when-using-file-parameter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python