Jump to content

[Question] Batch processing?


overwraith

Recommended Posts

I am trying to take some batch script and make it executable line by line in the command prompt. Have made some good progress, but some of the aspects of batch programming still evade me.

at this webpage: http://forums.techgu...-selecting.html

I found this code:

BATCH FILE CODE
setlocal EnableDelayedExpansion
set count=0
for /f "delims=" %%a in ('dir /b *.mp4') do @(
set filename[!count!]=%%a
set /a count = count + 1
)
set /a choose = (%random% * 32768 + %random%) %% count
set chosen=!filename[%choose%]!
echo %chosen%
pause[/CODE]

which I figured out I could put the set commands on one line using the '&' operator, and I could change the file scope to all files in the directory:

[CODE]BATCH FILE CODE
setlocal EnableDelayedExpansion
set count=0
for /f "delims=" %%a in ('dir /b *.*') do @( set filename[!count!]=%%a & set /a count = count + 1 )
set /a choose = (%random% * 32768 + %random%) %% count
set chosen=!filename[%choose%]!
echo %chosen%
pause[/CODE]

Now what is bothering me is converting the variables to command line batch instead of batch file batch. I know the counter variables we have to take off a '%' operator, but the '!' operators really confuse me. Do we have to completely change the '!' operators? This will ultimately be something my ducky types in to select a random file to append an alternate data stream to. It is very important that there is a variable at the end of whatever solution is made which contains the file name. When you paste this in your command prompt to test paste "cmd /V:On" at the beginning to enable delayed variable expansion. My understanding of delayed variable expansion is limited right now, I only know that if it is not enabled and a for loop calls the same variable twice there will be problems.

My current code with one '%' removed from the counter variables:

[CODE]ATTEMPT AT CONVERTING TO STRAIGHT COMMAND LINE
cmd /V:On
set count=0
for /f "delims=" %a in ('dir /b *.*') do @( set filename[!count!]=%a & set /a count = count + 1 )
set /a choose = (%random% * 32768 + %random%) % count
set chosen=!filename[%choose%]!
echo %chosen%
[/CODE]

Edited by overwraith
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...