0

I have been writing a powershell script to solve a problem, the utility of the script may baffle some but I have a use in mind.

The aim of the script is to create a new directory in the temp folder that mirrors the name of the folder in the parent folder but with the script as it is the newly created folder names in the temp folder have the following text around them @{Name=FOLDERNAME} how can I edit the script just to get FOLDERNAME?

$dirs = @(Get-ChildItem -Path C:\Users\LTGoldman\Desktop\keys -Recurse -Directory -Force -ErrorAction SilentlyContinue | Select-Object Name)
for($i=0; $i -lt $dirs.length;$i++)
    {
        New-Item -Path "C:\Users\LTGoldman\Desktop\keys\temp" -Name $dirs[$i] -ItemType "directory"
        Move-Item -Path .\*.tar.gz -Destination C:\Users\LTGoldman\Desktop\keys\temp\$dirs[$i]
    }

Changed to:

$dirs = @(Get-ChildItem -Path C:\Users\LTGoldman\Desktop\keys -Recurse -Directory -Force -ErrorAction SilentlyContinue | Select-Object Name)
for($i=0; $i -lt $dirs.length;$i++)
    {
        New-Item -Path "C:\Users\LTGoldman\Desktop\newkeys" -Name $dirs[$i].Name -ItemType "directory"
        Move-Item -Path "C:\Users\LTGoldman\Desktop\keys\"+$dirs[$i]+"\*.tar.gz" -Destination C:\Users\LTGoldman\Desktop\newkeys\$dirs[$i].Name
    }

I am not sure how to concatenate the Move-Item line properly?

3
  • 1
    At first glance, doing this will solve your problem $dirs[$i].Name (on both lines) or just do Select-Object -ExpandProperty Name Commented Dec 9, 2021 at 0:06
  • 1
    You also need to correct this Move-Item -Path .\*.tar.gz, which files are these? The ones that are in each $dirs[$i] ? Commented Dec 9, 2021 at 0:21
  • Brilliant, that seems to have done the trick, the folders are empty at the moment but I will look into this first. If I have further difficulties, i'll post back. Commented Dec 9, 2021 at 0:29

1 Answer 1

2

Normally you would use a foreach loop to handle this, it also looks cleaner:

$sourceFolder = 'C:\Users\LTGoldman\Desktop\keys'
$destinationFolder = 'C:\Users\LTGoldman\Desktop\keys\temp'

foreach($folder in Get-ChildItem $sourceFolder -Directory -Recurse)
{
    New-Item -Path $destinationFolder -Name $folder.Name -ItemType Directory
}

Since you're using -Recurse you might also will need to handle collision of folders:

$newFolder = Join-Path $destinationFolder -ChildPath $folder.Name
if(Test-Path $newFolder)
{
    Write-Warning "$($folder.Name) already exists in $destinationFolder. Skipping."
    continue
}

Another way to handle collision would be with a try catch, just assume there is no collision and if there is, skip it:

try
{
    New-Item -Path $destinationFolder -Name $folder.Name -ItemType Directory
}
catch
{
    Write-Warning "$($folder.Name) already exists in $destinationFolder. Skipping."
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.