Duplicating a folder with its content n number of times - Page 1
Forum section: electronics / projects

author: ricko_uk
author_rank: Super Contributor
timestamp: August 14, 2021, 02:12:25 pm
content: Hi,can anybody suggest either an app/software for Windows, a script or a way to duplicate a folder with its entire content a large number of times (like 10,000 or more)?I tried few scripts from various forums but none of them seem to be working.Thank you

author: Zero999
author_rank: Super Contributor
timestamp: August 14, 2021, 02:24:11 pm
content: What operating system are you using?

author: ricko_uk
author_rank: Super Contributor
timestamp: August 14, 2021, 02:43:51 pm
content: Windows 10 (I updated it now in the OP)Thank you Zero999

author: NiHaoMike
author_rank: Super Contributor
timestamp: August 14, 2021, 05:05:38 pm
content: Windows 10 has WSL, therefore a bash script should work. Or you can use Powershell if that's what you prefer.

author: mariush
author_rank: Super Contributor
timestamp: August 14, 2021, 05:09:08 pm
content: Make an archive of the folder you want duplicated ... can be an archive without compression like TAR for example, or ZIP / 7zip with no compression or ultrafast compressionGenerate the numbers you want using whatever website or script you can find, for example try :https://www.reformattext.com/sequential-number-generator.htmTell the website or script to print one number per line, with some sequence of characters before and after each number.You get something like this :Code:abc00001xyzabc00002xyzabc00003xyzBasically, you want to form the command line to unpack the archive on each line, by replacing text before the number with something, and the text after number with something.For example, assuming the archive is C:\temp\temp.tar and you want it extracted in C:\temp\00001\ , C:\temp\00002 and so on, you want each line to look like this :Code:7z.exe x -o"C:\temp\00001" "c:\temp\temp.tar"x means extract, keeping the folder structure inside the archive intact-o means specify output folderuse "" if there's spaces in the file or folder paths.So you paste those numbers in notepad or any other text editor, and do two search and replace commandsSearch for :abcReplace with :7z.exe x -o"C:\temp\Search for :xyzReplace with :" "c:\temp\temp.tar"and your text file becomes :Code:7z.exe x -o"C:\temp\00001" "c:\temp\temp.tar"7z.exe x -o"C:\temp\00002" "c:\temp\temp.tar"7z.exe x -o"C:\temp\00003" "c:\temp\temp.tar"Now select the file with a .BAT extension in the same folder with 7z.exe from 7zip (you can copy it from program files somewhere else, like a temporary folder) and run the batch file and the archive will be unpacked as many times as you have lines in the batch file.(In notepad, you do File > Save as ... select any file type, then type your file name and add .bat at the end, and the file will be saved as a batch file .. otherwise it's saved as a .TXT file)Or use something else under linux or mac if that's what you're using.Test it with 5-10 numbers and if all looks good to you, do it with 10k or whatever.

author: Ian.M
author_rank: Super Contributor
timestamp: August 14, 2021, 06:03:57 pm
content: That's gratuitously dumb. Even in a CMD batch script you can increment a variable withset /a. Seehttps://ss64.com/nt/set.html#expressions

author: mariush
author_rank: Super Contributor
timestamp: August 14, 2021, 06:21:16 pm
content: It may be dumb, but it's super easy.Doing a search and replace twice is super easy and idiot proof.I've worked with computers since the Turbo Pascal days and 486 days and I never bothered to learn batch file programming, and even if I learn it now, using it so little I'll soon forget it..For me it's much more intuitive to write a few lines in a language like php and run the php script from command line, but if the job can be done with two search and replace commands in notepad and it works .... :Code:$handle = fopen("c:/temp/batch.bat","w"); // open batch file for writingfor ($i=1;$i<=10000;$i++) {$nr = str_pad($i,5,"0",STR_PAD_LEFT); // put up to 5 "0"characters in front of our number, converted into stringfwrite($handle, "7z.exe x -o\"C:\\temp\\$nr\" \"c:\\temp\\temp.tar\"\r\n"); // " and \ characters have to be escaped by placing \ in front}fclose($handle);

author: Renate
author_rank: Super Contributor
timestamp: August 14, 2021, 06:33:05 pm
content: Mmm, just wondering why you want 10,000 of the same thing.Is this for some distribution that will be further customized?

author: Ian.M
author_rank: Super Contributor
timestamp: August 14, 2021, 06:47:27 pm
content: @mariush,Maybe so, but CMD scripting is the lowest common denominator that you can be certain will be available on any Windows PC. There are a few gotchas if you go back before Win XP but otherwise its pretty much identical from then to now.@all,Here's a batch script that counts:Code:@echo offset /a limit=1000set /a loopvar=0:loopecho Do something with %loopvar%rem your code hereset /a loopvar+=1if %loopvar% leq %limit% goto looprem cleanupset limit=set loopvar=

author: Renate
author_rank: Super Contributor
timestamp: August 14, 2021, 07:49:19 pm
content: Am I missing something?Are leading zeroes even possible in a straight .bat file?

author: james_s
author_rank: Super Contributor
timestamp: August 14, 2021, 07:51:19 pm
content: It would be trivial to do this with a Python script.

author: EPAIII
author_rank: Super Contributor
timestamp: August 14, 2021, 08:25:18 pm
content: I once did that. Not 10,000 copies, but until the hard drive was full.Why? I was leaving a job under bad circumstances. I had installed several programs on my office computer and I did not want to leave them as that employer did not have any rights to use them: I did have that right. I left all the data files that I had created with them, but erased the programs. But it was very easy to un-do that so I wanted to over write the entire hard drive.I used a batch file employing DOS commands to copy a bunch of files until the drive was full. It only took a minute to write and set in motion.I have not written or used a batch file (.bat) in years, but it appears that you still can via the command prompt window which is still in Windows 10. Here is how a loop is written:http://www.trytoprogram.com/batch-file-for-loop/You can easily use values of 1 and 10000 in that. The rest is a simple command to copy that directory.Quote from: Renate on August 14, 2021, 06:33:05 pm

author: Ian.M
author_rank: Super Contributor
timestamp: August 15, 2021, 12:43:38 am
content: Quote from: Renate on August 14, 2021, 07:49:19 pmYes, and it isn't even that tricky to do. Conceptually, you need to concatenate a string of sufficient leading zeros with the number then slice out the the desired number of digits from the end of the resulting string. In practice, this code:Code:set zplv=0000%loopvar%set zplv=%zplv:~-5%echo Do something with zero padded no. %zplv%will do it. Simply add it as 'payload' to the loop of the batch script I posted above.@EPAIII,Unfortunately the batch FOR command was originally only intended to repeat a single command for all or a subset of the files in a directory. There is no NEXT command. Everything else it can do has been bolted on to that original functionality, resulting in a slow and fugly mess with arcane syntax if you need to execute multiple commands. Look at this:https://ss64.com/nt/delayedexpansion.htmland you'll see why I went for a simple IF and GOTO above!Believe me, it wasn't from lack of knowledge of FOR loops, as I've been using them for stuff like this since the bad old days of DOS, when the only way to do the above was to have a FOR loop process a list for each digit, something like:Code:for %%a in (0 1 2 3 4 5 6 7 8 9) do for %%b in (0 1 2 3 4 5 6 7 8 9) do for %%c in (0 1 2 3 4 5 6 7 8 9) do call %0 /loopbody %%a%%b%%cthen as CALL couldn't use GOTO labels, at the top of your batch file you'd test parameter %1 and if it was /loopbody you'd jump to the loop body. If you wanted to stop before 999, you'd need to test %2 in the loop body code, and once it reached the desired limit, set a flag to skip the loop body thereafter. Fortunately the NT family command processor was available as an extra for Win9x, (Win95Cmd) so I was able to escape from raw DOS batch syntax sooner than many others.

author: BradC
author_rank: Super Contributor
timestamp: August 15, 2021, 01:57:11 am
content: I'm not sure why it needs to be so complicated :for /L in %G in (1,1,10000) DO xcopy /E /I sourcedir destdir-%gIf using in a batch file make the %g %%gIf you are worried about the number being consistent length, start with (10000,1,20000) and number them 10000-20000.

author: ricko_uk
author_rank: Super Contributor
timestamp: August 15, 2021, 10:40:23 am
content: Thank you a

author: Zero999
author_rank: Super Contributor
timestamp: August 15, 2021, 01:45:33 pm
content: Quote from: Ian.M on August 15, 2021, 12:43:38 amI used to use QBASIC for a lot of things, which can now be done using batch files.

author: Jan Audio
author_rank: Frequent Contributor
timestamp: August 15, 2021, 04:44:19 pm
content: Why dont you use the CTRL+A CTRL+C CRTL+V method ?It doubles each time u use it, and you have 10.000 faster then you read this topic.The problem is naming, copy of copy of copy of file.exe

author: bson
author_rank: Unknown
timestamp: August 17, 2021, 04:56:42 pm
content: Install the git bash shell and do something like,Code:ncopies=10src_folder=filesdst_folder_prefix=more_files_for n in $(seq 1 ncopies) ; do cp -r $src_folder ${dst_folder_prefix}${n} ; done