Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1634
  • Last Modified:

Powershell script to move files from one folder location to another

Hi,

I need some help to write a powershell script to :

Monitor Temp folder (and all sub directories) for files that contain certain text string in the file name. File names =  "SiteName_Description_Date.*"
Create a folder in the destination directory that match the "SiteName" and move all files that match the criteria into the new folder, eg

c:\Temp\New Folder1 contains flies named Africa_xxx_Aug12.* and Asia_xxx_Aug12.*

Create a new directory on the destination drive (d:\Data) named Africa and move all Africa*.* files to the Africa folder and all the Asia*.* files to the Asia folder.

Thanks,

Dawie
0
flyblack
Asked:
flyblack
  • 3
  • 3
1 Solution
 
QlemoC++ DeveloperCommented:
$src = "c:\Temp\New Folder1"
$dst = "D:\Data"
gci $src -recurse -include "*_*_*.*" | % {
  $subfolder = @($_.Name -split "_")[0]
  md "$dst\$subfolder" -EA SilentlyContinue
  move-item $src.FullName "$dst\$subfolder\"
}

Open in new window

Just replace the two vars at the beginning with your folders.
0
 
AkulshCommented:
Qlemo,

Your comment is of course the solution.

As a 3rd party, I just want to know if there is any advantage to putting '@' in the line $subfolder = @($_.Name -split "_")[0] ?
0
 
QlemoC++ DeveloperCommented:
@() will build an array even if there is only one value. Example:
  @("Hohoho")[0]    #works as intended
  ("Hohoho")[0]   # result in the first character

Open in new window

It doesn't matter with -split, as that always returns an array of strings, but it is good style just to not rely on that ;-), and make the intention obvious. Imagine
    @(1)+2
    1+2

Open in new window

The former adds an array element, the latter a value. If you substitute the 1 with a var, you are at a point where you can no longer tell what operation is performed at runtime with $a+2, but with @($a)+2.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
flyblackAuthor Commented:
Hi Qlemo, thanks for the update. I will try it out and let you know. It's all new to me :-)
0
 
flyblackAuthor Commented:
I have executed the script as provided and get the following error:

The string starting:
At C:\scripts\file_transfer.ps1:6 char:43
+     move-item $src.FullName "$dst\$subfolder\ <<<< "
is missing the terminator: ".
At C:\scripts\file_transfer.ps1:7 char:2
+ } <<<<
    + CategoryInfo          : ParserError: (
}:String) [], ParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

Would you mind to comment on the syntax, commands and how you build the arguments. From reading the script I understand logic of it. Lastly, how would one script this in Cygwin?

Thanks
0
 
QlemoC++ DeveloperCommented:
I cannot say why you get that error (I cannot detect what's wrong), but found a different bug preventing execution:
$src = "c:\Temp\New Folder1"
$dst = "D:\Data"
gci $src -recurse -include "*_*_*.*" | % {
  $subfolder = @($_.Name -split "_")[0]
  md "$dst\$subfolder" -EA SilentlyContinue
  move-item $_.FullName "$dst\$subfolder\"
}

Open in new window

0
 
flyblackAuthor Commented:
Thanks. Seems like it is working now.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now