在硬盘上查找文件,如果存在则删除

在硬盘上查找文件,如果存在则删除

C:\Temp\*\FileToRemove.txt旧程序在或内部这两个位置之一创建了一个文件C:\Windows\Temp\*\FileToRemove.txt,我需要从网络上的计算机中删除此文件,因为该文件包含不应通过网络访问的数据。

我尝试在一个位置使用这个修改后的脚本来执行此操作,它找到了该文件,但没有按预期删除该文件:

function delete-remotefile {
  PROCESS {
    $file = Get-ChildItem -Path C:\Temp\* -Include FileToRemove.txt -Recurse
    if (Test-Path $file -PathType Leaf)
    {
      echo "file exist"
      Remove-Item $file -Force
      echo "file deleted"
    }
  }
}

Get-Content C:\Computers.txt | delete-remotefile
  • 我的目标是搜索两个位置,但是这只适用于本地计算机的单个位置,如果文件不存在,脚本就会出错。

如果文件不存在,我该如何让这个脚本无错误地完成?

答案1

由于您提到过您的系统可以通过 UNC 路径访问,因此我选择了 UNC 路径。请注意,要搜索的路径的列出方式c$\temp与您在命令行中查找它们的路径相同。

我没有删除任何东西。我想您可以根据需要添加。

该代码似乎注释得相当好,但如果您有任何疑问...请询问。[咧嘴笑]

# save the Verbose setting
$Old_VPref = $VerbosePreference
# enable display of Write-Verbose
$VerbosePreference = 'Continue'

#region >>> fake reading in a list of computers
#    in real life, use Get-Content
$ComputerList = @'
LocalHost
10.0.0.1
127.0.0.1
BetterNotBeThere
'@ -split [System.Environment]::NewLine
#endregion >>> fake reading in a list of computers

$RootDirList = @(
    'c$\temp'
    'C$\Windows\Temp'
    )
$FileToFindList = @(
    # when ready to do this for real,
    #    put your real file name on the next line
    #'FileToRemove.txt'
    #    then remove or comment out the two fake ones below
    'ThisIsTheOne.txt'
    'Not_There.txt'
    )
$NotFound = '__FileNotFound__'
$NoResponse = '__No_Response__'

$Results = foreach ($CL_Item in $ComputerList)
    {
    Write-Verbose ('Checking {0} ...' -f $CL_Item)
    if (Test-Connection -ComputerName $CL_Item -Count 1 -Quiet)
        {
        foreach ($RDL_Item in $RootDirList)
            {
            foreach ($FTFL_Item in $FileToFindList)
                {
                $GCI_Params = @{
                    Path = '\\{0}\{1}' -f $CL_Item, $RDL_Item
                    Filter = $FTFL_Item
                    Recurse =$True
                    File = $True
                    ErrorAction = 'SilentlyContinue'
                    }
                $FileInfo = Get-ChildItem @GCI_Params

                $IsThere = [bool]$FileInfo
                if ($IsThere)
                    {
                    $FoundPath = $FileInfo.FullName
                    }
                    else
                    {
                    $FoundPath = $NotFound
                    }
                [PSCustomObject]@{
                    ComputerName = $CL_Item
                    FileName = $FTFL_Item
                    RootDir = $RDL_Item
                    IsThere = $IsThere
                    FoundPath = $FoundPath
                    }
                } # end >>> foreach ($FTFL_Item in $FileToFindList)
            } # end >>> foreach ($RDL_Item in $RootDirList)
        }
        else
        {
        Write-Warning ('    {0} did not respond.' -f $CL_Item)
        [PSCustomObject]@{
            ComputerName = $CL_Item
            FileName = $NoResponse
            RootDir = $NoResponse
            IsThere = $NoResponse
            FoundPath = $NoResponse
            }
        } # end >>> if (Test-Connection -ComputerName $CL_Item -Count 1 -Quiet)
    } # end >>> foreach ($CL_Item in $ComputerList)

# restore old verbose pref
$VerbosePreference = $Old_VPref

# show on screen
#    pipe this to Export-Csv if you want to save the info
$Results

脚本运行期间的输出...

VERBOSE: Checking LocalHost ...
VERBOSE: Checking 10.0.0.1 ...
WARNING:     10.0.0.1 did not respond.
VERBOSE: Checking 127.0.0.1 ...
VERBOSE: Checking BetterNotBeThere ...
WARNING:     BetterNotBeThere did not respond.

脚本末尾的 $Results 输出被截断...

ComputerName : LocalHost
FileName     : ThisIsTheOne.txt
RootDir      : c$\temp
IsThere      : True
FoundPath    : \\LocalHost\c$\temp\KSN_TestRootDir\ThisIsTheOne.txt

ComputerName : LocalHost
FileName     : Not_There.txt
RootDir      : c$\temp
IsThere      : False
FoundPath    : __FileNotFound__

ComputerName : LocalHost
FileName     : ThisIsTheOne.txt
RootDir      : C$\Windows\Temp
IsThere      : True
FoundPath    : \\LocalHost\C$\Windows\Temp\KSN_TestDir_Level_1\KSN_TestDir_Level_2\ThisIsTheOne.txt

ComputerName : LocalHost
FileName     : Not_There.txt
RootDir      : C$\Windows\Temp
IsThere      : False
FoundPath    : __FileNotFound__

ComputerName : 10.0.0.1
FileName     : __No_Response__
RootDir      : __No_Response__
IsThere      : __No_Response__
FoundPath    : __No_Response__

[*...snip...*] 

ComputerName : BetterNotBeThere
FileName     : __No_Response__
RootDir      : __No_Response__
IsThere      : __No_Response__
FoundPath    : __No_Response__

相关内容