How to duplicate randomly some files ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
budhax
Posts: 63
Joined: 09 Oct 2006 12:25

How to duplicate randomly some files ?

#1 Post by budhax » 05 Sep 2010 07:12

Hello,
Let's a folder containing some PDF files:

Code: Select all

GeM-13117.pdf
GeM-14404.pdf
GeM-16853.pdf
GeM-17914.pdf
GeM-20255.pdf
GeM-20559.pdf
SOS-647272.pdf
SOS-665824.pdf
SOS-668677.pdf
SOS-674574.pdf
SOS-676535.pdf


I would write a batch script duplicating randomly some files.
The duplicated files will be ended by "_".
EXAMPLE: 4 files duplicated randomly.

Code: Select all

GeM-13117.pdf
GeM-14404.pdf
GeM-16853.pdf
GeM-16853_.pdf
GeM-17914.pdf
GeM-20255.pdf
GeM-20559.pdf
SOS-647272.pdf
SOS-647272_.pdf
SOS-665824.pdf
SOS-665824_.pdf
SOS-668677.pdf
SOS-668677_.pdf
SOS-674574.pdf
SOS-676535.pdf


Any idea how to start this script?
Thanks

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: How to duplicate randomly some files ?

#2 Post by orange_batch » 05 Sep 2010 07:41

Ah boy, randomization.

>Count and log .pdfs in folder.

>Get number of .pdfs to copy by generating a random number from 1 to length of log using modulus.

>Get .pdf to copy by generating a random number from 1 to length of log using modulus.

Either more easily:
-If undefined log variable is selected, repeat without any decrements.
-If defined, process that .pdf and erase it from the log, decrement number of .pdfs to copy and repeat.

Or more properly:
-Process that .pdf and erase it from the log, merge log, decrement number of .pdfs to copy and repeat.

This is the proper method with log merging. You're lucky I felt up to this one, I had already written a couple of the routines.

Code: Select all

@echo off&echo:&color B&title Copy Random Number of Files Randomly by orange_batch&setlocal enableextensions,enabledelayedexpansion

:: Customizable options.
set "_targetfolder=C:\...\Target Folder"
set "_suffix=_"
set "_filetype=.pdf"

:: Log files in target folder.
for %%x in ("%_targetfolder%\*%_filetype%") do (
set /a _loglength+=1
set "_log!_loglength!=%%x"
)

:: Get random number of files to copy.
set /a _repeats=%random%%%%_loglength%+1

:l_repeat

:: Copy a random file. Erase file's log for log merging.
set /a _any=%random%%%%_loglength%+1
echo:f|xcopy "!_log%_any%!" "!_log%_any%:~0,-4!%_suffix%%_filetype%" /v/q/g/h/k/x>nul
set _log%_any%=

:: Merge log.
set _logcounter=
for /l %%x in (1,1,%_loglength%) do (
if defined _log%%x (
set /a _logcounter+=1
set "_log!_logcounter!=!_log%%x!"
if !_logcounter! NEQ %%x set _log%%x=
))
set /a _loglength-=1

:: Decrement number of repeats. Repeat if number is greater than 0.
set /a _repeats-=1
if %_repeats% NEQ 0 goto l_repeat

echo:   Complete.
exit/b

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: How to duplicate randomly some files ?

#3 Post by !k » 05 Sep 2010 09:39

budhax

Code: Select all

@echo off
setlocal enableextensions enabledelayedexpansion
for /f "delims=" %%i in ('dir /a-d/b *.pdf') do if !random:~-1! LSS 3 copy /b "%%i" "%%~ni_%%~xi"


or w/o enabledelayedexpansion

Code: Select all

@echo off
setlocal enableextensions
for /f "delims=" %%i in ('dir /a-d/b *.pdf') do call :r "%%i"
goto :eof
:r
if %random:~-1% LSS 3 copy /b %1 "%~n1_%~x1"
goto :eof

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: How to duplicate randomly some files ?

#4 Post by ghostmachine4 » 05 Sep 2010 19:14

download gawk for windows, then use this script

Code: Select all

BEGIN{
  howmany = 4  #how many files you want to randomize
  #random seed
  srand(systime())
  # Get all pdf files into array
  for(i=1;i<=ARGC;i++){  filename[++c]=ARGV[i]  }
  while(1){
    RAND=int(rand()*c+1)  # generate a random number
    if (! (RAND in r ) ){
       r[RAND]   #store random number already generated
       fn2=filename[RAND]
       sub(/\.pdf/,"_.pdf",fn2)      # replace with "_"
       cmd="ren \042"filename[RAND]"\042 \042" fn2
       print cmd
       #system(cmd) #uncomment to do actual renaming
       z++
       if(z==howmany){
         break
       }
    }
  }
}


save as myscript.awk and on command line

Code: Select all

C:\test>gawk -f myscript.awk *pdf
ren "SOS-674574.pdf" "SOS-674574_.pdf
ren "SOS-668677.pdf" "SOS-668677_.pdf
ren "GeM-17914.pdf" "GeM-17914_.pdf
ren "012345(2).pdf" "012345(2)_.pdf

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: How to duplicate randomly some files ?

#5 Post by orange_batch » 06 Sep 2010 10:32

!k wrote:

Code: Select all

@echo off
setlocal enableextensions enabledelayedexpansion
for /f "delims=" %%i in ('dir /a-d/b *.pdf') do if !random:~-1! LSS 3 copy /b "%%i" "%%~ni_%%~xi"


Cool idea, I think it's less random though being 30% chance. It also assumes 0 is okay.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: How to duplicate randomly some files ?

#6 Post by !k » 06 Sep 2010 17:42

orange_batch wrote:30% chance. It also assumes 0 is okay.

Code: Select all

@echo off
setlocal enableextensions
set /a cnt=0
:loop
for /f "delims=" %%i in ('dir /a-d/b *.pdf') do call :r "%%i"
if %cnt% LSS 1 goto :loop
goto :eof
:r
if %random:~-1% LSS 3 (copy /b %1 "%~n1_%~x1" &set /a cnt+=1)
goto :eof
30% chance with at least 1 file ;)

budhax
Posts: 63
Joined: 09 Oct 2006 12:25

Re: How to duplicate randomly some files ?

#7 Post by budhax » 13 Sep 2010 13:04

Thanks all, it works fine.
This is exactly what I needed.

Post Reply