0

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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Martin B.
  • 101
  • 2
  • 9
  • Interesting how you want to do this on MS-DOS, check your tags: `Don't use this tag for questions regarding the Windows command line.` ... Ever thought of using `robocopy` for this task ? If I remember correctly it can use a text file which contains a number of directories. – Marged Dec 08 '15 at 23:55
  • I just looked up robocopy.But I think the problem still remain;when I launch the script,it has to ask the user to how many paths he want to save; it cannot be a number of path that is already known at the beginning since its the user choice? – Martin B. Dec 09 '15 at 00:04
  • Although this involves other techniques than batch files: use windows scripting host or powershell to create some kind of gui which will ask the user for folders. Write them to a file and run robocopy. – Marged Dec 09 '15 at 00:05
  • You can either have the user provide all of the paths at once as command line arguments and use `%*` in a `for` loop to process them all, or you can have the user put all of the paths in a text file and use a `for /f` loop to process the text file. – SomethingDark Dec 09 '15 at 00:19
  • And you don't need separate variables for separate paths at all; just overwrite the variable inside of the loop when you're done using it. – SomethingDark Dec 09 '15 at 00:20

1 Answers1

0

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.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108