r0sWell Posted October 6, 2009 Posted October 6, 2009 (edited) Hi guys,1st, my apologies if my English is not perfect 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 WshShellDim vProgDataDim objFolderDim oFSO,oFlDim objFileDim objNbrSet 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).FilesRandomizeFor i=1 To oFl.Count objNbr = Int(oFl.Count * Rnd + 1)NextMsgBox 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 October 6, 2009 by r0sWell
Yzöwl Posted October 6, 2009 Posted October 6, 2009 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.
r0sWell Posted October 7, 2009 Author Posted October 7, 2009 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
Yzöwl Posted October 7, 2009 Posted October 7, 2009 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.
geezery Posted October 7, 2009 Posted October 7, 2009 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.FilesFilecount = 0For Each File In FilesFilecount = Filecount + 1objDictionary.Add Filecount, File.NameNextRandomizeFor i=1 To objDictionary.CountobjNbr = Int(objDictionary.Count * Rnd + 1)NextRandomFile = objDictionary.Item(objNbr)oFSO.CopyFile SourceFolder & RandomFile,DestinationFolder & "user.bmp",TrueMsgBox "File " & Randomfile & " copied to " & DestinationFolder & "user.bmp"
Yzöwl Posted October 7, 2009 Posted October 7, 2009 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.
r0sWell Posted October 7, 2009 Author Posted October 7, 2009 (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 i'll try to adapt it to my needsEdit :Now fully working with variable paths :-)thx again ! Edited October 7, 2009 by r0sWell
Yzöwl Posted October 7, 2009 Posted October 7, 2009 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 ENABLEEXTENSIONSSET "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.
jaclaz Posted October 7, 2009 Posted October 7, 2009 (edited) 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.txtVBS:http://www.robvanderwoude.com/files/wallpapr_vbs.txtjaclaz Edited October 7, 2009 by jaclaz
r0sWell Posted October 7, 2009 Author Posted October 7, 2009 (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.FilesFilecount = 0For Each File In FilesFilecount = Filecount + 1objDictionary.Add Filecount, File.NameNextRandomizeFor i=1 To objDictionary.CountobjNbr = Int(objDictionary.Count * Rnd + 1)NextRandomFile = 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 ! Edited October 7, 2009 by r0sWell
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now