able72 Posted December 31, 2014 Share Posted December 31, 2014 Hello, new to coding. I've serched the internet and tried a bunch of different solutions to solve my problem with no luck, so I thought I would ask and see if I couldn't find someone to help me. I am trying to select a random .tga file from a directory (the option to select from sub directories within the primary directory would prove advantageous). Once selected, I would like to copy that file to another directory and then rename it to 1.tga. I'm not picky about whether it is .vbs or .bat or whatever (I will need to know which so I can create it correctly on my machine), just so long as it's something native to Windows 7 x64. Here is the specific application: I'm trying to write some code to randomly change the dashpic in IL-2 Cliffs of Dover. The images are currently stored in my user folder under: \Documents\1C SoftClub\il-2 sturmovik cliffs of dover - MOD\DashPics\Templates It needs to be copied to: \Documents\1C SoftClub\il-2 sturmovik cliffs of dover - MOD\DashPics\Selected and renamed to 1.tga. I would like to be able to change it so that I could perhaps use it to randomize splash screens, etc. Which would have a different directory structure and perhaps a different file extension. Any help would be greatly appreciated. Link to comment Share on other sites More sharing options...
Yzöwl Posted January 1, 2015 Share Posted January 1, 2015 Here's a New Year present (RandCopy.cmd) (it uses your given paths and does not use sub-directories):@Echo Off & SetLocal DisableDelayedExpansionSet "_BD=Documents\1C SoftClub\il-2 sturmovik cliffs of dover - MOD\DashPics"If "%CD%" NEq "%_BD%" PushD %_BD%||Exit/BSet "_$="For %%# In (Templates\*.tga) Do Set/A _$+=1 & For /F %%_ In ('Call Echo(%%_$%%' ) Do Set "_F[%%_]=%%#"Set/A _R=%Random% %% _$ +1Call Echo(Processing "%%_F[%_R%]%%"Call Copy "%%_F[%_R%]%%" Selected\1.tga>NulTimeout 5 1>NulYou may remove the last line if it irritates you. Link to comment Share on other sites More sharing options...
able72 Posted January 1, 2015 Author Share Posted January 1, 2015 Can't get it to work. Is there someplace in particular I should place it in relation to the file structure? some way I ought to modify the directory path? Not sure what's happening with it because the command window closes so fast. Link to comment Share on other sites More sharing options...
jaclaz Posted January 1, 2015 Share Posted January 1, 2015 Try running this one:@Echo Off & SetLocal DisableDelayedExpansionSet "_BD=Documents\1C SoftClub\il-2 sturmovik cliffs of dover - MOD\DashPics"ECHO "%CD%"ECHO "%_BD%"ECHO are the above two lines identical?PAUSEIf "%CD%" NEq "%_BD%" PushD %_BD%||Exit/Bjaclaz Link to comment Share on other sites More sharing options...
able72 Posted January 1, 2015 Author Share Posted January 1, 2015 Try running this one:@Echo Off & SetLocal DisableDelayedExpansionSet "_BD=Documents\1C SoftClub\il-2 sturmovik cliffs of dover - MOD\DashPics"ECHO "%CD%"ECHO "%_BD%"ECHO are the above two lines identical?PAUSEIf "%CD%" NEq "%_BD%" PushD %_BD%||Exit/Bjaclaz Am I supposed to add this to the previous file? Or save it by itself? As a .cmd file I'm assuming? Link to comment Share on other sites More sharing options...
jaclaz Posted January 1, 2015 Share Posted January 1, 2015 Am I supposed to add this to the previous file? Or save it by itself? As a .cmd file I'm assuming? Sure, save it as a .cmd and run it "as is", it should help you find out what is the issue you are having with the batch Yzöwl posted. As a general rule, when you are testing a batch, you DO NOT run it by double clicking on it, but rather open a command prompt, navigate to where the batch is and then you invoke it from command line, by typing the batch filename. jaclaz Link to comment Share on other sites More sharing options...
Yzöwl Posted January 1, 2015 Share Posted January 1, 2015 Can't get it to work. Is there someplace in particular I should place it in relation to the file structure? some way I ought to modify the directory path? Not sure what's happening with it because the command window closes so fast.Yes there is and no there isn't For `Yes there is`:You place your file in the directory which contains the path starting Documents, (on Line 2)e.g. C:\Users\able72For `no there isn't`:You input in the full path within the script, (in front of Documents on line 2)e.g. C:\Users\able72\Documents\1C SoftClub\il-2 sturmovik cliffs of dover - MOD\DashPics Link to comment Share on other sites More sharing options...
able72 Posted January 1, 2015 Author Share Posted January 1, 2015 (edited) Can't get it to work. Is there someplace in particular I should place it in relation to the file structure? some way I ought to modify the directory path? Not sure what's happening with it because the command window closes so fast.Yes there is and no there isn't For `Yes there is`:You place your file in the directory which contains the path starting Documents, (on Line 2)e.g. C:\Users\able72For `no there isn't`:You input in the full path within the script, (in front of Documents on line 2)e.g. C:\Users\able72\Documents\1C SoftClub\il-2 sturmovik cliffs of dover - MOD\DashPics Ok, so to make sure I understand (I did get it to work, now I'm learning how). In order to place the .cmd file where ever I want in the structure, I should set the full path directory? So for example, I would change Set "_BD=Documents\1C SoftClub\il-2 sturmovik cliffs of dover - MOD\DashPics" to Set "_BD=c:\Users\able72\Documents\1C SoftClub\il-2 sturmovik cliffs of dover - MOD\DashPics" My next question is, if I want this to be usable by whomever the current user is, should I use the variable %user%? is that right? so it would be Set "_BD=c:\Users\%user%\Documents\1C SoftClub\il-2 sturmovik cliffs of dover - MOD\DashPics" Thank you for your help with this. I would like to learn this, because next I want to alter this code to select random music for startup and a random splash screen (those are in a different location, and use different file extensions). I'll then create a .bat file to call each one in sequence, wait till they are finished, start my TrackIR and then start the game. Edited January 1, 2015 by able72 Link to comment Share on other sites More sharing options...
Yzöwl Posted January 1, 2015 Share Posted January 1, 2015 Just change the 2nd line to read, for example;Set "_BD=%UserProfile%\Documents\1C SoftClub\il-2 sturmovik cliffs of dover - MOD\DashPics" Link to comment Share on other sites More sharing options...
able72 Posted January 1, 2015 Author Share Posted January 1, 2015 (edited) Ok, don't get this. Here is what I have:@Echo Off & SetLocal DisableDelayedExpansionSet "_BD=%UserProfile%\Documents\1C SoftClub\il-2 sturmovik cliffs of dover - MOD\DashPics"If "%CD%" NEq "%_BD%" PushD %_BD%||Exit/BSet "_$="For %%# In (Templates\*.tga) Do Set/A _$+=1 & For /F %%_ In ('Call Echo(%%_$%%' ) Do Set "_F[%%_]=%%#"Set/A _R=%Random% %% _$ +1Call Echo(Processing "%%_F[%_R%]%%"Call Copy "%%_F[%_R%]%%" Selected\1.tga>NulTimeout 5 1>NulRem Start TrackIRcd C:\Program Files (x86)\NaturalPoint\TrackIR5start TrackIR5.exeTimeout 5 1>NulRem Start Steam Directlycd D:\Applications\SteamStart Steam.exeTimeout 5 1>NulRem Start IL-2 Directlycd C:\Games\SteamLibrary\SteamApps\common\IL-2 Sturmovik Cliffs of DoverStart Launcher.exeIt isn't making the switch to d drive. I've tried entering it manually into the command prompt cd d:\ but nothing, it stays in the c: drive. Any suggestions? Nevermind, I found it. I need to use the /D switch Edited January 1, 2015 by able72 Link to comment Share on other sites More sharing options...
jaclaz Posted January 1, 2015 Share Posted January 1, 2015 Try CD /D http://ss64.com/nt/cd.html(and use double quotes for paths containing spaces ). jaclaz Link to comment Share on other sites More sharing options...
able72 Posted January 1, 2015 Author Share Posted January 1, 2015 Try CD /D http://ss64.com/nt/cd.html(and use double quotes for paths containing spaces ). jaclaz Yep. I found that by using cd/? Amazing what you can find in a help function Link to comment Share on other sites More sharing options...
jaclaz Posted January 1, 2015 Share Posted January 1, 2015 Amazing what you can find in a help function Yep , but quite a few things are not there JFYI:http://ss64.com/nt/start.htmlthere are issues sometimes when start is used without a "title" or just ""... jaclaz Link to comment Share on other sites More sharing options...
gunsmokingman Posted January 1, 2015 Share Posted January 1, 2015 Here is the VBS way of searching the Parent folder and all it subfolder and a filter to get the require extension. Then it collects all the files add them to a Dictionary Object, then selects a random item from the dictionary and copy the file to 1.vbs. I have included comments to help edit the code. Dim Dic :Set Dic = CreateObject("Scripting.Dictionary")Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")Dim Col, Obj'-> Uncomment Search(Fso.GetFolder(".")) Use Only If Script Is In The '-> Same Folder That You Want To Search All Folders And Sub Folders Search(Fso.GetFolder("."))'-> Uncomment Place Full Path Search(Fso.GetFolder("Drive:\FOLDER\ANOTHERFOLDER"))'-> This Will Then Use That Path As The Parent Folder To Search Threw All The Folders ' Search(Fso.GetFolder("Drive:\FOLDER\ANOTHERFOLDER"))'-> Start The Work After The Search Has Collected All The Files Work() Function Search(F) For Each Obj In F.Files'-> Here We Filter Out The File Type Then Add It To The Dictionary'-> Change "vbs" To Extension Than You Need If Not Dic.Exists(Obj) And LCase(Right(Obj,3)) = "vbs" Then Dic.Add Obj.Path, Obj End If Next For Each Col In F.SubFolders Search(Col) Next End Function '-> Selects Dictionary Ramdom File, Then Copy File Function Work() Dim a,b,i '-> Where We Get The Random Item Randomize a = Dic.Count Do b = int(Rnd(1)*a) Loop While b < 1 a = Dic.Items For i = 0 To Dic.Count -1'-> Match Random Item With The Dictionary Item If b = i Then'-> Uncomment If You Want To See The Original FileName Before Rename' WScript.Echo "CopyFile : " & a(i)'-> Uncomment And Change "\\HOMEBETAVISTA\Vista-D\TestCopyRename\1.vbs" To What You Need' Fso.CopyFile a(i), "\\HOMEBETAVISTA\Vista-D\TestCopyRename\1.vbs",True End If Next End Function Link to comment Share on other sites More sharing options...
able72 Posted January 1, 2015 Author Share Posted January 1, 2015 Thank you everyone for your help. I really appreciate it. Link to comment Share on other sites More sharing options...
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