Computing.Net > Forums > Programming > Batch that grabs a random line?

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 that grabs a random line?

Reply to Message Icon

Name: Nicktendo
Date: August 26, 2008 at 19:45:53 Pacific
OS: WinXP
CPU/Ram: n/a
Product: n/a
Comment:

I know this question has been asked before, but I couldn't find the answer I was quite looking for. So, sorry if I annoy anyone with this!

I've got some large, list-like text files, and I'd like to be able to grab a random line from that list. The thing is, the file lengths aren't static. They're kind of an ever-changing inventory.

Like,

line1
line2
...
line9645
line9645
etc...

Id there a batch file method for this kind of larger text file? I'd like to be able to store it in a variable. (Also, I'm kind of new to this. If that matters.)
Any help is appreciated, thanks.




Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: August 26, 2008 at 21:40:03 Pacific
Reply:

The environment variable %random% in windows 2k,xp and higher(I think) "expands to a random decimal number between 0 and 32767."

Unfortunately you can't specify a range, this is fine if you have a static number as you can alot chunks (eg 0 - 100=1...) and/or add %random% to %random% if you need more than 32767.

It's probably best to find a third party tool that you can specify a range with. Not that it can't be done, it's just you may be waiting for it to go in circles for a while until a number hits.


The below is veeery sloooooow and I haven't bothered to set up anyway of making sure the it's possible that every line could have been picked, In fact the below example can't pick anything after 32767. It's just sort of "proof of concept" I still recommend trying to find a third party utility that you can specify a range with, the below could be modified with such a utility to pick within the range from 1 to %cnt%.

:start
set cnt=0
for /f "tokens=*" %%g in (yourfilename) do set /a cnt+=1
:reset
set ran=%random%
set /a ran-=1
if %ran% gtr %cnt% goto reset
call :grabline
goto otherstuff
:grabline
for /f "skip=%ran% tokens=*" %%h in (yourtextfile) do set yourvariable=%%h&&goto :eof
:otherstuff
echo %yourvariable%
pause
set cnt=
set ran=


0

Response Number 2
Name: ghostdog
Date: August 26, 2008 at 23:01:27 Pacific
Reply:

See http://www.computing.net/answers/pr...
and see if its what you want


0

Response Number 3
Name: Mechanix2Go
Date: August 26, 2008 at 23:36:53 Pacific
Reply:

I got it partly done but for some reason which eludes me, it seems to 'skew'.

Maybe somebody can find the glitch.

::============================

:: pad line NUM to xxx so we can grab RAND line
:: gen 3 digit RAND

@echo off
setLocal EnableDelayedExpansion

call :gen RAND3

:main
for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
call :pad3
if !RAND3! equ !LN! echo lucky line is: %%a
)
goto :eof

:gen RAND3
:loop
set RAND3=!RANDOM:~-3!
if !RAND3! gtr !N! goto :loop
echo lucky number is !RAND3!
goto :eof

:pad3
if !N! lss 10 (
set LN=00!N!
) else (
if !N! lss 100 (
set LN=0!N!
) else (
set LN=!N!
)
)
goto :eof


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: Nicktendo
Date: August 27, 2008 at 00:22:55 Pacific
Reply:

Thanks for the input, all. I've used bits and pieces from a lot of things I've seen here and elsewhere, but I'm still learning.

I might as well ask a few other questions while I'm amongst those who know what they're doing.

I've already written a batch file that creates several text files of all the .WAV files in several drives and directories dictating what "genre" they're in. (like Alternative.txt, Metal.txt, etc.)

So what I'm aiming to do is set up a ratio system. I'm generating a random number between 1 and 100 by %RANDOM:~-2%. (I found this somewhere. I don't know if there are problems with it, like if it doesn't return every possibility.) Using that number, I'll set up comparison parameters, and depending on where the number lands I'll grab a random line from a non-static text file. (Like, 1-30 will grab from Alternative.txt to ensure that 30% of the music that's appended to the final Library.txt is from the Alternative genre. Follow?)

I'm actually writing this for a radio automation system I maintain.

Once again, any help (maybe with explanation?) is awesome. Thanks again.


0

Response Number 5
Name: Razor2.3
Date: August 27, 2008 at 04:44:22 Pacific
Reply:

Once upon a time, I had a command script that I'd use as an alarm clock; it'd play one of my .mp3 files at random. I wish I knew what happened to it.

The basic workings of it went something like this (from memory):

SET cnt=0
FOR /F %%a IN ('dir music /s /b') DO SET /a cnt+=1

SET /a num=%RANDOM% %% %cnt% + 1
FOR /F "tokens=1* delims=:" %%a IN ('dir music /s /b ^| findstr /n "\\" ^| findstr "^%num%:"') DO START mplayer2 /play /close "%%b"

...Yeah, something like that.

Also, I'm starting to like bolded red text for my code. It stands out from the blue theme of this site's redesign. I'll probably pick a darker red, though.

EDIT: Nicktendo: (Like, 1-30 will grab from Alternative.txt to ensure that 30% of the music that's appended to the final Library.txt is from the Alternative genre. Follow?)
Not really. Why not just run the script for 30 times on Alternative.txt, then XX number of times on Metal.txt?


0

Related Posts

See More



Response Number 6
Name: ghostdog
Date: August 27, 2008 at 08:41:27 Pacific
Reply:

here's a vbscript


CONST FOR_READING = 1
Set ofso = CreateObject("Scripting.FileSystemObject")
Set ofile = ofso.OpenTextFile("c:\test\text1.html",FOR_READING)
arrFile = Split(ofile.readall,vbCrLf)
Randomize
i = Int((Rnd * UBound(arrFile)))
arrLine = Split(arrFile(i),",")
For i=0 To UBound(arrLine)
WScript.Echo arrLine(i)
Next


save it as script.vbs and on command line


c;\test> cscript /nologo script.vbs


0

Response Number 7
Name: Nicktendo
Date: August 27, 2008 at 11:46:51 Pacific
Reply:

Razor2.3:
EDIT: Nicktendo: (Like, 1-30 will grab from Alternative.txt to ensure that 30% of the music that's appended to the final Library.txt is from the Alternative genre. Follow?)
Not really. Why not just run the script for 30 times on Alternative.txt, then XX number of times on Metal.txt?

Because I don't want to have to specify how many tracks from each genre I want. I have to do that now. That's why I'm writing this script. I just want to set up a ratio, so when I generate a random number between 1 and 100, if it lands inside 1-30, it'll grab a random track from Alternative, and if it lands between 31-40, it will grab from Metal.txt, so in the end, I'll have a RandomLibrary.txt with approximately 30% Alternative songs, 10% metal songs, etc.

@ghostdog
Is it possible to put that in my batch file and have the line stored in a variable, so I can then write it to my RandomLibrary.txt? I still know very little about all this.


0

Response Number 8
Name: Razor2.3
Date: August 27, 2008 at 14:39:33 Pacific
Reply:

Nicktendo: Because I don't want to have to specify how many tracks from each genre I want. I have to do that now. That's why I'm writing this script. I just want to set up a ratio, so when I generate a random number between 1 and 100, if it lands inside 1-30, it'll grab a random track from Alternative, and if it lands between 31-40, it will grab from Metal.txt
You're quickly heading out of batch's logic territory. Your best bet would be to combine Alternative.txt and Metal.txt into some sort of Base.txt, and run these scripts against that.


0

Response Number 9
Name: Judago
Date: August 28, 2008 at 02:18:34 Pacific
Reply:

[edit fixed filenames with spaces]

Ok here's (yet) another script, being reminded of %random:~-3% sparked something. It's probably not so good for text file containing more than 32767 lines of text. It should work and each line *can* be picked(I think), except it becomes less likely to pick lines from the end of the file each time %random% is added to %random%.

Mechanix2Go: I'm having trouble understanding your script, but the when you call :gen ran3 the first time is !n! ment to be set to something?

Your Explanation(the full code is at the end of th post):
Firstly it sets a variable I will use as a counter and another counter to the highest value %random% can be:


SET CNT=0
SET LRGR=32767

Then it sets "flran" to the last digit of a random number:

SET FLRAN=%RANDOM:~-1%

It then check if the random number if less than 3, if it is then %filen% is set to alternates.txt and jumps to :ranset

IF %FLRAN% LSS 3 SET FILEN=ALTERNATES.TXT&&GOTO RANSET

If %flran% was not less than 3 the it checks if it's less than 6 %filen% will be set to rock and if it's not less than 6 it will be set to pop and continues to :ranset.

IF %FLRAN% LSS 6 (SET FILEN=ROCK.TXT) ELSE SET FILEN=POP.TXT

:lines sets the counter variable %cnt% up on for each line of text in %filen%(set earlier).

FOR /F "TOKENS=*" %%G IN (%FILEN%) DO SET /A CNT+=1

Ranset sets %ran% to a random number less than OR or equal to %cnt% then jumps to :grab unless %cnt% is larger than what %random% can provide, then it will jump to :larger.

IF %CNT% LSS 10 SET RAN=%RANDOM:~-1%&&GOTO GRAB
IF %CNT% LSS 100 SET RAN=%RANDOM:~-2%&&GOTO GRAB
IF %CNT% LSS 1000 SET RAN=%RANDOM:~-3%&&GOTO GRAB
IF %CNT% LSS 10000 SET RAN=%RANDOM:~-4%&&GOTO GRAB
IF %CNT% LSS 32767 (SET RAN=%RANDOM%) ELSE SET RAN=%RANDOM%&&GOTO LARGER

:Larger is to make sure every line can be picked. It adds 32767(the most %random% can provide) to %lrgr% then adds %random% to ran. It does this in a loop until %Lrgr% is larger than %cnt%, then jumbs to grab.

:LARGER
SET /A LRGR+=32767
SET /A RAN+=%RANDOM%
IF %CNT% LSS %LRGR% (GOTO GRAB) ELSE GOTO LARGER

Grab first checks that %ran% isn't larger than %cnt%, if it is the it sets %lrgr% back to 32767 and ran back to nothing then goes gack to :ranset

IF %RAN% GEQ %CNT% SET RAN=&&SET LRGR=32767&&GOTO RANSET

The next part is very important it calls grabber rather than using goto. Why? because goto :eof ends a call this is important for grabber. The goto playtrack is also important as it stops us from going to grabber after we have just called it as when a call finishes program flow goes to the line after the call.

CALL :GRABBER
GOTO PLAYTRACK

:grabber, in normal circumstances would do the same thing to ever line of text except lines that are skipped. The trick is to skip our random number of lines and set the next line to a variable. The &&goto :eof ends the call on the first iteration! leaving us with a variable set to the first line of text after the skipped lines of text.

FOR /F "SKIP=%RAN% TOKENS=*" %%H IN (%FILEN%) DO SET SONG=%%H&&GOTO :EOF

Now we goto playtrack (sorry for the misleading label) all we need to do now is figure out if our filename contains a space/spaces, we have to "double quote". To do this it will set "temp" to our filename and take one off %temp% each time until it is no longer defined or it finds a space. If it finds a space the it sets sw to 1 and goes to starter. If sw isn't set then we start the file name as is and if it is we enclose it in spaces.

:SPACE
IF NOT DEFINED TEMP GOTO STARTER
IF "%TEMP:~0,1%" == " " SET SW=1&&GOTO STARTER
SET TEMP=%TEMP:~1%
GOTO SPACE
:STARTER
IF NOT DEFINED SW (%SONG%) ELSE "%SONG%"


Here's the whole thing:



@ECHO OFF
SET CNT=0
SET SW=
SET TEMP=
SET LRGR=32767
SET FLRAN=%RANDOM:~-1%
IF %FLRAN% LSS 3 SET FILEN=ALTERNATES.TXT&&GOTO LINES
IF %FLRAN% LSS 6 (SET FILEN=ROCK.TXT) ELSE SET FILEN=POP.TXT

:LINES
FOR /F "TOKENS=*" %%G IN (%FILEN%) DO SET /A CNT+=1

:RANSET
IF %CNT% LSS 10 SET RAN=%RANDOM:~-1%&&GOTO GRAB
IF %CNT% LSS 100 SET RAN=%RANDOM:~-2%&&GOTO GRAB
IF %CNT% LSS 1000 SET RAN=%RANDOM:~-3%&&GOTO GRAB
IF %CNT% LSS 10000 SET RAN=%RANDOM:~-4%&&GOTO GRAB
IF %CNT% LSS 32767 (SET RAN=%RANDOM%) ELSE SET RAN=%RANDOM%&&GOTO LARGER
:GRAB
IF %RAN% GEQ %CNT% SET RAN=&&SET LRGR=32767&&GOTO RANSET
CALL :GRABBER
GOTO PLAYTRACK
:GRABBER
FOR /F "SKIP=%RAN% TOKENS=*" %%H IN (%FILEN%) DO SET SONG=%%H&&GOTO :EOF
:LARGER
SET /A LRGR+=32767
SET /A RAN+=%RANDOM%
IF %CNT% LSS %LRGR% (GOTO GRAB) ELSE GOTO LARGER
:PLAYTRACK
SET TEMP=%SONG%
:SPACE
IF NOT DEFINED TEMP GOTO STARTER
IF "%TEMP:~0,1%" == " " SET SW=1&&GOTO STARTER
SET TEMP=%TEMP:~1%
GOTO SPACE
:STARTER
IF NOT DEFINED SW (%SONG%) ELSE "%SONG%"




0

Response Number 10
Name: Nicktendo
Date: August 28, 2008 at 13:24:43 Pacific
Reply:

Wow, thank you so much for both the code and explanation. I get it! Haha.

Few questions. It seems there's a problem with STARTER. I get an error when I run the batch,

'SONGNAMEHERE' is not recognized ... etc.

I'm pretty sure it's

IF NOT DEFINED SW (%SONG%) ELSE "%SONG%"

But ultimately, the script still works. I don't think I have any spaces in any of the lines, is why.


0

Response Number 11
Name: Mechanix2Go
Date: August 28, 2008 at 14:22:18 Pacific
Reply:

Judago,

"Mechanix2Go: I'm having trouble understanding your script, but the when you call :gen ran3 the first time is !n! ment to be set to something?"

Yep, I dropped the bal on that one. Thanks for spotting my mistake.

::==============================

:: pad line NUM to xxx so we can grab RAND line
:: gen 3 digit RAND

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('find /v /c "" ^< myfile') do (
set MAX=%%a
)

call :gen RAND3

:main
for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
call :pad3
if !RAND3! equ !LN! echo lucky line is: %%a && goto :eof
)
goto :eof

:gen RAND3
:loop
set RAND3=!RANDOM:~-3!
if !RAND3! gtr !MAX! goto :loop
echo lucky number is !RAND3!
goto :eof

:pad3
if !N! lss 10 (
set LN=00!N!
) else (
if !N! lss 100 (
set LN=0!N!
) else (
set LN=!N!
)
)
goto :eof


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 12
Name: Judago
Date: August 28, 2008 at 14:24:32 Pacific
Reply:

If there's no spaces in any of your file names, then simply cut it back to :playtrack and insert "start %song%" (without quotes
) at the end.

I actually tested this with text documents in the same directory as the batch and for some reason start doesn't seem to like file names with spaces, even if they are enclosed in quotes.

I think what's happening is that the songs are in a different directory and for some reason using only the file name,even with the full path, only seems to work if it is in the currrent directory. Using the file name alone leaves the batch running as though it called it anyway.


0

Response Number 13
Name: Nicktendo
Date: August 28, 2008 at 20:52:02 Pacific
Reply:

Judago, you're awesome. It works. Thank you.

I am getting errors when the batch is running though, like:
# TOKENS=*" was unexpected at this time.
Where # is a real number. I'm also getting an "Invalid Number" error, but a lot less frequently.

It's not a huge deal though. It still does what's needed. Thanks again.


0

Response Number 14
Name: Judago
Date: August 29, 2008 at 01:21:55 Pacific
Reply:

[EDIT: Added :zeroskip, small optimisations, fixes and made it work with start instead of call]

Here's an updated version, I think the errors you were having were from leading zero's on the random numbers. I have also changed most of the variable names to make them easier to understand.

It should also work properly with file names that either do or don't contain a space as is, all you should need to do is input your filenames.



@ECHO OFF
SET LINECNT=0&&SET LRGCNT=32767&&SET FILESEL=%RANDOM:~-1&&SET SW=
IF %FILESEL% LSS 3 SET GENERE=ALTERNATE.TXT&&GOTO LINES
IF %FILESEL% LSS 6 (SET GENERE=ROCK.TXT) ELSE SET GENERE=POP.TXT

:LINES
FOR /F %%G IN (%GENERE%) DO SET /A LINECNT+=1

:RANSET
IF %LINECNT% LSS 10 (SET RAN=%RANDOM:~-1%&&GOTO ZEROSKIP) ELSE IF %LINECNT% LSS 100 (SET RAN=%RANDOM:~-2%) ELSE IF %LINECNT% LSS 1000 (SET RAN=%RANDOM:~-3%) ELSE IF %LINECNT% LSS 10000 (SET RAN=%RANDOM:~-4%) ELSE IF %LINECNT% LSS 32767 (SET RAN=%RANDOM%) ELSE SET RAN=%RANDOM%&&GOTO LARGER

:CHKNCAL
IF NOT DEFINED RAN SET RAN=0&&GOTO ZEROSKIP
IF %RAN:~0,1% == 0 SET RAN=%RAN:~1%&&GOTO CHKNCAL

:ZEROSKIP
IF %RAN% GEQ %LINECNT% SET RAN=&&SET LRGCNT=32767&&GOTO RANSET
IF NOT %RAN% == 0 (SET OPT="SKIP=%RAN% TOKENS=*") ELSE SET OPT="TOKENS=*"
CALL :LINEGRAB
SET TEMP=%SONG%
GOTO SPCECHK

:LINEGRAB
FOR /F %OPT% %%H IN (%GENERE%) DO SET SONG=%%H&&GOTO :EOF

:LARGER
SET /A LRGCNT+=32767&&SET /A RAN+=%RANDOM%
IF %LINECNT% LSS %LRGCNT% (GOTO CHKNCAL) ELSE GOTO LARGER

:SPCECHK
IF NOT DEFINED TEMP GOTO MUSIC
IF "%TEMP:~0,1%" == " " (SET SW=1&&GOTO MUSIC) ELSE SET TEMP=%TEMP:~1%&&GOTO SPCECHK
:MUSIC
IF NOT DEFINED SW (START %SONG%) ELSE START "" "%SONG%"
SET LINECNT=&&SET LRGCNT=&&SET FILESEL=&&SET RAN=&&SET GENERE=&&SET TEMP=&&SET SONG=




0

Response Number 15
Name: Nicktendo
Date: August 29, 2008 at 19:15:47 Pacific
Reply:

Awesome. Thanks again. I've modified the code to fit my needs. I've got it writing 500 lines to each day text file (so I loop the "Song Writer" 500 times) and then I need to write a whole month of days (so I loop "DayWriter" 31 times).

I've got that done. No help needed there.

But after it's all set up and running, CMD closes after writing about 600 tracks. So, a hundred tracks or so into the second day. Any idea what might be going wrong? Is there a timeout or something I don't know about? I'm still new to batch scripting and CMD, so I've got no clue.


0

Response Number 16
Name: Judago
Date: August 29, 2008 at 20:58:44 Pacific
Reply:

Does the code you using contain these lines:


:CHKNCAL
IF %RAN:~0,1% == 0 SET RAN=%RAN:~1%&&GOTO CHKNCAL
IF NOT DEFINED RAN SET RAN=0

I actually edited the post above a few times to fix bugs I realised were there, the one above fixes leading zeros. The if not defined line I added in an edit fixes the fix, causing ran to be undefined if all of the digits that make up the random number are zero's. %ran% being undefined will cause the batch to close like this.

I'm just wondering if you copied the post above before I edited out the bugs. One thing you can try with the code you already have is invoke the batch file from a command line rather than double clicking on it, this way you will be able to read the error it spits out as the batch ends.

Can you please post the code your using if the problem isn't what I mentioned above.


0

Response Number 17
Name: Nicktendo
Date: September 1, 2008 at 23:10:02 Pacific
Reply:

Yeah, that code is in there all right. Still crashing. Here's the portion of the code that isn't working. It's almost your code exactly, just changed a few things.

SET /A DAY=1

:DayMaker
REM Here, I set DayFile to the format MMDDYY (MONTH and YEAR are previously defined by user)
IF %DAY% LSS 10 (SET DayFile=%MONTH%0%DAY%%YEAR%&&GOTO MakeDay)
IF %DAY% LEQ 32 (SET DayFile=%MONTH%%DAY%YEAR%) ELSE (GOTO AllDaysDone)

:MakeDay


ECHO.
ECHO Deleting current %DayFile%.txt...
IF EXIST %DayFile%.txt DEL %DayFile%.txt
ECHO Compiling %DayFile%.txt...

SET /A LIBlengthCNT=0

:DayBuilder

REM I want 500 tracks in each file, so I loop the file writer 500 times for each day
IF %LIBlengthCNT%==500 (GOTO DayBuilderOut)


SET LINECNT=0&&SET LRGCNT=32767&&SET FILESEL=%RANDOM:~-2%&&SET SW=&&SET GENRE=

REM The genre 'percent' variables are also predefined to completely cover 01-99
IF %FILESEL% LSS %ALTpercent% (SET GENRE=ALT.TXT&&GOTO LINES)
IF %FILESEL% LSS %AMBpercent% (SET GENRE=AMB.TXT&&GOTO LINES)
IF %FILESEL% LSS %BLUpercent% (SET GENRE=BLU.TXT&&GOTO LINES)
IF %FILESEL% LSS %CTYpercent% (SET GENRE=CTY.TXT&&GOTO LINES)
IF %FILESEL% LSS %ELEpercent% (SET GENRE=ELE.TXT&&GOTO LINES)
IF %FILESEL% LSS %LOCpercent% (SET GENRE=LOC.TXT&&GOTO LINES)
IF %FILESEL% LSS %METpercent% (SET GENRE=MET.TXT&&GOTO LINES)
IF %FILESEL% LSS %URBpercent% (SET GENRE=URB.TXT&&GOTO LINES)
IF %FILESEL% LSS %WLDpercent% (SET GENRE=WLD.TXT) ELSE (SET GENRE=MUS.TXT)


:LINES
FOR /F "TOKENS=*" %%G IN (%GENRE%) DO SET /A LINECNT+=1

:RANSET
IF %LINECNT% LSS 10 SET RAN=%RANDOM:~-1%&&GOTO ZEROSKIP
IF %LINECNT% LSS 100 SET RAN=%RANDOM:~-2%&&GOTO CHKNCAL
IF %LINECNT% LSS 1000 SET RAN=%RANDOM:~-3%&&GOTO CHKNCAL
IF %LINECNT% LSS 10000 SET RAN=%RANDOM:~-4%&&GOTO CHKNCAL
IF %LINECNT% LSS 32767 (SET RAN=%RANDOM%) ELSE SET RAN=%RANDOM%&&GOTO LARGER

:CHKNCAL
IF %RAN:~0,1% == 0 SET RAN=%RAN:~1%&&GOTO CHKNCAL
IF NOT DEFINED RAN SET RAN=0

:ZEROSKIP
IF %RAN% GEQ %LINECNT% SET RAN=&&SET LRGCNT=32767&&GOTO RANSET
IF NOT %RAN% == 0 (SET OPT="SKIP=%RAN% TOKENS=*") ELSE SET OPT="TOKENS=*"
CALL :LINEGRAB
SET TEMP=%SONG%
GOTO SPCECHK

:LINEGRAB
FOR /F %OPT% %%H IN (%GENRE%) DO SET SONG=%%H&&GOTO :EOF

:LARGER
SET /A LRGCNT+=32767&&SET /A RAN+=%RANDOM%
IF %LINECNT% LSS %LRGCNT% (GOTO CHKNCAL) ELSE GOTO LARGER

:SPCECHK
IF NOT DEFINED TEMP GOTO SONGWRITE
IF "%TEMP:~0,1%" == " " (SET SW=1&&GOTO SONGWRITE) ELSE SET TEMP=%TEMP:~1%&&GOTO SPCECHK

:SONGWRITE
ECHO.%SONG%>>%DayFile%.txt

SET LINECNT=&&SET LRGCNT=&&SET FILESEL=&&SET RAN=&&SET GENRE=&&SET TEMP=&&SET SONG=


SET /A LIBlengthCNT=%LIBlengthCNT%+1
GOTO DayBuilder1


:DayBuilderOut1


ECHO File %DayFile%.txt created, with %LIBlengthCNT% entries.


SET /A DAY=%DAY%+1

GOTO DayMaker

:AllDaysDone


0

Response Number 18
Name: Judago
Date: September 2, 2008 at 00:10:29 Pacific
Reply:

I've only given this a quick look, and will keep looking into it there's just a couple of things I have noticed, firstly:


set /a day=1

I think this should probably just be:

set day=1

and secondly although 2 of your labels are :daybuilder and :daybuilderout your goto lines specify daybuilder1 and daybuilderout1


Did you by any run it from the command line to see what error, if any, it spits out before it closes?


0

Response Number 19
Name: Judago
Date: September 2, 2008 at 00:47:21 Pacific
Reply:

[EDIT: SCREWED UP AGAIN!]

I think I found the problem, ran became undefined so the line after :chkncal will cause a problem:


:CHKNCAL
IF %RAN:~0,1% == 0 SET RAN=%RAN:~1%&&GOTO CHKNCAL
IF NOT DEFINED RAN SET RAN=0

This fixes it:

:CHKNCAL
IF NOT DEFINED RAN SET RAN=0&&GOTO ZEROSKIP
IF %RAN:~0,1% == 0 SET RAN=%RAN:~1%&&GOTO CHKNCAL

Sorry for the trouble.....
Definatley my fault, Hope it works properly this time.


A couple of pointers though:

Set /a is for maths, if you just want to set a value just use set var=value even if it's a number.

Again with set /a if you want to add, subtract ect. to or from a variable specify it without % signs and then the operater and the value after the = sign. e.g. set /a var+=1 adds 1 to %var%.

Let me know if it works out or not.


0

Response Number 20
Name: Nicktendo
Date: September 2, 2008 at 18:32:08 Pacific
Reply:

Seems to be working flawlessly now. :)

Thank you again, SO much, for your help.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Batch to del first x line... what to do to make it wor...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch that grabs a random line?

Batch:Getting random line from file www.computing.net/answers/programming/batchgetting-random-line-from-file/16304.html

batch to edit a txt file www.computing.net/answers/programming/batch-to-edit-a-txt-file/15327.html

Batch File To Delete Line www.computing.net/answers/programming/batch-file-to-delete-line/15539.html