0

This is the code I have so far:

@echo off
set /P suffix=Enter name to add:

@echo off
setlocal EnableDelayedExpansion    
PUSHD .
cd "%%d"
set n=0
for %%f in (*.*) do (
   set /A n+=1
   set "file[!n!]=%%f"
)                        
set /A "rand=(n*%random%)/32768+1" 
copy "!file[%rand%]!" "%suffix%"  
POPD

Progress: I have the functions of taking user input, selecting a single random file and copying it and pasting it with a new name.

Problems: It doesn't apply to subfolders, the user input isn't added as a suffix but literally renames the entire file.

Question: How can I add the user input as a suffix to a newly copied file while retaining the filename and filetype?

And is it possible to apply this function to subfolders as well?

  • 7
    I'm voting to close this question as off-topic because it's a program spec, not a solution-request – Magoo Jan 20 '17 at 05:51
  • understood, will add my own code of what I have so far next time – user3470157 Jan 20 '17 at 06:12
  • You can as well [edit] your question with the current state of your script... – geisterfurz007 Jan 20 '17 at 06:28
  • edited to reflect the guidelines – user3470157 Jan 21 '17 at 00:14
  • @user3470157 The first hints no really related to your question as answering is currently not possible. Read [this answer](http://stackoverflow.com/a/38676582/3074564) explaining in detail what the commands `setlocal` and `endlocal` do. The current directory is also pushed on stack by `setlocal` and restored by `endlocal`. Therefore `PUSHD .` is superfluous if replacing `POPD` by command `endlocal`. The second `@echo off` is also unnecessary. – Mofi Jan 21 '17 at 12:42
  • @user3470157 Next I suggest to open a command prompt window, run `for /?` and read __all__ help pages output. Like help of command __CALL__ output by running `call /?` in command prompt window there is explained `%~dpn1` (argument of batch file or subroutine) and `%~dpnI` (loop variable I). `f` is not good as loop variable because of `%~fI` with `I` being the loop variable. Loop variables are case-sensitive. If you want to use nevertheless `F` as loop variable for file names, use upper case `F` to avoid complications. – Mofi Jan 21 '17 at 12:47
  • @user3470157 My last hint which answers your question: Replace the `copy` line with the command line `for /F "delims=" %%I in ("!file[%rand%]!") do copy "%%~I" "%%~dpnI%suffix%%%~xI"` – Mofi Jan 21 '17 at 12:53
  • @user3470157 And for your second question change the line `for %%f in (*.*) do (` to `for /R %%f in (*) do (` to search recursive (= in current folder and all subfolders) for files whereby it would be better to use something different than `f` as loop variable. – Mofi Jan 21 '17 at 13:02

0 Answers0