Page 1 of 1

Need help

Posted: 23 Jun 2011 11:44
by yabba
I have no experience whatsoever in .bat, but I do know c++ a bit so if someone could just tell me how to do the following I think I can piece it together myself.

1. Randomly select a string (file name) from preset strings
2. Check if a file with a specific name exists in a folder
3. Move a file (if not possible, then how to copy and delete files)

All help appreciated, and thanks in advance.

Re: Need help

Posted: 23 Jun 2011 11:59
by Acy Forsythe
Well if you had a string:

Set "String=My Data"

And another string:

Set "String2=File"

You could concatenate them:

Set "String3=%String1%%String2%"

But then you would need a file extension:

Set "Ext=log"

And you would need to add that:

Set "String3=%String3%.%Ext%"

So now String3 = My DataFile.log

But you want to now see if it exists and then move it:

IF EXIST "C:\pathtomyfile\%String3%" MOVE /Y "C:\pathtomyfile\%String3%" "C:\Newpath\Newfilename"

You can of course continue to use %string3% for the new file name, or specify a completely new filename.

If you don't know the original location of the file, it get's more complicated.... but there is an excellent thread for finding the location of a file/directory right here: viewtopic.php?f=3&t=1978

Re: Need help

Posted: 23 Jun 2011 12:23
by yabba
The directory is known, thanks.

Meddling with this, I figure I need a way to check a file if it has a certain part right.

What I mean is I need to check for files starting with bgm_ and extension being .mp3

bgm_*.mp3 ?

edit
Does it do any harm to use DEL on a file that doesn't exist?

edit2
how to delete without prompt?

Re: Need help

Posted: 23 Jun 2011 12:50
by Acy Forsythe
DEL /Q will delete without a prompt.

It doesn't hurt to delete a file that doesn't exist, and you can check for files and delete files with a wildcard, but I'm not sure about moving them. Though COPY will work instead of MOVE but I don't think you can change the filename. Then just DEL /Q bgm_*.mp3

Deleting a file that doesn't exist will just tell you it doesn't exist.

You can to really get into specifics and write a batch file smart enough to find the files you are looking for, move the ones you want, delete the ones you don't create an html index of the files that are left and link to them so you can pull up MediaPlayer to listen to them, etc...

But if you're just looking for a quick and dirty get it done solution:

Code: Select all

@Echo Off

CD "C:\Known Directory"

Copy bgm_*.mp3 "C:\My New Folder\Mp3 Files\"
DEL /Q bgm_*.mp3
exit /b



That will do what you want.

Re: Need help

Posted: 23 Jun 2011 12:55
by yabba
Thanks for your help, its working now. Though I have one trivial question, why does %random% work like a clock?

It seems to return a slowly increasing number, depending on time..

Re: Need help

Posted: 23 Jun 2011 13:37
by Acy Forsythe
I beleive it's based on time, I don't think it is actually truly random. I could be misunderstanding, but It seems to start at 0 and increment to 32767 and start over. So it won't always be a higher number it will eventually start back over at 0.

Here's a good discussion on it though and how to get a more random random:

viewtopic.php?f=3&t=1817

Re: Need help

Posted: 23 Jun 2011 13:53
by yabba
Thanks again.

Re: Need help

Posted: 24 Jun 2011 03:18
by orange_batch
yabba wrote:I have no experience whatsoever in .bat, but I do know c++ a bit so if someone could just tell me how to do the following I think I can piece it together myself.

1. Randomly select a string (file name) from preset strings
2. Check if a file with a specific name exists in a folder
3. Move a file (if not possible, then how to copy and delete files)

All help appreciated, and thanks in advance.

Not sure why you're doing it this way in the first place, but this will repeat randomly until finished... This is an example of pseudo-arrays and a bit of management.

Code: Select all

@echo off&setlocal enabledelayedexpansion

set "destination=out"
set files=8
set "file1=bgm_1.mp3"
set "file2=bgm_2.mp3"
set "file3=bgm_3.mp3"
set "file4=bgm_4.mp3"
set "file5=bgm_5.mp3"
set "file6=bgm_6.mp3"
set "file7=bgm_7.mp3"
set "file8=bgm_8.mp3"

:next
set /a choice=%random%%%%files%+1
if exist "%destination%\!file%choice%!" (echo:!file%choice%! already exists.) else move "!file%choice%!" "%destination%"&&echo:Moved !file%choice%!.
set file%choice%=
call :merge
if defined counter (goto next) else echo:Done.
pause
exit

:: Functions are below this point.
:merge
set counter=
for /l %%x in (1,1,%files%) do (
if defined file%%x (
set /a counter+=1
if !counter! NEQ %%x set file!counter!=!file%%x!&set file%%x=
))
set files=%counter%
exit/b

Beware that this is for files in the same directory as the batch file (observe the if exist "%destination%\!file%choice%!"). Use FOR to modify the path using %~dpnx notation otherwise.