简体   繁体   中英

Search for a file and store the path in a variable

I am trying to search for 2 files in a directory tree and delete them. I want to keep the path of the file that had been deleted.

So far that's what I have:

#set the path to search for the file
$path = ".\"

#File name
$file = "GeneratedCode.cs"
$file2 = "TypedEnums.cs"

#Look for the file and delete every instance of it in all directories.
Get-Childitem $path -include $file -recurse | foreach ($_) {remove-item $_.fullname}



#Look for the file and delete every instance of it in all directories.

Get-Childitem $path -include $file2 -recurse | foreach ($_) {remove-item $_.fullname}

EDIT: I forgot to mention that I want to delete every instance of the files.

I would store the result of the Get-ChildItem cmdlet to a variable lets say $filesToDelete . Then you could simple pipe the object to the Remove-Item cmdlet (you don't need the foreach there):

$filesToDelete = Get-Childitem $path -include $file -recurse
$filesToDelete | Remove-Item

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM