Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 


r0sWell

[VBS] Randomly select a file then copy it ?

Recommended Posts

Hi guys,

1st, my apologies if my English is not perfect :blushing:

But i have troubles to create a working VBS script.

To be honest i'm a 99% noob concerning VBS, and i'd like to make a script that will do 2 things :

- in a folder C:\test\Default Pictures\ which contains many *.BMP files, i need to randomly select one of these.

- then, copy this file as USER.BMP, in the Parent folder : C:\test\

I googled many things & did some tests, but i'm not good enough to make it work.

my most recent script is:

Dim WshShell

Dim vProgData

Dim objFolder

Dim oFSO,oFl

Dim objFile

Dim objNbr

Set WshShell = WScript.CreateObject("Wscript.Shell")

vProgData = WshShell.ExpandEnvironmentStrings("%systemdrive%")

Set objShell = CreateObject("Shell.Application")

Set objFolder = objShell.Namespace(vProgData & "\test\Default Pictures\")

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFl = oFSO.GetFolder(objFolder).Files

Randomize

For i=1 To oFl.Count

objNbr = Int(oFl.Count * Rnd + 1)

Next

MsgBox objNbr (line written to see if the Random Selection works : it's OK, it randomly gives me a number between 1 and the number of the files)

Set objFile = oFSO.Name(objNbr) (troubles start from here, as you can see i'm not familiar with VB and i don't see how to "convert" this random number above to the corresponding file in the folder)

''oFSO.CopyFile "objFolder\Default Pictures\objFile","objFolder\user.bmp",True (could not even test that part...)

THX in advance for your help.

i'm blocked for now :}

Edited by r0sWell

Share this post


Link to post
Share on other sites

Is there a particular reason why you require a vbscript? You say that you'd like to make a script, so would a batch file do for instance?

Also, as a side note, are you sure that those are the real names for the source and intended destination folders? or are you just using them as an example? In some cases providing alternatives in this kind of request may exclude better solutions. I tend to see scripts as a command or series of commands to achieve a specific goal; its often more difficult to provide a generic solution where people can just change out portions to suit any scenario.

Share this post


Link to post
Share on other sites
Is there a particular reason why you require a vbscript? You say that you'd like to make a script, so would a batch file do for instance?

Also, as a side note, are you sure that those are the real names for the source and intended destination folders? or are you just using them as an example? In some cases providing alternatives in this kind of request may exclude better solutions. I tend to see scripts as a command or series of commands to achieve a specific goal; its often more difficult to provide a generic solution where people can just change out portions to suit any scenario.

i'm pretty sure that a VBS script can do that, but i'm not sure concerning a Batch (?).

for the folders, they're examples here (but i'm sure they're OK in the real most recent script).

if you have any solutions, i thank you in advance

thanks

Share this post


Link to post
Share on other sites

The task can be done using a batch file.

There's more than one way to do it but the method I'd choose would be dependent upon the specifics of the task, (source and destination locations, number of files, when and how the script is invoked, OS etc.) The only unknown which could scupper solutions is if file names contain 'poison' characters.

Share this post


Link to post
Share on other sites

SourceFolder = "C:\Temp2\"

DestinationFolder = "C:\"

Set WshShell = WScript.CreateObject("Wscript.Shell")

vProgData = WshShell.ExpandEnvironmentStrings("%systemdrive%")

Set objShell = CreateObject("Shell.Application")

Set objDictionary = CreateObject("Scripting.Dictionary")

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFl = oFSO.GetFolder(SourceFolder)

Set Files = oFL.Files

Filecount = 0

For Each File In Files

Filecount = Filecount + 1

objDictionary.Add Filecount, File.Name

Next

Randomize

For i=1 To objDictionary.Count

objNbr = Int(objDictionary.Count * Rnd + 1)

Next

RandomFile = objDictionary.Item(objNbr)

oFSO.CopyFile SourceFolder & RandomFile,DestinationFolder & "user.bmp",True

MsgBox "File " & Randomfile & " copied to " & DestinationFolder & "user.bmp"

Share this post


Link to post
Share on other sites
vProgData = WshShell.ExpandEnvironmentStrings("%systemdrive%")
Was this line required?

There's no way of us telling whether it correlates to the C: provided in the original question.

Share this post


Link to post
Share on other sites
vProgData = WshShell.ExpandEnvironmentStrings("%systemdrive%")
Was this line required?

There's no way of us telling whether it correlates to the C: provided in the original question.

it's required because in the real script, i must point to the %username% directory (which vary), so i used %systemdrive% to test variables paths.

@geezery :

thx for your script, it's working :thumbup

i'll try to adapt it to my needs

Edit :

Now fully working with variable paths :-)

thx again !

Edited by r0sWell

Share this post


Link to post
Share on other sites

As I'd stated previously, you decided, for your own reasons, to keep certain information from us without knowing whether or not it would affect potential solutions.

Anyhow with the intention of following up on my post here's one batch file method. (untested)

@ECHO OFF & SETLOCAL ENABLEEXTENSIONS

SET "D_=C:\test"
SET "S_=C:\test\Default Pictures"
SET "E_=bmp"
SET "T_=%Temp%\_$.tmp"
SET "F_=USER.BMP"

PUSHD %D_%
DIR/B/A-D "%S_%\*.%E_%"|FIND /I /N ".%E_%">"%T_%"
FOR /F %%_ IN ('FIND /V /C "" ^<"%T_%"') DO SET/A "C_=%%_ + 1"
SET/A "N_=%RANDOM% %% %C_%"
FOR /F "DELIMS=" %%_ IN ('FINDSTR/BC:"\[%N_%\]" "%T_%"') DO SET "L_=%%_"
COPY "%L_:*]=%" "%F_%"
DEL %T_%

I decided to go with output to temp file thinking it may be faster than other ideas I had.

Share this post


Link to post
Share on other sites

There is a very similar mechanism in this:

http://www.robvanderwoude.com/batexamples_w.php#Wallpaper

(intended to randomly change the wallpaper, just remove the parts that are not required/add the copy part)

Batch:

http://www.robvanderwoude.com/files/wallpapr_nt.txt

VBS:

http://www.robvanderwoude.com/files/wallpapr_vbs.txt

jaclaz

Edited by jaclaz

Share this post


Link to post
Share on other sites

i didn't want to "hide" information, i wanted to simplify my explanations in English using basic paths & needs.

and because paths was not the big deal i suppose.

below is the final script i think i'll use.

my goal is to change the Windows 7 "User Account Picture" randomly, picking one from the folder and set it to user.bmp which is the user account picture file, so it can be used by other people too ;) .

(btw i was completely wrong earlier, i said i'll need the %username% variable but in fact it's %programdata% - to make it independant from the drive letter -, shame on me i didn't check)

Set WshShell = WScript.CreateObject("Wscript.Shell")

varFolder = WshShell.ExpandEnvironmentStrings("%ProgramData%")

SrcFolder = varFolder & "\Microsoft\User Account Pictures\Default Pictures\"

DestFolder = varFolder & "\Microsoft\User Account Pictures\"

Set objDictionary = CreateObject("Scripting.Dictionary")

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFl = oFSO.GetFolder(SrcFolder)

Set Files = oFL.Files

Filecount = 0

For Each File In Files

Filecount = Filecount + 1

objDictionary.Add Filecount, File.Name

Next

Randomize

For i=1 To objDictionary.Count

objNbr = Int(objDictionary.Count * Rnd + 1)

Next

RandomFile = objDictionary.Item(objNbr)

oFSO.CopyFile SrcFolder & RandomFile,DestFolder & "user.bmp",True

''MsgBox "File " & Randomfile & " copied to " & DestFolder & "user.bmp"

thx again for your help, very appreciated !

:D

Edited by r0sWell

Share this post


Link to post
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.   Restore formatting

  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...