Multi-file creation symbolic link (daily) without the date at the beginning of f

Hello

Being a beginner Being a beginner, I try to find a solution 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?

$File = Get-Childitem “$Source*.xls” -Recurse | where { $_.CreationTime -gt (Get-Date).AddHours(-2.5)}
$Source = "\share\report"
$Destination = "\Share\Report\Dayly"
$DailyLog = $Files
$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

I can’t see how this script works at all. You potentially collect multiple files in the $file variable. Later you try to populate $DailyLog with a nonexistent variable $files (at least it doesn’t exist in what you’ve shown us.) Then the value of the symbolic link would be this empty $dailylog. You specify the -recurse option when gathering the .xls files so the path could be just $source or children. The destination is a single folder so you could potentially be putting all the reports into a single folder. This may be exactly what you’re after, but the $file vs $files and multiple source files (potentially, and likely based on your question) treated as a single are issues that you need to clarify for us.

Hello, Thank you for your message

Sorry, maybe I didn’t explain myself well

Every day I receive logs but named differently and which always starts with the date and time (unfortunately I cannot parameter the logs differently because the naming of the source files cannot be changed in the system), example:

The source of the \ share \ report directory
2021_02_25_10_10_10_FilereportProduction.xls
2021_02_25_10_10_10_FilereportSharing.xsl
2021_02_25_10_10_10_FilereportEncryped.xls
2021_02_25_10_10_10_Filereport.xls
etc …

So that an application can read these daily files I opted for a symlink creation solution updated every day with the source files of the day and always with the same name but without the date and time, I will want these symlink files are always named this way.

Destination of the \ share \ report \ Dayly directory
FilereportProduction.xls “.symlink”
FilereportSharing.xsl “.symlink”
FilereportEncryped.xls “.symlink”
Filereport.xls “.symlink”
etc …

Unfortunately I am new to powershell, so my knowledge is quite basic. But I would like a script which is not based on fixed file names in a script but which will automatically take all the files that have arrived the day and create the symlinks at the destination keeping only the name which is after the date and hours. thank you in advance for your help

Best regards Moan

The file names are not a concern. What is a concern is if the files are all in the root of source or if they can be in subfolders. If they are all located right in source, great we just remove -recurse from the Get-ChildItem. If they are in sub folders then we need to account for the varying paths by using the full name property.

This should be close

$Source = "\share\report"

$Filelist = Get-Childitem "$Source*.xls" -Recurse | Where-Object CreationTime -gt (Get-Date).AddHours(-2.5)

$Destination = "\Share\Report\Daily"

foreach($file in $filelist)
{
    $current = ($file.name -split '_')[-1]

    write-host Current report file: $current

    $targetfile = Join-Path $Destination $current

    if (Test-Path $targetfile -ErrorAction SilentlyContinue)
    {
        Remove-Item $targetfile -Confirm:$false -Force
        Start-Sleep -Seconds 3
    }

    New-item -ItemType SymbolicLink -Path $Destination -Name $current -Value $file.FullName
}

Get-ChildItem -Path $Destination | Where-Object {$_.LinkType} | Select-Object FullName, LinkType, Target

Also note you are pulling a new Get-Date for each file so you may consider storing that in a variable instead.

Hi
Thank you so much so much for your support, the script working very well,

Just a last question about the script, if the file is named FilereportProduction_2021_02_25_10_10_10.xls the $current = ($file.name -split ‘_’)[-1] is not available so what I have to change?

Thank you again.

Best regards.

What do you mean it’s not available? It would still work and you’d end up with 10.xls in that example.

Hello

Thank you so much for your help,

The script working very well with file named “2021_02_25_10_10_10_FilereportProduction.xls”, the symlinks created is : “FilereportProduction.xls”

But if the file is named like that “FilereportProduction_2021_02_25_10_10_10.xls” yes the symlinks is named “10.xls”

Best regards

OK so when you split the file up by _ you end up with however many pieces. The last piece could be the 5th or the 10th or the 1000th so a convenient way to grab the last element is us -1 as we did before. To grab the first like in your new example, you can grab the 0 element. But then you’ll need to add your file extension back.

… and to make the confusion complete … of course there would be ways to pick the part of the filename not containing numbers regardless of its position.

('FilereportProduction_2021_02_25_10_10_10.xls' -split '\.')[0] -split '_' | Where-Object { $_ -match '[a-z]' }
('2021_02_25_10_10_10_FilereportProduction.xls' -split '\.')[0] -split '_' | Where-Object { $_ -match '[a-z]' }
('2021_02_25_FilereportProduction_10_10_10.xls' -split '\.')[0] -split '_' | Where-Object { $_ -match '[a-z]' }

all three lines of code result in the output FilereportProduction. :wink:

1 Like

Well in that case I’d just do

'FilereportProduction_2021_02_25_10_10_10.xls' -replace '_|\d'


:wink:

1 Like