• 0

Batch Script to delete, choose random, rename and move. PLEASE HELP!


Question

5 answers to this question

Recommended Posts

  • 0

Hi,

Something like the following should work, although I haven't tested it.

Dim firstPath : firstPath = "C:\Users\Matt\Desktop\TestFolder1\"
Dim secondPath : secondPath = "C:\Users\Matt\Desktop\TestFolder2\"

Dim fileSystemObject : Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
Dim fileName : fileName = "x.jpg"

If (fileSystemObject.FileExists(firstPath & fileName)) Then
	fileSystemObject.DeleteFile firstPath & fileName
End If

Dim allFiles: Set allfiles = fileSystemObject.GetFolder(secondPath).Files
Dim imageFiles : Set imageFiles = CreateObject("Scripting.Dictionary")

Dim count : count = 1
Dim file

For Each file In allFiles
	If (Right(Lcase(file.Name), 4) = ".jpg") Then
		imageFiles.Add count, file.Name
		count = count + 1
	End If
Next

Randomize
Dim random : random = Int((count - 1 + 1) * Rnd + 1)

fileSystemObject.MoveFile secondPath & imageFiles(random), firstPath & fileName

The idea is we do the following:

  1. If the target image currently exists, delete it.
  2. Get a list of all the available files in the source directory.
  3. Create a list of all the available images in the source directory.
  4. Generate a random number used as the key in the dictionary of available images.
  5. Perform the rename of the source image as we move it to the target directory.

Make sure you test it first, as I haven't.

  • 0
  Antaris said:
Hi,

Something like the following should work, although I haven't tested it.

Dim firstPath : firstPath = "C:\Users\Matt\Desktop\TestFolder1\"
Dim secondPath : secondPath = "C:\Users\Matt\Desktop\TestFolder2\"

Dim fileSystemObject : Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
Dim fileName : fileName = "x.jpg"

If (fileSystemObject.FileExists(firstPath & fileName)) Then
	fileSystemObject.DeleteFile firstPath & fileName
End If

Dim allFiles: Set allfiles = fileSystemObject.GetFolder(secondPath).Files
Dim imageFiles : Set imageFiles = CreateObject("Scripting.Dictionary")

Dim count : count = 1
Dim file

For Each file In allFiles
	If (Right(Lcase(file.Name), 4) = ".jpg") Then
		imageFiles.Add count, file.Name
		count = count + 1
	End If
Next

Randomize
Dim random : random = Int((count - 1 + 1) * Rnd + 1)

fileSystemObject.MoveFile secondPath & imageFiles(random), firstPath & fileName

The idea is we do the following:

  1. If the target image currently exists, delete it.
  2. Get a list of all the available files in the source directory.
  3. Create a list of all the available images in the source directory.
  4. Generate a random number used as the key in the dictionary of available images.
  5. Perform the rename of the source image as we move it to the target directory.

Make sure you test it first, as I haven't.

Hi really appreciate but I need it in MSDOS command prompt. Windows batch file. Are you able to help as well?

  • 0

Batch simply isn't advanced enough to do this. Random selection is the main problem, it has no method to find a random number. Complex things like this aren't for batch. Use vb.net, its simple and its the language the previous poster used. If needed you could use vb.net and choose 'Console Application' to run it in a command prompt window.

  • 0

Wrong, you can do this. You would just have to know the names of the .jpg files in the Directory B, and how many.

@echo off
set 1=A .jpg file location in directory B. (C:\Directory B\)
set 2=A .jpg file location in directory B. (C:\Directory B\)
set 3=A .jpg file location in directory B. (C:\Directory B\)
set 4=A .jpg file location in directory B. (C:\Directory B\)
\\keep on doing this for how ever many .jpg files there are
set /a random2=%random%%%5
if %random2%==1 set random2=%1%
if %random2%==2 set random2=%2%
if %random2%==3 set random2=%3%
if %random2%==4 set random2=%4%
\\keep on doing this for how ever many .jpg files there are
del C:\Directory A\X.jpg
ren %random2% X.jpg
move C:\Directory B\X.jpg C:\Directory A\
echo A new, random .jpg file has replaced the old .jpg in Directory A.
pause

On line number 7, you have to change the "5" to 1 more than the currrent number of .jpg files Directory B. This is so that it selects a number 1-n.

Edited by PVVnT(0)L
  • 0
  Quote
Hi I really need help with creating a batch file that does the following. pleas help!

1) Delete "X.jpg" from directory A,

2) Choose random .jpg file in directory B and rename it to "X.jpg",

3) Move "X.jpg" from Directory b to directory A.

Please help

junjie: Did you mean copy instead of rename ? (This technique is used to show random images on a web page.)

Good responses so far. Here is an alternate script that would work on Windows. This is not absolutely random but comes clsoe. (Please test it first.)

var str list; lf -n "*.jpg" "DirectoryB"  # DirectoryB is the directory B.
var int count; set $count = { len $list }
var int s; set $s = makeint(str({chex "12[" gettime()}))
var int random; set $random = $count/60*($s+1)
# We want to copy random'th file to X.jpg.
var str file; set $file = { lex makestr(int($random)) $list }
system copy $file "DirectoryA/X.jpg" # DirectoryA is the directory A.

This script is in biterscripting ( http://www.biterscripting.com ). Email me if you need further help.

Sen

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • This is why you NEVER use AI for development unless you are intelligent enough to know what it is doing.   100% the developer's fault.
    • im an MS fanboy and even this is hilarious to me... so many issues with 24h2, how can MS say this with a straight face?
    • Every vendor does this. Its just software dev. Do you release a current set or fixes or wait for them all? What if those fixes are taking really long?
    • Google Drive videos finally get thumbnail previews, but there's a big caveat by Usama Jawad Google Drive is a pretty decent cloud storage solution, especially if you're well-invested in Google's ecosystem. If you use an Android phone, you've probably used Google Drive to store your media content, emails, and WhatsApp backups. Just a couple of weeks ago, Google announced a UI revamp for its desktop sync client for Drive, and while it has announced a much smaller feature today, it's definitely quite handy. Videos stored on Google Drive are finally getting thumbnail previews. This means that if you hover your mouse over the progress bar of a video, you'll see a thumbnail for that time frame, just like YouTube. And similar to YouTube, you'll also be able to drag your cursor (or finger, depending upon your input mechanism) to scrub through the video while seeing thumbnail previews. This is a very useful capability that feels long overdue. When trying to locate a specific scene in a video, you no longer need to guess the approximate time, you can simply use thumbnail previews to sift through the video and reach your desired frame. There is a big caveat, though. Thumbnail previews in Google Drive videos are just available for newly uploaded content. There is no way to leverage the feature on the videos currently present in your Drive library. If you have turned on automatic backups to Drive through your Android phone or are just a Drive user in general, you likely have hundreds, if not thousands, of videos in your library. However, there is no way to enable thumbnail previews on any of them. In terms of rollout, this capability is now being made available to all Google Workspace customers, Google Workspace Individual subscribers, and personal Google accounts over the next few days. There is no way for Google Workspace admins to disable the feature from their side.
    • ..."both powered by Intel's latest Core Ultra 200 Series processors"
  • Recent Achievements

    • Week One Done
      SmileWorks Dental earned a badge
      Week One Done
    • Community Regular
      vZeroG went up a rank
      Community Regular
    • Collaborator
      Snake Doc earned a badge
      Collaborator
    • Week One Done
      Snake Doc earned a badge
      Week One Done
    • One Month Later
      Johnny Mrkvička earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      583
    2. 2
      Michael Scrip
      199
    3. 3
      ATLien_0
      196
    4. 4
      +FloatingFatMan
      129
    5. 5
      Xenon
      123
  • Tell a friend

    Love Neowin? Tell a friend!