首页 > 解决方案 > 字符串存在检查并回显到文件

问题描述

对于此目录中的每个文件夹:

"F:\!Storage\!FS Addons\!X-Plane\!Tools\!Ortho4XP\Tiles"

以下是上述目录中的示例文件夹(数字为 GPS 坐标):

zOrtho4XP_+65-023
zOrtho4XP_+65-024
zOrtho4XP_+65-025
zOrtho4XP_+66-015
zOrtho4XP_+66-016

这些zOrtho4XP_*文件夹包含卫星图像,这些图像将覆盖在 X-Plane 11 Flight Simulator 的默认风景上。

为了让卫星图像出现在模拟器中,需要将它们的文件夹名称scenery_packs.ini添加到文件中,该文件位于
"E:\X Plane 11\X-Plane 11\Custom Scenery\scenery_packs.ini"

以下是该scenery_packs.ini文件的示例:注意它如何包含包含卫星图像的文件夹的文件夹名称

SCENERY_PACK Custom Scenery/zOrtho4XP_+65-023/
SCENERY_PACK Custom Scenery/zOrtho4XP_+65-024/
SCENERY_PACK Custom Scenery/zOrtho4XP_+65-025/
SCENERY_PACK Custom Scenery/zOrtho4XP_+66-015/
SCENERY_PACK Custom Scenery/zOrtho4XP_+66-016/

这些zOrtho4XP_*文件夹是分批下载的,有些批次包含几十个这样的zOrtho4XP_*文件夹,每个文件夹名称都必须添加到scenery_packs.ini文件中。这就是我要避免手动执行的操作,因为在接下来的几周内我将做很多这样的事情,而且我更喜欢不必逐行手动添加这些文件夹;如果不超过 1,000 人,那将是数百人。

我已经设法把它写下来了,但据我所知,这是批处理:

@echo off
setlocal

set "target=F:\!Storage\!FS Addons\!X-Plane\!Tools\!Ortho4XP\Tiles"
set "destination=E:\X Plane 11\X-Plane 11\Custom Scenery\scenery_packs.ini"

    for %%a in ("%target%") do (
        findstr %%~nxA 
        if exist IGNORE 
        )else(
        echo %%~nxA >> %destination% )

endlocal
pause

标签: batch-file

解决方案


@echo off
setlocal

set "target=F:\!Storage\!FS Addons\!X-Plane\!Tools\!Ortho4XP\Tiles"
set "destination=E:\X Plane 11\X-Plane 11\Custom Scenery\scenery_packs.ini"

(
    for /d %%A in ("%target%\*") do @(
        >nul findstr /c:"SCENERY_PACK Custom Scenery/%%~nxA/" "%destination%" ^
        || echo SCENERY_PACK Custom Scenery/%%~nxA/
    )
) >> "%destination%"

这将获得根目录中的每个子文件夹名称%target%Findstr将在%destination%ini 文件中搜索文件夹名称模式。如果失败,则将文件夹名称模式回显到%destination%文件。

^是一个行继续字符,因为一行很长。


推荐阅读