Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to randomly rearrange lines in a text file using a batch file

I am creating a code that strips through different MAC addresses randomly, but cannot figure out how to do this. My thought on how to approach this is to randomize or rearrange the order of the MAC address in the text file with this script, but I cannot figure out how to do this with a batch file. How this will work is that it will read "maclist.txt", then create a new temp file with the random order "maclist_temp.txt", that will be the rearranged file. Then, it will pull this randomized file in order.

I have tried Google and searching the web, but I haven't found anything too useful. I'm still actively looking, but any advice would be extremely useful.

Something as simple as extracting and deleting a random line and then adding to the bottom might work. Randomization would be better though, but I want to keep the original list. Something like:

  1. Make a temp copy of maclist.txt called maclist_temp.txt
  2. Take one random MAC address, remove it from maclist_temp.txt
  3. Readd it to the bottom

That is all I want, but any suggestions are welcome.

like image 782
computerlife22 Avatar asked Sep 16 '25 19:09

computerlife22


1 Answers

You may try this batch file to help you to shuffle your maclist.txt. The usage of the batch code is

C:\> type list.txt | shuffle.bat > maclist_temp.txt

Here are the contents of shuffle.bat:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET TmpFile=tmp%RANDOM%%RANDOM%.tmp
TYPE NUL >%Tmpfile%
FOR /F "tokens=*" %%i IN ('MORE') DO SET Key=!RANDOM!!RANDOM!!RANDOM!000000000000& ECHO !Key:~0,15!%%i>> %TmpFile%
FOR /F "tokens=*" %%i IN ('TYPE %TmpFile% ^| SORT') DO SET Line=%%i&ECHO.!Line:~15!
::DEL %TmpFile%
ENDLOCAL

After issuing the above command, maclist_temp.txt will contain a randomized list of MAC addresses.

Hope this helps.

like image 83
Dale Avatar answered Sep 19 '25 19:09

Dale