Home > Blockchain >  PowerShell Script to copy files from one directoy to another
PowerShell Script to copy files from one directoy to another

Time:04-28

So I created a script to copy files daily from a directory and push it to another on our file server. So he store the files on the source as year\month\day i.e 2022\04\26\file.txt. I get the script to go into the latest day based on the variables provide and copy the file from the current day and push it to the destination. The only thing I can't get to work is copying the file, I get the error that the path don't exist although it shows up correctly. But if I add the path without using the variables it works as expect.

#Get Dates for Folder Creation 
$Timex = Get-Date -Format MM-dd-yyyy_HHmm
$Timex1 = Get-Date -Format yyyyMMdd
$Year = Get-Date -Format yyyy
$Month = Get-Date -Format MM
$Day = Get-Date -Format dd

#Source files that will need to be moved
$APLogs = "C:\Logs\Stack3\$Year\$Month\$Day\Folder\_AP.LOG"
$ARLogs = "C:\Logs\Stack3\$Year\$Month\$Day\Folder\AR.LOG"
$NSLogs = "C:\Logs\Stack3\$Year\$Month\$Day\Folder\_NS.LOG"
$CSLogs = "C:\Logs\Stack3\$Year\$Month\$Day\Folder\_CS.LOG"

#Set the directory for the day
$TodaysDir = "\\FileServer\LOGS\LOG_DATA\$Timex1\Stack3"

# Test to see if folder [$TestFolder]  exists"
if (Test-Path -Path $TodaysDir) {
    "Path exists!"
} else {
    # Create Directory
    New-Item $TodaysDir -ItemType Directory

 }

   # Copy Log File 
   Copy-Item $APLogs -Destination $TodaysDir
   Copy-Item $ARLogs -Destination $TodaysDir
   Copy-Item $NSLogs -Destination $TodaysDir
   Copy-Item $CSLogs -Destination $TodaysDir

Error message = Copy-Item : Cannot find path 'C:\Logs\Stack3\2022\04\26\Folder\_AP.LOG' because it does not exist.

CodePudding user response:

OPs code works just fine on my system when I tested it (which is why I removed my previous answer).

The code below is a cleaner way of implementing it.

Note: In the script (same as the original) there is an unused variable '$Timex'. Ensure that is not needed somewhere in the file path.

EDIT: After reviewing the error provided in the question, the folder path is exactly as expected. The problem is that the provided path is the wrong location. As there is an unused variable, made an assumption that it should be used instead of 'Folder' in $path.

$Date = Get-Date

$Timex = '{0:MM-dd-yyyy_HHmm}' -f $Date
$Timex1 = '{0:yyyyMMdd}' -f $Date
$Year = '{0:yyyy}' -f $Date
$Month = '{0:MM}' -f $Date
$Day = '{0:dd}' -f $Date


$TodaysDir = "\\FileServer\LOGS\LOG_DATA\{0}\Stack3" -f $Timex1
$path = "C:\Logs\Stack3\{0}\{1}\{2}\{3}\" -f $Year, $Month, $Day, $Timex

# If path 'doesn't' exist, it creates it
if (!(Test-Path -Path $TodaysDir)) 
{
    New-Item $TodaysDir -ItemType Directory -Force
}

$Logs = @("_AP.LOG","AR.LOG","_NS.LOG","_CS.LOG")

foreach($x in $Logs)
{
    $log = "{0}\{1}" -f $path, $x
    if(Test-Path -Path $log)
    {
        Copy-Item $log -Destination $TodaysDir
    }
    else
    {
        Write-Host "Could Not Locate: $log"
        $Error[0]
    }
}
  • Related