Windows 如何创建一个包含目录名并在批处理文件中按位置选择的菜单?

Windows 如何创建一个包含目录名并在批处理文件中按位置选择的菜单?,windows,batch-file,cmd,Windows,Batch File,Cmd,首先,我想说,我涉足编程已经10年了,我对所学内容的记忆似乎让我失望。我希望我已经包含了足够的信息,让人们能够看到我在做什么,而不会走极端。可能会有更好的方法来做我所做的事情,任何建议都将不胜感激 我有2个批处理文件,都在父目录中,1个用于将父目录的2个子目录中的文件备份到父目录下几层的另一个子目录中 例如。 批处理文件位于目录“Test Folder”中 要备份的文件位于以下位置:- Test Folder\LocalProfiles 及 要将它们备份到的文件夹称为“备份”,其中创建的目录使

首先,我想说,我涉足编程已经10年了,我对所学内容的记忆似乎让我失望。我希望我已经包含了足够的信息,让人们能够看到我在做什么,而不会走极端。可能会有更好的方法来做我所做的事情,任何建议都将不胜感激

我有2个批处理文件,都在父目录中,1个用于将父目录的2个子目录中的文件备份到父目录下几层的另一个子目录中

例如。 批处理文件位于目录“Test Folder”中 要备份的文件位于以下位置:-

Test Folder\LocalProfiles

要将它们备份到的文件夹称为“备份”,其中创建的目录使用用户选择的名称,例如“蓝色”,然后在该目录下的另一个目录中使用当前日期和时间作为目录名称,后跟原始目录。(日期和时间的格式(当前为2016-04-06 23.03.15)尚未最终确定,因为我在备份批处理文件中使用的日期和时间之间的空间在恢复时给我带来了此批处理文件中的问题)。因此,此用户已备份的文件的位置为:-

Test Folder\Backup\Blue\2016-04-06 23.03.15\LocalProfiles

我已经编写了一个批处理文件,其中列出了所有用户选择的目录 然后,在用户选择一个目录后,他们从标有日期和时间的目录中选择一个目录 然后将下面的文件复制回原始位置

问题是这需要时间,而且可能会导致打字错误,尤其是在输入日期和时间时。我想通过创建一个菜单,用户可以在其中选择一个与用户创建的目录对应的数字,从而使它尽可能简单

最终结果将是:- 第一菜单屏幕

选择以下选项之一 1.蓝色 2.红色 3.绿色 等等

然后在下一个菜单屏幕上显示包含在所选目录中的日期和时间的目录

第二菜单

选择以下选项之一 1.2016-04-05 21.03.28 2.2016-04-05 21.05.51 3.2016-04-06 23.00.14 等等

用户只需键入与所选目录对应的编号,然后按enter键

最后,我想合并这两个批处理文件,选择备份或恢复,但我需要这一点才能正常工作

以下是在不使用菜单的情况下还原文件的代码。它可以工作,但并不总是处理目录名中的空格,我相信有更好的方法来处理错误检查和消息

@echo off

if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit )

:Prompt for Profile Name
cls
echo.
echo Please select one of the following Profiles to load.
echo.


:: Create the list of Profiles from the Directory Names
for /d %%D in (.\Backup\*) do echo %%~nxD
echo. 


:: Prompt for Profile Name for multiple Profile Names
set "profile="
set /p profile=Please enter a Profile Name. 

:: OR

:: Consider adding Fixed Profile Name to top of list/menu as the default for quick saving of files.
:: set "profile=Default"


:: Check that the Profile exists
echo.
if not exist ".\Backup\%profile%" echo The Profile %profile% does not exist.
if not exist ".\Backup\%profile%" pause
if [%profile%] == [] echo A Profile must have a name. 
if [%profile%] == [] pause

:: If input is empty or doesn't exist return to the beginning.
if not exist ".\Backup\%profile%" goto Prompt for Profile Name
if [%profile%] == []  goto Prompt for Profile Name


:Saved
:: Create the list of Saved locations from the Directory Names

cls
echo.
echo Existing Saved Locations for the Profile named %profile%
echo.
for /d %%S in (".\Backup\%profile%\*") do echo %%~nxS


:: Prompt for Saved location for selected Profile Name

set "save="

echo.
set /p save=Please enter a Saved location. 


:: Check that the Saved Location exists

echo.
if not exist ".\Backup\%profile%\%save%" echo The Saved Location %profile%\"%save%" does not exist.
if not exist ".\Backup\%profile%\%save%" echo Select a Saved Location from the list.
if not exist ".\Backup\%profile%\%save%" pause


:: Check that the Saved Location is not empty

if [%save%] == [] echo Select a Saved Location from the list. 

if [%save%] == [] pause


:: If input is empty or doesn't exist return to the beginning.

if not exist ".\Backup\%profile%\%save%" goto Saved
if [%save%] == []  goto Saved


:: Check for Saved files

if not exist ".\Backup\%profile%\%save%\LocalProfiles\LocalData.profile" echo.
if not exist ".\Backup\%profile%\%save%\LocalProfiles\LocalData.profile" echo LocalData.profile for %profile% does not exist at location "%save%\LocalProfiles\"

if not exist ".\Backup\%profile%\%save%\SavedLocal\LocalMember.profile" echo.
if not exist ".\Backup\%profile%\%save%\SavedLocal\LocalMember.profile" echo LocalMember.profile for %profile% does not exist at "%save%\SavedLocal\"


:: If LocalData.profile doesn't exist display message and return to Saves.

if not exist ".\Backup\%profile%\%save%\LocalProfiles\LocalData.profile" echo.
if not exist ".\Backup\%profile%\%save%\LocalProfiles\LocalData.profile" echo The Save at %save% is incomplete or does not exist.

if not exist ".\Backup\%profile%\%save%\LocalProfiles\LocalData.profile" echo.
if not exist ".\Backup\%profile%\%save%\LocalProfiles\LocalData.profile" echo Please select another Save. 

if not exist ".\Backup\%profile%\%save%\LocalProfiles\LocalData.profile" Pause

if not exist ".\Backup\%profile%\%save%\LocalProfiles\LocalData.profile" goto Saved


:: If LocalMember.profile doesn't exist display message and return to Saves.

if not exist ".\Backup\%profile%\%save%\SavedLocal\LocalMember.profile" echo.
if not exist ".\Backup\%profile%\%save%\SavedLocal\LocalMember.profile" echo The Save at %save% is incomplete or does not exist.

if not exist ".\Backup\%profile%\%save%\SavedLocal\LocalMember.profile" echo.
if not exist ".\Backup\%profile%\%save%\SavedLocal\LocalMember.profile" echo Please select another Save. 

if not exist ".\Backup\%profile%\%save%\SavedLocal\LocalMember.profile" Pause

if not exist ".\Backup\%profile%\%save%\SavedLocal\LocalMember.profile" goto Saved


:Load Save

:: Restore the valid Save to the original location.

echo.
echo Restoring Saved Files

robocopy ".\Backup\%profile%\%Save%\LocalProfiles" ".\LocalProfiles"
robocopy ".\Backup\%profile%\%Save%\SavedLocal" ".\SavedLocal"

echo.
echo Files Restored
pause
exit
@ECHO关闭
SETLOCAL
设置“sourcedir=U:\sourcedir”
设置“directories=localprofiles savedlocal”
电话:zap$
::第一个菜单
呼叫:立即选择蓝色默认绿色红色“白色和浅褐色条纹”/
设置“userchosenname=%$choice%”
::不知道日期和时间的格式。
::我使用dd/mm/yyyy和hh:mm:ss.hh
设置“backupdatestamp=%date:~-4%-%date:~3,2%-%date:~0,2%%时间:=。%”
设置“backupdatestamp=%backupdatestamp:~0,-3%”
调用:选择“备份为%backupdatestamp%”
对于/f“delims=“%%a IN('dir/b/ad”%sourcedir%\%userchosenname%\'2^>nul'),请执行以下操作:(
如果存在“%sourcedir%\%userchosenname%\%%a\localprofiles\”调用:选择“从%%a还原”
)
调用:选择/现在“在此提示下”
对于(%directories%)中的%%a,请执行以下操作(
如果“%$choice:~0,6%”==“备份”(
ECHO(MD“%sourcedir%\%userchosenname%\%backupdatestamp%\%%a”
ECHO(XCOPY/s/e“%sourcedir%\%a”“%sourcedir%\%userchosenname%\%backupdatestamp%\%a\”
)否则(
ECHO(XCOPY/s/e“%sourcedir%\%userchosenname%\%%$选项:~13%\%%a\*”“%sourcedir%\%%a\”
)
)
后藤:EOF
::删除开始时的变量$
:ZAP$
对于/F“delims=”%%Z In('set$2^>Nul')DO set“%%Z=”
后藤:EOF
::做出选择
::如果参数为“/now”,请实际使用提示下一个参数进行选择
::其他选择
:选择
设置“$item=%~1”
如果未定义$item GOTO:EOF
如果/i“%$item%”=“/now”转到makechoice
如果未定义$options cls
如果未定义$choices集“$choices=123456789ABCDEFGHIJKLMNOPQRSTUVXYZ”
设置“$options=%%$options%%$choices:~0,1%”
设置“$choices=%$choices:~1%”
设置/a$optioncount+=1
设置“$option%$optioncount%=%~1”
回显(%$选项:~-1%%~1)
移位
转到选择
:作出选择
如果“%~2”neq“设置”$options=%$options%/m“%~2”
选项/c%$选项%
调用集“$choice=%%$option%errorlevel%%”
调用:zap$&设置“$choice=%$choice%”
后藤:EOF
您需要更改
sourcedir
的设置以适应您的环境

所需的MD命令仅用于测试目的。
ECHO
ed。验证命令正确后,将
ECHO(MD
更改为
MD
以实际创建目录。追加
2>nul
以抑制错误消息(例如,当目录已存在时)

所需的XCOPY命令仅用于测试目的。
ECHO
ed。验证命令正确后,将
ECHO(XCOPY
更改为
XCOPY
以实际复制文件。追加
>nul
以抑制报告消息(例如,
1个复制的文件

此例程的基础是
:选择将选项累加到$-变量中的
子例程。它可以使用1个或多个参数运行,也可以选择引用(如果参数包含空格)

如果找到参数
/now
,则例程要求从显示的选项中进行选择。如果
/now
参数后面紧跟着另一个参数,则以下参数将用作提示选择

调用:zap$&SET“$choice=%%$choice%”
行使用与从setlocal/endlocal括号导出值相同的解析技巧

除此之外,您需要做的就是使用批处理substri修复
backupdatestamp
的设置
Test Folder\Backup\Blue\2016-04-06 23.03.15\SavedLocal
@echo off

if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit )

:Prompt for Profile Name
cls
echo.
echo Please select one of the following Profiles to load.
echo.


:: Create the list of Profiles from the Directory Names
for /d %%D in (.\Backup\*) do echo %%~nxD
echo. 


:: Prompt for Profile Name for multiple Profile Names
set "profile="
set /p profile=Please enter a Profile Name. 

:: OR

:: Consider adding Fixed Profile Name to top of list/menu as the default for quick saving of files.
:: set "profile=Default"


:: Check that the Profile exists
echo.
if not exist ".\Backup\%profile%" echo The Profile %profile% does not exist.
if not exist ".\Backup\%profile%" pause
if [%profile%] == [] echo A Profile must have a name. 
if [%profile%] == [] pause

:: If input is empty or doesn't exist return to the beginning.
if not exist ".\Backup\%profile%" goto Prompt for Profile Name
if [%profile%] == []  goto Prompt for Profile Name


:Saved
:: Create the list of Saved locations from the Directory Names

cls
echo.
echo Existing Saved Locations for the Profile named %profile%
echo.
for /d %%S in (".\Backup\%profile%\*") do echo %%~nxS


:: Prompt for Saved location for selected Profile Name

set "save="

echo.
set /p save=Please enter a Saved location. 


:: Check that the Saved Location exists

echo.
if not exist ".\Backup\%profile%\%save%" echo The Saved Location %profile%\"%save%" does not exist.
if not exist ".\Backup\%profile%\%save%" echo Select a Saved Location from the list.
if not exist ".\Backup\%profile%\%save%" pause


:: Check that the Saved Location is not empty

if [%save%] == [] echo Select a Saved Location from the list. 

if [%save%] == [] pause


:: If input is empty or doesn't exist return to the beginning.

if not exist ".\Backup\%profile%\%save%" goto Saved
if [%save%] == []  goto Saved


:: Check for Saved files

if not exist ".\Backup\%profile%\%save%\LocalProfiles\LocalData.profile" echo.
if not exist ".\Backup\%profile%\%save%\LocalProfiles\LocalData.profile" echo LocalData.profile for %profile% does not exist at location "%save%\LocalProfiles\"

if not exist ".\Backup\%profile%\%save%\SavedLocal\LocalMember.profile" echo.
if not exist ".\Backup\%profile%\%save%\SavedLocal\LocalMember.profile" echo LocalMember.profile for %profile% does not exist at "%save%\SavedLocal\"


:: If LocalData.profile doesn't exist display message and return to Saves.

if not exist ".\Backup\%profile%\%save%\LocalProfiles\LocalData.profile" echo.
if not exist ".\Backup\%profile%\%save%\LocalProfiles\LocalData.profile" echo The Save at %save% is incomplete or does not exist.

if not exist ".\Backup\%profile%\%save%\LocalProfiles\LocalData.profile" echo.
if not exist ".\Backup\%profile%\%save%\LocalProfiles\LocalData.profile" echo Please select another Save. 

if not exist ".\Backup\%profile%\%save%\LocalProfiles\LocalData.profile" Pause

if not exist ".\Backup\%profile%\%save%\LocalProfiles\LocalData.profile" goto Saved


:: If LocalMember.profile doesn't exist display message and return to Saves.

if not exist ".\Backup\%profile%\%save%\SavedLocal\LocalMember.profile" echo.
if not exist ".\Backup\%profile%\%save%\SavedLocal\LocalMember.profile" echo The Save at %save% is incomplete or does not exist.

if not exist ".\Backup\%profile%\%save%\SavedLocal\LocalMember.profile" echo.
if not exist ".\Backup\%profile%\%save%\SavedLocal\LocalMember.profile" echo Please select another Save. 

if not exist ".\Backup\%profile%\%save%\SavedLocal\LocalMember.profile" Pause

if not exist ".\Backup\%profile%\%save%\SavedLocal\LocalMember.profile" goto Saved


:Load Save

:: Restore the valid Save to the original location.

echo.
echo Restoring Saved Files

robocopy ".\Backup\%profile%\%Save%\LocalProfiles" ".\LocalProfiles"
robocopy ".\Backup\%profile%\%Save%\SavedLocal" ".\SavedLocal"

echo.
echo Files Restored
pause
exit
set /?