0

I created the following .bat in order to copy a random .mp4 file from a directory A into a directory B (with a fixed file name video.mp4). So, in the directory A there is a pool of .mp4 file, and in the directory B there is the the file video.mp4 (same name, but different video every time I execute my batch-file).

That's a code who do this. It perfectly works.

@echo off
setlocal EnableDelayedExpansion
cd C:\Users\aless\Desktop\....DIRECTORY A
set n=0
for %%f in (*.*) do (
   set /A n+=1
   set "file[!n!]=%%f"
)
for /L %%i in (1,1,%time:~-1%) do set "dummy=!random!"
set /A "rand=(n*%random%)/32768+1"
copy "!file[%rand%]!" C:\Users\aless\Desktop\....DIRECTORY B\video.mp4

That's the question:

I want to add a file named "title.txt" (fixed name) in the directory B and I want that the original name of the .mp4 file chosen randomly will be written (before the copy action) into the content of the title.txt (without .mp4 extension).

So, at the end... every time I execute the .bat file, a random file have to be copied from directory A to directory B with a fixed file name (video.mp4) and the title.txt content have to be updated and substituted at the same time with the original file name without extensions.

How can I can add this operation in my code?

Thanks a lot

How can I do to add this feature to my code?

Gerhard
  • 22,678
  • 7
  • 27
  • 43
alexs
  • 1
  • 3
  • 2
    I bet you got that code from the answer by Aacini in [`this question`](https://stackoverflow.com/questions/18945521/need-to-create-a-batch-file-to-select-one-random-file-from-a-folder-and-copy-to#18946519). `:)` Though I understand why you are using a text file, you do not need to use a file, you already have the content of the directory, just use it, like that answer also demonstrated. – Gerhard Apr 13 '21 at 22:07
  • `FOR %%G IN ("!file[%rand%]!") DO >"C:\Users\aless\Desktop\DIRECTORY B\title.txt" ECHO %%~nG` – Squashman Apr 13 '21 at 22:25
  • Yes you won the bet Gerhard :) anyway i need to write the .mp4 file name (without extension) in the content of txt file (not in the txt file name) because i will run an external software who read that content of title.txt. I m not so expert in coding but I will try to add the squashman line soon. Where have I to place it exactly? – alexs Apr 14 '21 at 08:43
  • It perfectly works!!! Thank you to you all – alexs Apr 15 '21 at 18:24

0 Answers0