首页 > 解决方案 > Batch For /R Results to be set menu选项

问题描述

在一个项目上需要一点帮助。

设想 :-

手动将 PC 迁移到新域会破坏软件,因为新配置文件没有旧用户配置文件中的设置 - 需要使其运行。旧的配置文件被称为一件事,新的配置文件被称为其他东西,例如

C:\Users\Sue.Barnes.B3209 (old) C:\Users\Susan.Barnes.NGP (new)

如果用户以前使用过,在每个(旧)配置文件的 %localappdata% 中“可能”有一个该软件的配置文件。例如

C:\Users\Sue.Barnes.B3209\AppData\Local\Acme_Limited\Acme.exe_Url_m5l4ujc5t22f3qw0q5uz1dwlfcnrdaoh\1.0.0.15\user.config

目标:-

我希望能够扫描 C:\Users 文件夹并报告找到的文件的完整路径、名称和扩展名,然后在菜单选项中设置列表,在输入选项时将复制 user.config文件到完全相同的位置,但在当前登录的新配置文件 appdata 文件夹中。

例如

Enumerating List Of User Profiles in the System...

Admin
Public
User

From the Profiles detecting active Configs...

C:\Users\Admin\AppData\Local\Acme_Limited\Acme.exe_Url_m5l4ujc5t22f3qw0q5uz1dwlfcnrdaoh\1.0.0.15\user.config
C:\Users\Public\AppData\Local\Acme_Limited\Acme.exe_Url_m5l4ujc5t22f3qw0q5uz1dwlfcnrdaoh\1.0.0.15\user.config

Press any key to continue . . .

然而,在这一点上,我已经超出了我的深度。我似乎无法为找到文件的每个实例创建一个选择菜单。不管找到多少,我想在屏幕上打印一个数字,这样我就可以选择正确的配置复制到新设置的配置文件中。简单地,

    Xcopy /f C:\Users\Sue.Barnes.B3209\AppData\Local\Acme_Limited\Acme.exe_Url_m5l4ujc5t22f3qw0q5uz1dwlfcnrdaoh\1.0.0.15\user.config

%localappdata%\Acme_Limited\Acme.exe_Url_m5l4ujc5t22f3qw0q5uz1dwlfcnrdaoh\1.0.0.15\

当前代码库:-

echo.
echo Enumerating List Of User Profiles in the System...
echo.
TIMEOUT /T 3 >NUL
dir /b C:\Users
echo.
echo From the Profiles detecting active Configs...
echo.
TIMEOUT /T 3 >NUL
for /R "C:\Users" %%a in (1.0.0.15\user.config*) do (
echo %%~dpnxa 
    )
)
echo.
pause

任何帮助将不胜感激,谢谢

标签: arraysbatch-filecmd

解决方案


@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
set "dontshow=u:\dontshow.txt"
set "configs=u:\configs.txt"
set "profiles=u:\profiles.txt"

:again

echo :>>"%dontshow%"
del "%configs%" >nul 2>nul
del "%profiles%" >nul 2>nul

:: Find all of the "user.config" files having ..\appdata\local\Acme_limited\...1.0.0.15
FOR /f "tokens=1-9delims=\" %%a IN (
 'dir /s /b /a-d "%sourcedir%\user.config" '
 ) DO if /i "%%d"=="appdata" if /i "%%e"=="local" if /i "%%f"=="Acme_Limited" if /i "%%h"=="1.0.0.15" (
 rem these are the users' config files that may be copied.
 echo %%c|findstr /x /g:"%dontshow%" /i /L >nul
 if errorlevel 1 >>"%configs%" echo %%c
)

:: Find all of the user profiles that are not in "dontshow" and do NOT have "user.config" - These are the profiles that may need to be processed
FOR /f "delims=" %%a IN (
 'dir /b /ad "%sourcedir%" '
 ) DO (
 echo %%a|findstr /x /g:"%dontshow%" /i /L >nul
 if errorlevel 1  echo %%a|findstr /x /g:"%configs%" /i /L >nul 
 if errorlevel 1 >>"%profiles%" echo %%a
)

:: Now we have a list of profiles with "user.config" missing in %profiles%
:: And possible source-profiles in %configs%

:: Show the list of "config missing" profiles
cls
findstr /n /v /L /c:":" "%profiles%"
:reselectd
set "selectiond="
set /p "selectiond=Choose destination profile "
if not defined selectiond goto :eof
for /f "tokens=1*delims=:" %%a in ('findstr /n /v /L /c:":" "%profiles%"') do if "%%a"=="%selectiond%" set "selectiond=%%b"&goto selects
echo Invalid selection
goto reselectd

:selects

for /L %%a in (1,1,4) do echo.
findstr /n /v /L /c:":" "%configs%"

:reselects
set "selections="
set /p "selections=Choose source profile "
if not defined selections goto :eof
for /f "tokens=1*delims=:" %%a in ('findstr /n /v /L /c:":" "%configs%"') do if "%%a"=="%selections%" set "selections=%%b"&goto process
echo Invalid selection
goto reselects

:process
echo Copy from profile "%selections%" to "%selectiond%" ?
choice 

if errorlevel 2 goto again

:: Do the copy... just echoed, remove `echo` keyword to activate

ECHO md "%sourcedir%\%selectiond%\appdata\localAcme_Limited\whatever\1.0.0.15"
ECHO copy  "%sourcedir%\%selections%\appdata\localAcme_Limited\whatever\1.0.0.15\user.config" "%sourcedir%\%selectiond%\appdata\localAcme_Limited\whatever\1.0.0.15"
 
choice /M "Add '%selections%' to processed file ? "
if errorlevel 2 goto processedd

>>"%dontshow%" echo %selections%

:processedd
choice /M "Add '%selectiond%' to processed file ? "
if errorlevel 2 goto again

>>"%dontshow%" echo %selectiond%
goto again

有趣的练习。但是,缺少有关内容的信息Acme.exe_Url_m5l4ujc5t22f3qw0q5uz1dwlfcnrdaoh(常量字符串,每个用户更改?什么?) - 留给 OP 解决。

您需要更改设置sourcedir以适应您的情况。该清单使用适合我的系统的设置。

我决定关键部分是第三级目录 - 用户 ID。这种方法使用用户 ID 文件,两个是临时的,一个是永久的 - “dontshow.txt”,其中包含不显示的用户 ID 列表。如果需要,可以使用编辑器手动维护。

所以 - 首先,在“dontshow”文件中添加一行包含一个冒号的行。这是一个永远无法匹配真实用户 ID 的虚拟对象,并且是必需的,因为findstr它不喜欢空字典文件。

第一步是查找所有user.config文件,并用于for /f处理找到的文件名,选择重要的目录级别。排除任何不符合条件的路径后,第三级内容将与“dontshow.txt”中的排除项进行匹配。如果找到的名称是新名称,请将其添加到configs文件中。

第二节 - 与第一节相同,但这次只查看目录名称。请注意,profiles如果找到的名称不在dontshow列表中并且也不在列表中,则输入config

现在通过使用 列出profiles并附加前缀linenumber:来选择目标配置文件findstr。输入一个数字(手动输入,未选中),然后使用 匹配行号for/f

冲洗并重复configs源配置文件的文件。

显示建议的命令,接受 Y/N 响应,然后询问是否应将每个名称添加到dontshow列表中。

一直到早上。

请注意,响应Return任何set /p命令都将退出批处理。


推荐阅读