Jump to content

Recommended Posts

Posted (edited)

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

Posted

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.

Posted
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

Posted

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.

Posted

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"

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

Posted (edited)
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
Posted

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.

Posted (edited)

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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