Home > database >  Windows batch file - How to shuffle and unshuffle files using variable inputs
Windows batch file - How to shuffle and unshuffle files using variable inputs

Time:09-11

EDIT: Updated to be more clear and to clarify that I only want to shuffle files 001 to 049 and leave file 050 as 050 and added an "Ideal End Goal Concept" at the end of this post

I'm looking for a way to shuffle 49 files in the current directory with my Windows batch file. All of the files are named .001, .002, etc. all the way to .049 using 9 or 10 numbers entered as variables to the batch file. I need to be able to use the same 9 or 10 numbers entered as variables to be able to "unshuffle" the files back to their original state using a seperate batch file. Any ideas and code examples to do this would be greatly appreciated.

Here is my batch file command line example:

I have 50 files in the folder named testfile.exe.001 to .050 (I only want to shuffle 001 - 049)

shuffle.bat testfile.exe 8 5 2 9 4 1 3 7 6

Making sure that I don't use the same number twice and not in the same place (i.e. don't put "1" as the first variable or it will just rename .001 to .001) so that the first 9 files are shuffled.

I then do something like this:

rem temporarily renaming .001 - .050 files to .001a - .050a to prevent collisions
ren %1.0?? %1.0??a
ren %1.001a %1.00%2
rem which renames testfile.exe.001a to testfile.exe.008
ren %1.002a %1.00%3
rem which renames testfile.exe.002a to testfile.exe.005
ren %1.003a %1.00%4
rem which renames testfile.exe.003a to testfile.exe.002
rem etc. all the way up to %1.009a

Then I figured, I could do the same with files .011 - .019, then with files .021 - 0.29, etc. and that works, but it doesn't shuffle the files across the entire span of 49 files. For example .005 could not become .045, etc.

To unshuffle, I would have to enter the same 9 or 10 number variables into the unshuffler batch file which just does everything in reverse. So without those 9 or 10 numbers (which act as a sequence or 'kind of' password), the files could not be restored to their original names.

Is there a way to improve my code or a better way to shuffle these 50 files using batch file variables (numbers) which would cause a shuffle across the entire span of 49 files and be totally reversible using the same 9 or 10 numbers as variables? Thanks for any help!

Ideal End Goal Concept:

To be able to shuffle these 49 files with all of these being true:

  • Ability to input a code on the command line (varible(s)) which uniquely affects and is the key to the shuffling order and allows unshuffling
  • Every file's original order has been changed to another order
  • No two files from the original order are one after the other in the shuffled order
  • All files can be shuffled across the entire 001-049 set (e.g. 005 can become 045, 032 can become 016, etc.)

CodePudding user response:

Your question is not clear. However, I "half-understood" the part with 9 files, so here it is my partial solution.

This is shuffle.bat:

@echo off
setlocal

set "basefile=%1"
set "i=0"
md "renamed"

:loop
shift
if "%1" equ "" goto endLoop

set /A i =1
move            
  • Related