• 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

    • Debian switches to 64-bit time completely to avoid Y2K38 disaster by Usama Jawad Some of you may remember the Y2K problem, where the world expected airplanes to fall from the sky as soon as we reached the year 2000, primarily because many software applications at that time typically just used the final two digits of each year to store calendar year data, which meant that the year 2000 was indistinguishable from the year 1900. Fortunately, we were able to avoid the 2K problem, thanks to the tireless efforts of many software vendors and engineers. Now, we are a few years away from a similar issue, and it looks like the Linux distro Debian wants to solve that problem right now in its own operating system. Basically, older 32-bit architectures will face the Y2K38 problem in the year 2038. This is because the signed representation of Unix datetime values will overflow the 32-bit space, which would cause bugs in associated software. Debian is a pretty old distro with its first release dating back to 1993, so the maintainers say that a lot of sensitive computing is still happening on 32-bit architecture. Although there are still roughly 13 years to go before we reach 2038, developers want to proactively tackle the problem rather than having to scramble at the last minute like with Y2K, according to The Register. Another name for Y2K38 is the Unix Epochalypse, since it impacts systems that store datetime values in the Unix format within a signed 32-bit space. On January 19, 2038, 03:14:07 UTC, this space will overflow. As such, Debian maintainers will use 64-bit time_t formats even on 32-bit architectures starting with the release of Debian 13 "Trixie". This is not a small change, as maintainers found the use of the time_t variable in random places across 6,429 packages. The maintainers went on to say that: This may be a breaking change for some applications, so it is important to test your program's response to the time_t variable switch by leveraging the Debian wiki. Interestingly, Y2K38 may also impact certain older Windows programs and out-of-support Windows operating systems.
    • I hope they programed its eyes to turn red when it becomes evil, that is an important feature for any AI representation.
    • Microsoft gives Copilot visual appearance with real-time expressions and emotions by Taras Buria Several months ago, during its 50th anniversary event, Microsoft teased a visual upgrade for Copilot (then called "Copilot Avatar") that would give the chatbot a visual character with expressions, reactions, and emotions. Now, users in the US, UK, and Canada can try Copilot Appearance, "a new, visual way to chat." Conversational mode has been available in Copilot for a while, but it lacked any visual cues or non-verbal communications. All users see on their screens is some abstract animation. With Copilot Appearance, Microsoft is improving voice conversations with real-time visual expressions. Sadly, Copilot Appearance is not a Clippy 2.0. In its current form, it is an abstract blob with a face that can morph into different shapes, express emotions, nod in agreement, etc. The concept is similar to what xAI offers with Grok AI companions that cost $300 per month, but Microsoft's approach is much more toned down (and you cannot undress it). On the official Copilot Labs website, Microsoft describes Copilot Appearance as "an experiment to incorporate a more visual, engaging experience into Copilot." Copilot Appearance is rolling out to a limited set of users in just three countries as Microsoft takes a cautious approach to a more personified AI. If you have a lucky ticket, you can try Copilot Appearance by launching Voice Mode (click the microphone button in the composer) and toggling the feature in Voice Mode settings. Microsoft says that the initial release is experimental, and it is working on refining the experience.
    • A number of years ago I purchased a "Renewed" HP laptop. It worked great until it didn't work at all. Both of my W11 devices are HP AIO desktops. Have had them since before W11. They upgraded fine and have worked great ever since. My printer is also an HP inkjet. I have had it several years and have never paid for ink.
    • If you use AdGuard on your PC, you can find the new "Disable Windows Recall" feature in Settings > Tracking Protection. The "Disable Windows Recall" feature in Adguard 7.21 in turned on by default. No need to do anything if you want to use the feature.
  • Recent Achievements

    • Week One Done
      CyberCeps666 earned a badge
      Week One Done
    • Very Popular
      d4l3d earned a badge
      Very Popular
    • Dedicated
      Stephen Leibowitz earned a badge
      Dedicated
    • Dedicated
      Snake Doc earned a badge
      Dedicated
    • One Month Later
      Philsl earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      626
    2. 2
      ATLien_0
      240
    3. 3
      Xenon
      163
    4. 4
      +FloatingFatMan
      124
    5. 5
      neufuse
      123
  • Tell a friend

    Love Neowin? Tell a friend!