I'm trying to do a batch script that will do a backup of file(s)/directorie(s), which the path is given by the user. The problem is that I need to let the user decide on how many path he want to enter(can be over 10),and then after make a copy of all the files/directories in a new directories. So far I made this which only take one path as argument:
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
I'm not quite sure on how to be able to store all the different paths in different variables, considering that the use decide on the number of path. So I cannot initialize variable like "SET path1=""SET path2="etc... Since I can't know the numbers of path I'm going to need. I guess I need a loop:
FOR %%c IN(numuser) DO
SET /P path1=enter the path
xcopy "%path1%" "%cd%"
But here again I got the problem that path variable name. I would need to increment and create new variable as the loop progress. I don't know how to do this.
The technical term of the concept that allows to "store all the differents paths in differents variables" is array. The Batch file below show how to use an array in order to solve this problem. Note that it is easier for the user to give several paths until just press enter, than count in advance the number of desired paths:
@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]!"
)
For further explanations on array management in Batch files, see this post.