Open a folder in powershell

Hello Team,

I have a folder named degtf-ithuju\syntg-backup@GMT-2022.02.18-02.00.10. The last 6 characters of this folder changes daily. I need to navigate to it.

I created the string till degtf-ithuju\syntg-backup@GMT-2022.02.18. Now I need to use a wild card so that the path is reachable and I can get the content of degtf-ithuju\syntg-backup@GMT-2022.02.18-02.00.10 folder.

So I hardcoded degtf-ithuju\syntg-backup@GMT-2022.02.18 in a variable but now how do I add that remaining name to it? It cant be hard coded.

$YD = (get-date -format “yyyy.MM.dd”).ToString()
$v1= “C:\Ps\degtf-ithuju\syntg-backup@GMT-”
$v2= $v1 + $YD
$v2

I Tried * and % but its not working.

cd c:\Ps$v2% or cd C:\Ps$v*

Till $V2 is fine but after that its not working.

Thank you!

Vikas,
Welcome to the forum. :wave:t4:

When you post code or sample data or console output please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance

Since your date is in the format yyyy.MM.dd you could simply sort it by name and use the last found to find the folder wirh the latest date.

$Path = 'C:\Ps\degtf-ithuju'

Get-ChildItem -Path $Path -Directory |
    Sort-Object -Property Name |
        Select-Object -Last 1

If you want to go with the date time string you have several options … like this for example:

$DateTimeString = Get-Date -Format 'yyyy.MM.dd-*'
$DateTimeString = (Get-Date).ToString('yyyy.MM.dd-*')

And at the end you should enclose your path in quotes.

2 Likes

Hi, welcome to the forum :wave:

Firstly, when posting code in the forum, please can you use the preformatted text button </>. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working).

This should work fine:

$folderPrefix = 'C:\Ps\degtf-ithuju\syntg-backup@GMT-'
$day = (Get-Date -Format 'yyyy.MM.dd')
Set-Location $folderPrefix$day*

I tried but it said, Cannot find path ‘\C:.…@GMT-2022.02.19*’ because it does not exist.
which means * did not work. Because folder exists with name ending as 2022.02.19-02.00.07.

Please always share the complete code along with the complete error message you get - both formatted as code. Otherwise we had to guess what the issue may be.

<$p=“\scrdc-isiln\syndb-backup@GMT-2022.02.11-02.00.05”
$p

$DBI = “{0} GB” -f ((Get-ChildItem $p -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1024GB)
$DBI
/>

In the above code, since I have provided the variable $p with exact value of the path. Its working fine. It gives me the size of the folder.

But I cant provide the whole path always. Since the path is half way always same (upto \scrdc-isiln\syndb-backup@GMT-) so that can be stored in a variable (see below) and rest I have to create and append to it. I have extracted date as well and appended it too. It created the name till (\scrdc-isiln\syndb-backup@GMT-2022.02.19). Now I need to make sure that the remaining name (-02.00.05) of the folder gets appended to it by using some wild character or anything which could do this.
<
$YD = (get-date -format “yyyy.MM.dd”).ToString()
$v1= “\scrdc-isiln\syndb-backup@GMT-”
$v2= $v1 + $YD
$v2
/>
So, now I need help in completing the full name so that I can calculate the size of that folder as well.

I can’t help but notice that your UNC file path is starting with only one back slash ''
UNC paths should have two ‘\’

What happens when you examine the variable $v2? Does it appear to be correct? If you step through the script and run the next line of code that is using your $v2 variable, but substitute it with the string you got when examining the variable, does that work or fail?

@tom-doherty Yes, those are two (not sure how it showed one here). And yes $v2 works fine. It displays \scrdc-isiln\syndb-backup@GMT-2022.02.20. Which means it did get the value from $YD and appended it with $v1.
Now how do I navigate to the folder that has this part in its name? I need to make this path as \scrdc-isiln\syndb-backup@GMT-2022.02.19-02.00.07, which actually means anytime this folder was generated. Time can not be calculated here and hence I need to make use of wild characters to reach to this folder.

I am pasting ISE shell output here to let you know how it looks when I run it:-
< $YD = (get-date -format “yyyy.MM.dd”).ToString()
$YD
$v1= “\scrdc-isiln\syndb-backup@GMT-”
$v2= $v1 + $YD
$v2
2022.02.20
\scrdc-isiln\syndb-backup@GMT-2022.02.20
/>

It’s because you’re ignoring the requests to use the </> button and format your code properly.

To further complicate matters, your initial post used a local path
C:\Ps\degtf-ithuju\syntg-backup@GMT-
and in Windows PowerShell 5.1 local paths behave differently to UNC paths when the @ character is in the folder name.

Windows PowerShell 5.1 doesn’t seem to support using the ‘@’ character in UNC paths and I haven’t found a way to escape that character.

It works fine in PowerShell 7.2.1

$folderpath = '\\server01\data\_z_temp\syndb-backup@GMT-'
$day = Get-Date -format '2022.02.20'
Set-Location $folderpath$day*
PS Microsoft.PowerShell.Core\FileSystem::\\server01\data\_z_Temp\syndb-backup@GMT-2022.02.20-02.00.07>
3 Likes

Hmmmm … what is it actually what you’re trying to achieve here? Could you describe the bigger problem instead of the way you think you have to go to achieve it? There might be a better way.

@Olaf Thanks for asking. So, as you already know by now thatI am trying to reach to a folder named as \scrdc-isiln\syndb-backup@GMT-2022.02.19-02.00.07. But since only constant part in this name is “\scrdc-isiln\syndb-backup@GMT-”. Which I assigned as a string in a variable ($v1). The other part which gets generated (Formed with date and time) is the time when it gets created. So I just want to reach to that folder, so that finally I could get the total size of that folder and compare it with another folder. Since this is a back up, so I am comparing two folders and getting the size differences if there is any.
I know there are various other ways to compare two folders. But the only challenge for me here is to navigate to this folder before I could get the size of it. I am able to get the size of other folder with which I have to compare this (\scrdc-isiln\syndb-backup@GMT-2022.02.19-02.00.07) one. As the other folder name does not changes. I hope I made sense this time… :smiley:

OK, your answer raises new questions. :wink:

Are the folders you want to compare on the same server?
If yes is this server a Windows server?
How do you provide the date you want to compare?
Is this an automated or an interactive process.
Are there more than one folder per day?
Do you have control about the folder name used for the backups?
If not - would it be possible to rename the folders after creation?