编写脚本来搜索解决方案

时间:2015-05-27 14:09:10

标签: visual-studio powershell batch-file visual-studio-2013 full-text-search

我正在努力从我们的网站上删除大量旧的和未使用的图像。我们使用C#代码运行ASP.NET,并使用Visual Studio(2013)完成我们的工作。现在我只是浏览我们的images目录并在解决方案中搜索图像文件名。虽然我们有一些遵循模式的文件名,并且可以使用正则表达式在一个组中完成,但这仍然相当繁琐。有没有办法可以编写批处理脚本(或任何东西)来搜索此目录中每个文件的解决方案?我可以想象伪代码如

for file in images_directory
    if file not in solution
        delete file

但这可能吗?

从技术上讲,我们只是将文件移动到另一个文件夹中是安全的,所以我猜实际的伪代码更像是

for file in images_directory
    if file not in solution
        move file to backup_directory

2 个答案:

答案 0 :(得分:2)

在解决方案文件中,查找对.csproj文件的所有引用。在每个.csproj文件中,找到所有包含行。在每个包含的文件中,查找包含图像引用的所有行。将每个相关行复制到临时列表。这将使搜索速度比为每个图像多次搜索每个.cs文件更快。

对于每个图形文件,使用findstr在临时列表中对/\bfilename\b/i执行正则表达式搜索。如果未找到,请使用conditional execution启动将孤立映像移动到备份。

使用.bat扩展名保存,将前三行set行修改为适当的值,然后尝试一下。默认情况下,它只是假装移动。如果您对模拟产生的结果感到满意,请从底部附近的echo行中移除move,让脚本脱离其皮带。

@echo off
setlocal

set "image_dir=c:\path\to\images"
set "sln_file=c:\path\to\solution\Project1.sln"
set "backup_dir=c:\path\to\backup"

set "remember=%temp%\proj_images.txt"   
for %%I in ("%sln_file%") do pushd "%%~dpI"

rem // .sln -> .csproj -> .cs -> images.  Find image references and remember.
del "%remember%" >NUL 2>NUL

for /f "delims=" %%I in ('findstr /i ".csproj\>" "%sln_file%"') do (

    rem // %%I contains lines matching /.csproj\b/ig
    for %%p in (%%I) do if exist "%%~p" (

        rem // %%p contains a .csproj filename
        for /f "delims=" %%J in ('findstr /i "\<include\>" "%%~p"') do (

            rem // %%J contains lines matching /\binclude\b/ig
            for %%c in (%%J) do if exist "%%~c" (

                rem // %%c contains the filename of an include
                findstr /i ".png\> .jpg\> .gif\> .bmp\> .tif\>" "%%~c" >>"%remember%" && (

                    echo Images referenced within %%~nxc.  I'll remember this.
                )
            )
        )
    )
)

rem // for each image file in image_dir (recursive)
for /r "%image_dir%" %%I in (*.png *.jpg *.gif *.bmp *.tif) do (

    rem // regexp test for /\bfilename.ext\b/i
    findstr /i "\<%%~nxI\>" "%remember%" >NUL || (

        rem // non-zero exit status of findstr means not found
        echo %%~nxI is not referenced by any files included in the solution's projects.

        rem // *********************************************************
        rem // REMOVE "ECHO" FROM THE FOLLOWING LINE TO ENABLE THE MOVES
        rem // *********************************************************
        echo move "%%~fI" "%backup_dir%"
    )
)

del "%remember%" >NUL 2>NUL
echo Press any key to exit.
pause >NUL

这是你的想法吗?

答案 1 :(得分:0)

一起如果我理解正确您首先要从目录中获取所有图像文件。使用PowerShell:

$imageFiles = Get-ChildItem 'path/to/image/directory' -Recurse | Where-Object { !($_.PSIsContainer) }

这会抓取除目录之外的所有文件。然后:

$solutionText = Get-Content 'path/to/solution/file.csproj' | Out-String
ForEach ($file in $imageFiles ) {
    if ($solutionText -match $file.Name) {
        # Move to another folder
    }
}

唯一的问题是,您需要确保文件名没有机会在文件的其他位置进行匹配,从而产生误报。

相关问题