# Script-subfolders

**URL:** <https://forums.powershell.org/t/script-subfolders/7607>\
**Category:** PowerShell Help\
**Created:** [November 28, 2016, 4:56pm UTC](https://forums.powershell.org/t/script-subfolders/7607 "2016-11-28T16:56:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![micky](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@micky](https://forums.powershell.org/u/micky)\
**Post date:** [November 28, 2016, 4:56pm UTC](https://forums.powershell.org/t/script-subfolders/7607/1 "2016-11-28T16:56:50Z")

</div>

Hello, i’m completely new to powershell.  
I’m trying to make a script where user enter path of a folder (C:\xx). The script in this folder and all subfolders (recursively) must find all files older than x days (x entered by the user). For each file that’s found the script asks user if it wants to be deleted. If user enter “yes”, script actually delete a file.  
My problem is that when script asks me if i want to delete file in subfolder (C:\xx\bin) and i enter yes i get error message : Remove-Item : Cannot find path 'C:\xx\bin\demo32.exe because it does not exist. (but it’s actually there). I have this problem only in subfolders…

ps. Sorry for my english

```
$a = Read-Host "Path of a Folder: "
$b = Read-Host "Enter x number: "
$c = Get-ChildItem -Path $a -Include *.* -Recurse | Where-Object {$_.LastWriteTime -lt(Get-Date).AddDays(-$b)}

foreach ($item in $c) {
	$decision = Read-Host "Do you want to delete $item ? (yes/no)"
	if($decision -eq "yes")
{
	Remove-Item -Path $a\$item -Force
}
}
```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 28, 2016, 6:19pm UTC](https://forums.powershell.org/t/script-subfolders/7607/2 "2016-11-28T18:19:38Z")

</div>

As you can see when your script asks for confirmation, $Item already has the path information. You don’t need to add $a.  
Instead of

```
Remove-Item -Path $a$item -Force
```
 it should be enough to do 
```
Remove-Item -Path $item -Force
```

---

<div class="post-metadata">

**Author:** ![micky](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@micky](https://forums.powershell.org/u/micky)\
**Post date:** [November 29, 2016, 12:49am UTC](https://forums.powershell.org/t/script-subfolders/7607/3 "2016-11-29T00:49:41Z")

</div>

I tried, but it’s still not working.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 29, 2016, 4:07am UTC](https://forums.powershell.org/t/script-subfolders/7607/4 "2016-11-29T04:07:21Z")

</div>

Do you get errors?

---

<div class="post-metadata">

**Author:** ![micky](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@micky](https://forums.powershell.org/u/micky)\
**Post date:** [November 29, 2016, 4:41am UTC](https://forums.powershell.org/t/script-subfolders/7607/5 "2016-11-29T04:41:07Z")

</div>

Yes:

Remove-Item : Cannot find path ‘C:\xx\C:\xx\Bin\demo32.exe’ because it does not exist. // why is here 2x c:.…?

- 

```
Remove-Item $a\$item -Force

```

- 

```
~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : ObjectNotFound: (C:\xx…\Bin\demo32.exe:String) [Remove-Item], ItemNotFoundException
  - FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 29, 2016, 4:44am UTC](https://forums.powershell.org/t/script-subfolders/7607/6 "2016-11-29T04:44:52Z")

</div>

So you did not change your code, right? It will not work this way. You will have to remove the '$a' from your ‘Remove-Item’!

The error message even shows it to you.

---

<div class="post-metadata">

**Author:** ![micky](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@micky](https://forums.powershell.org/u/micky)\
**Post date:** [November 29, 2016, 4:52am UTC](https://forums.powershell.org/t/script-subfolders/7607/7 "2016-11-29T04:52:57Z")

</div>

It’s working :). I forgot to delete ‘$a’. Thank you for your help

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:39pm UTC](https://forums.powershell.org/t/script-subfolders/7607/8 "2024-05-16T20:39:51Z")

</div>

Closed on May 16, 2024, 8:39pm UTC


