Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if file exists, then move it

Tags:

powershell

I'm trying to write a few lines of code in powershell, to check if a file arrived to a specific folder. If the file is there, copy it to another folder. No action required if the file is not there. So far I have only the copying part:

cd C:\
Move /y "C:\myfolder\*.csv" "C:\MyDestinationFolder"

I can't find a simple code to check if the file is present.

like image 946
Demagus Miyan Avatar asked Sep 06 '25 08:09

Demagus Miyan


1 Answers

Maybe you can use this:

$SourceFile = "C:\source\file.txt"
$Destination = "C:\destination\"

if(Test-Path -Path $SourceFile)
{
    Copy-Item -Path $SourceFile -Destination $Destination
}
like image 138
Martin Hastrup Henriksen Avatar answered Sep 09 '25 06:09

Martin Hastrup Henriksen