Home > database >  Powershell: Search in path, rename file and copy it
Powershell: Search in path, rename file and copy it

Time:12-15

I am trying to build a script that can do the following When I open the powershell script

  1. it should ask me for a path where the files are located e.g. C:\SearchPath\DateOfToday Daily there will be a new folder e.g. 20221214. Maybe it is possible to specify the path and the script will get today's date by itself or maybe just opening a windows explorer so I can select the SourceFolder.

  2. A file must be copied e.g. Test.xmr and then renamed to Test.$xmr

  3. some more files should be collected like jpl, eml, html, pdf, cml, xmr, $xmr and xml

  4. the collected files should be moved to the folder C:\AnotherFolder\

What I have so far is this

$SourceFolder = "\\Path1\Dateoftoday"
$DestFolder = '\\Path2' #Path to Destination Folder
[array]$FileList = gci -Path $SourceFolder -Recurse -File -Filter "ABC.xmr"
[array]$FileList = gci -Path $SourceFolder -Recurse -File -Filter "ABC.jpl"
[array]$FileList = gci -Path $SourceFolder -Recurse -File -Filter "ABC.eml"
[array]$FileList = gci -Path $SourceFolder -Recurse -File -Filter "ABC.html"
[array]$FileList = gci -Path $SourceFolder -Recurse -File -Filter "ABC.pdf"
[array]$FileList = gci -Path $SourceFolder -Recurse -File -Filter "ABC.cml"
[array]$FileList = gci -Path $SourceFolder -Recurse -File -Filter "ABC.xml"
ForEach ($F in $FileList) {
    Copy-Item $F.FullName (Join-Path $DestFolder ($F.Name))
}

I can specify the path and the filenames and then run it. Sorry if it's basic, but I'm new at powershell.

CodePudding user response:

Good day!!

Maybe we can help you with this: First of all you need to ask for the path and we have two ways to do this (maybe more) but those are the ways I know

The first option is the simple, using the Read-Host Cmdlet:

$path = Read-Host "Please type the path of the folder"

The second option is more complicated and use dotnet framework: (to ask for the destination just change the last two variables with destination instead of sourcePath)

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$box = New-Object System.Windows.Forms.Form
$box.Text = 'Script Execution'
$box.Size = New-Object System.Drawing.Size(300,200)
$box.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$box.AcceptButton = $okButton
$box.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,120)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$box.CancelButton = $cancelButton
$box.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please type the path of the files:'
$box.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$box.Controls.Add($textBox)

$box.Topmost = $true

$box.Add_Shown({$textBox.Select()})
$result = $box.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $sourcePath = $textBox.Text
    $sourcePath
}

Once we have the path we can copy the files to do it we can use Copy-item (I'm not sure if you want to rename the original files or the final files, anyway if you want to rename the last files just use rename-item cmdlet)

$fileList = @(
    Get-ChildItem -Path $sourcePath -Recurse -include "*.jpl", "*.eml", "*.html", "*.pdf", "*.cml", "*.xml"
)

foreach ($f in $fileList){
   Copy-Item -Path $f.fullname -Destination "$destPath\$f.name"
}

And this should be enough, i'm not really sure about the $fileList part maybe I made some typo, sorry in this case.

Have a nice day!

CodePudding user response:

Looks like you just need this:

$today        = '{0:yyyyMMdd}' -f (Get-Date)
$SourceFolder = "\\Path1\$today"
$DestFolder   = '\\Path2' #Path to Destination Folder
$filePattern  = '*.xmr', '*.jpl', '*.eml', '*.html', '*.pdf', '*.cml', '*.xml', '*.$xmr'

# unclear about your question 2)..
# do we need to copy a file to the $SourceFolder first ??
# Copy-Item -Path 'X:\Somewhere\Test.xmr' -Destination (Join-Path -Path $SourceFolder -ChildPath 'Test.$xmr')

# copy all chosen files to the destination folder
Get-ChildItem -LiteralPath $SourceFolder -Recurse -File -Include $filePattern | ForEach-Object {
    $_ | Copy-Item -Destination $DestFolder
}
  • Related