Clicky

Hi Experts,

I am trying to copy multiple files matching a pattern in a shared folder under sub-folders and want to copy the folders with the related files to a windows machine.

This is what i tried

foreach ($i in get-childitem \\lodshare.ca.com\lod_reports -r -include usage*pool*.xls)
{
            Copy-Item $i.FullName C:\LODreports -recurse
   }

When i try this it does not create the sub-folders and keeps on overwriting files with the same name.

Searching multiple locations did. any help is much appreciated.

Fine if i can get a rsync

regards
Bhanu

asked 01/06/2011 04:20

bhanukir7's gravatar image

bhanukir7 ♦♦


16 Answers:
can you please more descriptive about your folder structure in source and destination
link
sumeshbnr's gravatar image

sumeshbnr

Do a CMD, tree /f so we can see folders
link
myhc's gravatar image

myhc

The files matching the patter  "usage*.xls" are located under located under multiple subfolders

\\foldershare\02-01-2010
\\  ""              \03-01-2010
\\  ""              \04-01-2010

when i try to run the get-childitem : this is the output

PS D:\Documents and Settings\kotna02> get-childitem \\lodshare.ca.com\lod_reports -r -include usage*pool*.xls


    Directory: \\lodshare.ca.com\lod_reports\2009_01_13


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         1/13/2010   5:05 PM    1224704 usageperpoolphysical.xls
-a---         1/13/2010   5:03 PM     183808 usageperpoolvm.xls


    Directory: \\lodshare.ca.com\lod_reports\2010_01_22


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         1/22/2010  11:57 AM    1224704 usageperpoolphysical.xls
-a---         1/22/2010  11:55 AM     182784 usageperpoolvm.xls


    Directory: \\lodshare.ca.com\lod_reports\2010_01_29


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         1/29/2010   3:00 PM    1224704 usageperpoolphysical.xls
-a---         1/29/2010   2:59 PM     183808 usageperpoolvm.xls


    Directory: \\lodshare.ca.com\lod_reports\2010_02_05


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          2/5/2010   1:30 PM      74240 usageperpoolphysical_costcenter.xls
-a---          2/9/2010   2:47 PM      71168 usageperpoolphysical_pool.xls
-a---          2/9/2010   3:16 PM     106496 usageperpoolvm_costcenter.xls
-a---          2/5/2010   1:32 PM      92672 usageperpoolvm_pool.xls


    Directory: \\lodshare.ca.com\lod_reports\2010_02_12


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         2/19/2010  11:06 AM     152576 usageperpoolphysical_costcenter.xls
-a---         2/12/2010   1:49 PM     117760 usageperpoolphysical_pool.xls
-a---         2/12/2010   1:36 PM     231424 usageperpoolvm_costcenter.xls
-a---         2/12/2010   1:30 PM     237056 usageperpoolvm_pool.xls

What i am trying to achieve is using "copy-item" cmdlt copy the files returned to local drive x:\foldername\
with the folder names

regards
bhanu

link
bhanukir7's gravatar image

bhanukir7

Let's make this a little different if you wouldn't mind:

$Files = gci "\\lodshare.ca.com\lod_reports" -r -include usage *pool*.xls
$Files | %{Copy-Item $_.FullName C:\LODreports}

HTH,

Dale Harris
link
DaleHarris's gravatar image

DaleHarris

Sorry, I made a typo.  I put a space between usage and *pool*.xls

That will need to be taken out.

DH
link
DaleHarris's gravatar image

DaleHarris

I guess you do not want ot have the complete path included in the target, only the relative path starting at \\lodshare.ca.com\lod_reports. Please clarify.

DaleHarris,
The asker requested to copy into subfolders, not into a single folder.
link
Qlemo's gravatar image

Qlemo

hi Qlemo,

yes....only relative path.  As there are subfolders on the network share for each week and there are multiple files that match the search string in each week...i wanted them to be organised by week..

DaleHarris,

I will try to copy files that way and see.

regards
bhanu
link
bhanukir7's gravatar image

bhanukir7

hi Qlemo/Haris

not sure if my posting was clear...trying to add some more clarity...

purpose is to copy the files with the folder structure as when i am running the query it is not creating subfolders and copying the files...which is resulting in overwriting the files...


Harris : it is able to query and insert the values into array and copy the files however it is not creating the subfolders and copying them

regards
bhanu
link
bhanukir7's gravatar image

bhanukir7

Bhanu,

I think I have a different approach we can try.

What if we copied everything over folders and all, and then ran a script against the exact copy and just removed files that didn't match our query.

This would be plausible if you don't have too much more to copy.  If it adds tons and tons of GBs, this may not be a suitable way to do it.

What do you think?

Dale Harris
link
DaleHarris's gravatar image

DaleHarris

1:
2:
3:
4:
5:
6:
7:
8:
$src = "\lodshare.ca.comlod_reports"
$dest= "C:LODreports"
$mask= "usage*pool*.xls"
get-childitem $src -r -include $mask | %{
  $dir = $dest+$_.Directory.FullName.Remove(0,$src.Length)
  md $dir -force
  $_.CopyTo($dir+""+$_.Name)
}

This code copies the files, replicating the subfolders in the destination.
If you want to overwrite existing files, replace line
1:
$_.CopyTo($dir+""+$_.Name)
by
1:
$_.CopyTo($dir+""+$_.Name, $True)

If you like that more, you can use new-item -type directory -path instead of md.
link
Qlemo's gravatar image

Qlemo

hi Harris,

I am getting a Unexpected token "md" in expression or statement error
 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
PS C:Documents and Settingskotna02> gci $src -r -include $mask | %{$dir = $des
t+$_.Directory.Fullname.Remove(0,$src.Length) md $dir -force $_.CopyTo ($dir+""
+$_.name)}
Unexpected token 'md' in expression or statement.
At line:1 char:91
+ gci $src -r -include $mask | %{$dir = $dest+$_.Directory.Fullname.Remove(0,$s
rc.Length) md <<<<  $dir -force $_.CopyTo ($dir+""+$_.name)}
    + CategoryInfo          : ParserError: (md:String) [], ParentContainsError
   RecordException
    + FullyQualifiedErrorId : UnexpectedToken
link
bhanukir7's gravatar image

bhanukir7

i did try new-item and the result is the same
link
bhanukir7's gravatar image

bhanukir7

Bhanu,

I don't want to confuse you, but Qlemo is actually giving you that script :)

I haven't made the other one that removes files that don't match your mask since I"m not sure if you wanted to go with the copy everything method due to size limitations.

Dale Harris
link
DaleHarris's gravatar image

DaleHarris

You need either to put that code snippet into a file with .ps1 extension, and then execute that file, or paste the contents into PS as-is. In both cases line breaks need to be maintained!
    dir \a md \a
is something completely different from
    dir \a
    md \a
.
link
Qlemo's gravatar image

Qlemo

bhanukir7, the script Qlemo provided will work great for what you are asking. If you want powershell use that script.

Just to give you another option take a look a robocopy, if you need to keep these directories insync robocopy might be easier. You can use the /MIR switch.

robocopy \\lodshare.ca.com\lod_reports C:\LODreports usage*pool*.xls /s

http://technet.microsoft.com/en-us/library/cc733145(WS.10).aspx
link
KenMcF's gravatar image

KenMcF

hi Qelmo,

the script works perfectly...

thanks a lot for the help

regards
bhanu
link
bhanukir7's gravatar image

bhanukir7

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×1

Asked: 01/06/2011 04:20

Seen: 1200 times

Last updated: 01/16/2011 06:27