Jump to content

Want to select random file, copy and rename to another directory


Recommended Posts

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


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

You may remove the last line if it irritates you.

Link to comment
Share on other sites

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

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/B

jaclaz

Link to comment
Share on other sites

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/B

jaclaz

 

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

 

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

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\able72

For `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

 

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\able72

For `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 by able72
Link to comment
Share on other sites

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

It 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 by able72
Link to comment
Share on other sites

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

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