Glam Prestige Journal

Bright entertainment trends with youth appeal.

I want to scan the whole desktop for .bat and then view the .bat file's content and delete the file if it includes ipconfig for a project. Is there any way I can do it? I'd like to NOT use third party tools

10

4 Answers

The Powershell script below gets the content of the file myfile.bat, check if the string ipconfig is present. If yes, the file is deleted.

$file = 'c:\temp\myfile.bat`
$a = Get-Content $file | Select-String ipconfig
if( $a.Length -gt 0){Remove-Item $file}

To include a folder for example your Desktop, change the script as shown below. Replace mthecodeexpert with your real username.

$folder = 'c:\users\thecodeexpert\*.bat'
$files = Get-ChildItem $folder -file
$files | ForEach-Object{ $a = Select-String -Path $_.Fullname ipconfig; if ($a.Length -gt 0){Remove-Item $_.Fullname}}
5

Continuing from my comment...

# Find the files by type
(Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Filter '*.bat' -Recurse).FullName
# Results
<#
C:\Users\WDAGUtilityAccount\Desktop\SomeBatFile.bat
#>
# Test planned action on the file to find the string pattern
(Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Filter '*.bat' -Recurse).FullName |
ForEach-Object { If (Select-String -Path $PSItem -Pattern 'ipconfig') {Remove-Item -Path $PSItem -WhatIf}
}
# Results
<#
What if: Performing the operation "Remove File" on target "C:\Users\WDAGUtilityAccount\Desktop\SomeBatFile.bat".
#>
# Take action on the file to find the string pattern
(Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Filter '*.bat' -Recurse).FullName |
ForEach-Object { If (Select-String -Path $PSItem -Pattern 'ipconfig') {Remove-Item -Path $PSItem -Force -Verbose}
}
# Results
<#
VERBOSE: Performing the operation "Remove File" on target "C:\Users\WDAGUtilityAccount\Desktop\SomeBatFile.bat".
#>
# Validate removal
(Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Filter '*.bat' -Recurse).FullName
# Results
<#
No results
#>
1

To search all files, use a for loop:

@echo off
cd /d "%userprofile%\desktop"
for %%a in (*.bat) do ( type "%%a"|find /i "ipconfig" && echo del "%%a"
)

There is another way (looks slightly more complicated but in programmer's view is more elegant because it doesn't have to process files that don't contain the search string). This will be much faster, if there are a lot of files (and I mean a lot of). You won't notice the speed advantage in your desktop folder though.

findstr /m returns not the found lines, but the filenames, where such lines were found. Put a for /f around to capture those filenames and delete them:

@echo off
for /f "delims=" %%a in ('findstr /im "ipconfig" "%userprofile%\desktop\*.bat"') do del "%%a"

(Note: your Desktop might be at a different location - check it)

9

You can use Select-String in PowerShell which allows you to search through a directory (for specific file types - extensions) allowing you to find a pattern match in the files filtered for:

$Paths = Select-String -Path "$env:USERPROFILE\Desktop\*" -Include '*.bat','*.cmd' -Pattern 'ipconfig' | Select-Object -ExpandProperty Path if ($null -ne $Paths) { foreach ($Path in $Paths){ "Match found in filepath: $Path" Remove-Item -Path $Path -WhatIf } } else { 'No file found containing pattern' }
  • By default, Select-String finds the first match in each line and, for each match, it displays the file name, line number, and all text in the line containing the match.
  • It also allows you to use Regex pattern matching for more complicated tasks.

Storing the output to a variable, we can explicitly select the Full Path referencing the Path property. Finally, we can add some simple if conditions to evaluate against our variable, and using a foreach loop we can iterate through the capture output.

  • Storing the output to a variable is necessary (at least in this scenario) so the file doesn't stay in use - Thank you dave_thompson_085 for pointing out my mistake.

The draw back of using just this cmdlet is that, it only allows to list a single directory. Luckily, PowerShell can chain commands and all you would need is to slightly modify the code by using a Get-ChildItem with a -Recurse switch to allow for directory (folder) recursion.

$Paths = Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Include '*.cmd','*.bat' -Recurse | Select-String -Pattern 'ipconfig' | Select-Object -ExpandProperty Path if ($null -ne $Paths) { foreach ($Path in $Paths){ "Match found in filepath: $Path" Remove-Item -Path $Path -WhatIf } } else { 'No file found containing pattern' }

Basically the same concept, so when you're ready, remove the -WhatIf common parameter and it will allow the removal of the file(s) found containing that pattern.

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy