Computing.Net > Forums > Disk Operating System > batch file to get subset of dir?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

batch file to get subset of dir?

Reply to Message Icon

Name: mrwright
Date: December 22, 2004 at 10:25:51 Pacific
OS: 98SE
CPU/Ram: P3 with 256 MB ram
Comment:

I have a large directory of .bmp files and each day I need to randomly select 20 of these files and rename them in a consistent way (e.g., pic1.bmp, pic2.bmp). I would appreciate any help in writing a batch file to do this.

If necessary, I can pre-process the file names with something like CKrename so that they are labelled sequentially (e.g., 1frog.bmp, 2cow.bmp, etc.) but I'm still not sure how to randomly select and rename 20 of the files.

Thanks,
Sean



Sponsored Link
Ads by Google

Response Number 1
Name: wizard-fred
Date: December 23, 2004 at 07:08:53 Pacific
Reply:

It seems to be an undefined task. If you rename 20 a day and not remove them, are they then eligible to be selected and renamed at a subsequent time? If you preprocess and rename all the files, is there a necessity to retain their original identify? Is this a fixed pool or does files get added and deleted from this directory?

I would get a directory listing, then copy and rename the random selection to another directory. Actually renaming is unnecessary since you would have the list in selected order.


0

Response Number 2
Name: Mechanix2Go
Date: December 23, 2004 at 08:42:37 Pacific
Reply:

wizard-fred,

Well said.

I've been working on this for a few hours.

I finally came up with a kludge to generate psuedo random strings 8 chars long since DOS doesn't have the %RANDOM% var.

Like you I think subdirectories will help.

Like you I think that if the group of BMPs ebbs and flows, this could be a real tough one.

Since he's already using a 3rd party renamer, he may as well use that or another 3rd party to do the random choosing.

M2


0

Response Number 3
Name: Mechanix2Go
Date: December 23, 2004 at 22:36:52 Pacific
Reply:

mrwright,

I've got this about 75% figured out and it's a real kludge.

I don't suppose you'll consider doing it in NT/2K/XP. It would be MUCH easier.

M2

Mechanix2@Golden-Triangle.com


0

Response Number 4
Name: mrwright
Date: December 30, 2004 at 16:30:00 Pacific
Reply:

Hi all,

Thanks for the advice to date. I know this is going to be a "real kludge" so perhaps I can supply a little more information.

I have custom-made, legacy, scientific software that I am using for monkey behavioral testing. This program displays pictures to the monkey on a touchscreen. You have to specify a text file (e.g., pics.txt) that has a list of the picture file names. Unfortunately, there are a number of other parameters that must be in this text file, so the easy solution of just making a number of different text files with randomly chosen images is out. My idea is to leave this text file static using pic1.bmp, pic2.bmp, ..., pic20.bmp as a consistent naming scheme. So each time, the program "expects" the same images, but if I rename different image files to pic1, pic2, etc., then they will appear instead and the program will be none the wiser. This is where I am stuck. I have a collection of 20,000 images (that have no consistent naming scheme) in a single directory. I could manually change them like this:

ren orb.bmp pic1.bmp
ren apple.bmp pic2.bmp
etc.

but I'm trying not to do it manually. So the first question is can I assign the 8.3 filename to a variable (String1) so that I could have a batch statement like:
ren $String1 pic.bmp ?

If this is possible, the next question is whether I can randomly choose files for the string variables? I don't know about random numbers in batch files, so any help would be appreciated. The reason I mentioned pre-processing in the original post is because I thought it might be easier to select an individiual file if it was prefixed by a number. So, for example, if $RAND was 197, then dir $RAND*.bmp would bring up 197cow.bmp which would be a random image that could then be passed to a string variable. As far as I know, renaming programs like CKrename cannot randomly select a subset of a directory to rename.

wizard-fred, you bring up a good question about whether there should be random selection with or without replacement, but if I can get the above to work then I could also move the files like this:
move $String1 ./oldPics

Thanks again for your help,
Sean


0

Response Number 5
Name: wizard-fred
Date: December 31, 2004 at 05:42:51 Pacific
Reply:

I would do the following:

1 - Put the file names in a list
2 - Generate the random number list
3 - Copy Rename the selected files in list to a work directory.
4 - Move the selected files to ./oldPics
5 - Save the list if needed.

I'm not a great batch programmer, but I use a combination of BASIC and DOS commands to create my batch commands. (Part of the reason is the BASIC is an old 16-bit DOS version that has limited Win 98 file name compatibility.)



0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: January 2, 2005 at 02:45:26 Pacific
Reply:

Hi Sean,

Thanks for the additional info.

The reason I ask if NT/2k is an option is that they have a built-in random var.

Leaving alone for the moment the rather thorny issue of "true" randomness, are we stuck with win98?

If so, I'll muddle on with the kludge I started.

Happy 2005

M2



0

Response Number 7
Name: mrwright
Date: January 3, 2005 at 08:34:47 Pacific
Reply:

M2,

I didn't realize NT/2K had the ability to generate random numbers. In that case, I could use a machine running 2K to select the subset of pictures and then copy them to the 98 machine (or if I'm really lazy I could have a 98/2K dual-boot and just switch OSes.

Instructions on a 2K batch file using random vars would be appreciated--I will look into it in the meantime.

Thanks,
Sean


0

Response Number 8
Name: Mechanix2Go
Date: January 5, 2005 at 22:40:49 Pacific
Reply:

Hi Sean,

I hadn't forgotten about this. I somehow failed to add it to my watch list and had to go searching.

Using 2k will help.

Please clarify re the "selected BMPs", will it suffice to name them: 1.bmp 2.bmp ... 20.bmp?

Or will they need something like 1name.bmp 2name.bmp etc?

M2


0

Response Number 9
Name: Michael D
Date: January 6, 2005 at 07:12:29 Pacific
Reply:

Hi !

Ive read the main question & skiped over the rest...

the program "LupasRename 2000" will do what (i think) you want..
its available here : http://www.azheavymetal.com/~lupasrename/news.php

all the best !



0

Response Number 10
Name: mrwright
Date: January 6, 2005 at 14:10:24 Pacific
Reply:

For the sake of completeness, I thought I should post the batch file that I finally got to solve this problem. As M2 noted, it's easier in NT batch because you can get random numbers via %random%. First I numbered all the images like 1aBanana.bmp, 2aLob.bmp, etc. with a number followed by "a." Then the command dir %rand%a*.bmp will find a single picture (e.g., 10aHouse.bmp). The character "a" ensures that the dir command will not find more than one file (i.e., dir 10*.bmp would find both 10aHouse.bmp and 100aZkey.bmp, but dir 10a* will only find 10aHouse.bmp). The selected picture is then moved to a folder for a specific date given by %userDate% and a copy of this image overwrites old1.bmp, which is the static file that the legacy program is expecting and writes a line to files.txt indicating what image has been assigned to the static pic. A problem arises as images are selected and removed each day because the random number generated might correspond to an image that has already been moved. So I used an if-else statement by testing whether the file exists in the directory. If it does, the move and copy steps are performed and a counter variable is incremented; if not, nothing happens. (Incidentally, I borrowed the clever idea of using the goto statement to allow multiple commands to be performed after an if statement.) The same trick is used to create a do-while loop that goes until the counter indicates that the correct number of pictures have been selected.

Thanks for the advice.

set /p userDate=Enter the date these pictures are going to be used (MM-DD):
mkdir "%userDate%"
echo You have selected to use these pictures on %userDate% >> "./%userDate%/files.txt"
set /a count = 0

echo Beginning DO WHILE loop for old pictures
:do
set /a rand =" %random% %% 17650 + 1"
if NOT EXIST %rand%*.bmp GOTO endif
set /a count = "%count%+1"
copy /b %rand%a*.bmp "../old%count%.bmp"
move %rand%a*.bmp "%userDate%"
echo pic # %rand% = old%count%.bmp >> "./%userDate%/files.txt"
echo Count %count%
:endif

if NOT %count% == 10 goto do


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: batch file to get subset of dir?

Batch file to Create and Open Dir www.computing.net/answers/dos/batch-file-to-create-and-open-dir/14596.html

Batch file to run on log off! www.computing.net/answers/dos/batch-file-to-run-on-log-off/14751.html

Making a .bat file to get input NT www.computing.net/answers/dos/making-a-bat-file-to-get-input-nt/12892.html