Forum Discussion

MoanCH's avatar
MoanCH
Copper Contributor
Feb 17, 2021

Creation symbolic link daily files

Hello

For application needs I create a daily symbolic link with the same naming convention.

1) Deletion of the last symbolic link type file at the destination
Creation of a symbolic type file at the destination by referring to the file that was created the same day at the source.

In this example the source file starts with the date and time followed by the file name "yyyy_MM_dd_HH_mm_ss_filereport.xls (2021_02_16_10_10_10_filereport.xls).

When the symbolic link is created, it is created without the start date (filereport.xls ".Symlink")

The script below works correctly.

However, I can't do the same thing, when I have several daily files with different names but always starting with the date and time. Anyone have an idea how I can do the same on all the differently named log files?

 

$Fichiers = Get-Childitem "$Source*.xls" -Recurse | where { $_.CreationTime -gt (Get-Date).AddHours(-2.5)}
$Source = "\\share\report\"
$Destination = "\\Share\Report\Dayly\"
$DailyLog = $Fichiers
$Localhost = $Destination + "filereport.xls"

write-host $Source
write-host $DailyLog

pushd $Destination

if (Test-Path $DailyLog -ErrorAction SilentlyContinue) {

if (Test-Path $Localhost -ErrorAction SilentlyContinue) {
remove-item $Localhost -Confirm:$false -Force
Start-Sleep -Seconds 3
}

New-item -ItemType SymbolicLink -Path $Destination -Name ".\filereport.xls" -Value $DailyLog
}

gci | ? { $_.LinkType } | Select FullName, LinkType, Target
popd

 

 

  • ZahidZ910's avatar
    ZahidZ910
    Copper Contributor
    To modify your script so that it can handle multiple daily files with different names, all starting with the date and time, you can use a loop to process each file individually. Below is an updated version of your script that iterates over each file found and creates a symbolic link for each one:

    # Define the source and destination paths
    $Source = "\\share\report\"
    $Destination = "\\Share\Report\Dayly\"

    # Get all .xls files created within the last 2.5 hours
    $Fichiers = Get-ChildItem "$Source*.xls" -Recurse | Where-Object { $_.CreationTime -gt (Get-Date).AddHours(-2.5)}

    # Loop through each file in the source directory
    foreach ($File in $Fichiers) {
    # Extract the original file name (without the date and time)
    $FileName = $File.Name -replace '^\d{4}_\d{2}_\d{2}_\d{2}_\d{2}_\d{2}_', ''
    $Localhost = Join-Path $Destination $FileName

    # Check if the symbolic link already exists and remove it if necessary
    if (Test-Path $Localhost -ErrorAction SilentlyContinue) {
    Remove-Item $Localhost -Confirm:$false -Force
    Start-Sleep -Seconds 3
    }

    # Create the new symbolic link
    New-Item -ItemType SymbolicLink -Path $Destination -Name $FileName -Value $File.FullName
    }

    # Display the created symbolic links
    gci $Destination | Where-Object { $_.LinkType } | Select-Object FullName, LinkType, Target




    Explanation:
    Loop through files: The script loops through each file in the $Fichiers collection, which includes all .xls files created in the last 2.5 hours.

    Extract the original file name: For each file, it removes the date and time prefix from the file name using the -replace operator with a regular expression.

    Create symbolic link: It creates a symbolic link for each file in the destination directory, using the filename without the date and time prefix.

    Handle existing links: Before creating a new symbolic link, the script checks if one already exists at the destination and removes it if necessary.

    This approach will allow you to create symbolic links for multiple files, regardless of their specific names, as long as they start with a date and time prefix.

Resources