• 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

    • did you not read this line . The Trump Organization, spearheaded by President Trump's sons, Donald Trump Jr. and Eric Trump This by his sons Trump himself
    • Looks nicer, must have app (or collection of apps?) for me. FancyZones, find my mouse, colour picker, run (and new download app) are amazing.
    • That is typically how his products go. They start off with a photoshopped image or slapped together pre-production sample. If they get enough orders, they make them, if they don't, they cancel and send refunds. Art of the deal, lol.
    • The idea of an adversary saying "oh darn it, the keys are on a TPM chip so we can't get them" is probably too good to be true.
    • PowerToys is getting a big Dashboard redesign by Taras Buria PowerToys currently has 28 utilities to enhance your user experience (the latest addition arrived in April). You can check out the list of all modules and quickly toggle them on or off on the dedicated Dashboard page that greets you each time you open PowerToys. However, with the increasing list of tools and their vast customizability, many may feel that the Dashboard is a bit undercooked and lacks more polish and useful features. If you think so, Microsoft has some good news for you. In a new PR on PowerToys' GitHub, Microsoft engineers agree that the Dashboard needs improvements, even though it worked fine in earlier days with fewer modules. The proposed solution for the overcrowded Dashboard is a redesigned variant, which looks more similar to the Windows 11 Settings app. With the suggested variant, you get much more from Dashboard: a quick link to the latest changelog, various warnings that need your attention (like conflicting shortcuts), an update button, Quick Launcher, shortcuts for your modules, and a list of available tools. A search box for modules and settings is also there to help you find stuff that you need. Here is how Niels Laute, a software engineer working on WinUI and PowerToys, describes the proposed redesign: Since PowerToys is an open-source project, other developers are welcome to contribute their ideas. One of those ideas is implementing SplitView so that you can open each module's settings without leaving the Dashboard page. Unfortunately, there is no information on when Microsoft will ship the redesigned Dashboard. While we wait for the June 2025 update, check out these three useful plugins for PowerToys Run. What other changes would you like Microsoft to add to PowerToys? Share your thoughts in the comments.
  • Recent Achievements

    • Week One Done
      patrickft456 earned a badge
      Week One Done
    • One Month Later
      patrickft456 earned a badge
      One Month Later
    • One Month Later
      Jdoe25 earned a badge
      One Month Later
    • Explorer
      Legend20 went up a rank
      Explorer
    • One Month Later
      jezzzy earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      637
    2. 2
      ATLien_0
      275
    3. 3
      +FloatingFatMan
      171
    4. 4
      Michael Scrip
      155
    5. 5
      Steven P.
      133
  • Tell a friend

    Love Neowin? Tell a friend!