保存未知(用户)数量 files/directory 的批处理脚本
Batch script that save unknown(user) number of files/directory
我正在尝试做一个批处理脚本来备份文件/目录,路径由用户提供。问题是我需要让用户决定他想输入多少路径(可以超过 10 个),然后在新目录中复制所有 files/directories 之后。到目前为止,我做了这个只采用一条路径作为参数:
SET /P numuser=How many paths?
SET /P pathh=Enter the path
SET "foldname=sav-%DATE%-%TIME:~0,8%
SET "foldname=%foldname::=-%
echo foldname=%foldname%
mkdir "%foldname%"
cd "%foldname%"
xcopy "%pathh%" "%cd%"
pause
考虑到使用决定路径的数量,我不太确定如何将所有不同的路径存储在不同的变量中。所以我无法初始化像“SET path1=”“SET path2=”等变量...因为我不知道我需要的路径数量。我想我需要一个循环:
FOR %%c IN(numuser) DO
SET /P path1=enter the path
xcopy "%path1%" "%cd%"
但是我又遇到了路径变量名的问题。随着循环的进行,我需要增加并创建新变量。我不知道该怎么做。
允许"store all the differents paths in differents variables"的概念的技术术语是array。下面的批处理文件显示了如何使用数组来解决此问题。请注意,与预先计算所需路径的数量相比,用户更容易给出多个路径,直到按下回车键:
@echo off
setlocal EnableDelayedExpansion
rem Get the paths from user and store them in "path" array
set num=0
:nextPath
set "input="
set /P "input=Enter next path: "
if not defined input goto endPaths
set /A num+=1
set "path[%num%]=%input%"
goto nextPath
:endPaths
rem Process the stored paths
for /L %%i in (1,1,%num%) do (
echo Processing: "!path[%%i]!"
)
有关批处理文件中数组管理的更多说明,请参阅this post。
我正在尝试做一个批处理脚本来备份文件/目录,路径由用户提供。问题是我需要让用户决定他想输入多少路径(可以超过 10 个),然后在新目录中复制所有 files/directories 之后。到目前为止,我做了这个只采用一条路径作为参数:
SET /P numuser=How many paths?
SET /P pathh=Enter the path
SET "foldname=sav-%DATE%-%TIME:~0,8%
SET "foldname=%foldname::=-%
echo foldname=%foldname%
mkdir "%foldname%"
cd "%foldname%"
xcopy "%pathh%" "%cd%"
pause
考虑到使用决定路径的数量,我不太确定如何将所有不同的路径存储在不同的变量中。所以我无法初始化像“SET path1=”“SET path2=”等变量...因为我不知道我需要的路径数量。我想我需要一个循环:
FOR %%c IN(numuser) DO
SET /P path1=enter the path
xcopy "%path1%" "%cd%"
但是我又遇到了路径变量名的问题。随着循环的进行,我需要增加并创建新变量。我不知道该怎么做。
允许"store all the differents paths in differents variables"的概念的技术术语是array。下面的批处理文件显示了如何使用数组来解决此问题。请注意,与预先计算所需路径的数量相比,用户更容易给出多个路径,直到按下回车键:
@echo off
setlocal EnableDelayedExpansion
rem Get the paths from user and store them in "path" array
set num=0
:nextPath
set "input="
set /P "input=Enter next path: "
if not defined input goto endPaths
set /A num+=1
set "path[%num%]=%input%"
goto nextPath
:endPaths
rem Process the stored paths
for /L %%i in (1,1,%num%) do (
echo Processing: "!path[%%i]!"
)
有关批处理文件中数组管理的更多说明,请参阅this post。