MSFN Forum: [VBS] Randomly select a file then copy it ? - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[VBS] Randomly select a file then copy it ? please Rate Topic: -----

#1 User is offline   r0sWell 

  • Member
  • PipPip
  • Group: Members
  • Posts: 179
  • Joined: 09-June 04
  • OS:Windows 7 x64
  • Country: Country Flag

  Posted 06 October 2009 - 08:31 AM

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:


Quote

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 :}

This post has been edited by r0sWell: 06 October 2009 - 08:38 AM



#2 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,195
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 06 October 2009 - 03:15 PM

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.

#3 User is offline   r0sWell 

  • Member
  • PipPip
  • Group: Members
  • Posts: 179
  • Joined: 09-June 04
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 07 October 2009 - 02:07 AM

View PostYzöwl, on Oct 6 2009, 11:15 PM, said:

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

#4 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,195
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 07 October 2009 - 06:27 AM

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.

#5 User is offline   geezery 

  • Member
  • PipPip
  • Group: Members
  • Posts: 254
  • Joined: 21-July 06

Posted 07 October 2009 - 06:51 AM

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"

#6 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,195
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 07 October 2009 - 07:22 AM

View Postgeezery, on Oct 7 2009, 01:51 PM, said:

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.

#7 User is offline   r0sWell 

  • Member
  • PipPip
  • Group: Members
  • Posts: 179
  • Joined: 09-June 04
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 07 October 2009 - 07:34 AM

View PostYzöwl, on Oct 7 2009, 03:22 PM, said:

View Postgeezery, on Oct 7 2009, 01:51 PM, said:

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 !

This post has been edited by r0sWell: 07 October 2009 - 07:41 AM


#8 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,195
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 07 October 2009 - 07:44 AM

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.

#9 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 9,107
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 07 October 2009 - 08:05 AM

There is a very similar mechanism in this:
http://www.robvander...w.php#Wallpaper

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

Batch:
http://www.robvander...wallpapr_nt.txt

VBS:
http://www.robvander...allpapr_vbs.txt

jaclaz

This post has been edited by jaclaz: 07 October 2009 - 08:06 AM


#10 User is offline   r0sWell 

  • Member
  • PipPip
  • Group: Members
  • Posts: 179
  • Joined: 09-June 04
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 07 October 2009 - 08:26 AM

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)


Quote

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

This post has been edited by r0sWell: 07 October 2009 - 08:37 AM


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy