0

I have to write a batch script to school, which moves all .jpg files from one folder to other one and rename them at the same time. I have a problem with for loop.

set i=1

for /R %folder1% %%F in (*.jpg) do (
move "%folder1%\%%F" "%folder2%\%newname%_%i%.jpg
set /a i+=1 )

Newname is a variable entered by user.

For example if I have files named file09.jpg, fvfdfv.jpg, whatever.jpg I need to rename them to %newname%_1.jpg, %newname%_2.jpg, %newname%_3.jpg.

My problem is, that it moves all files from folder1, but in folder2 there is just one file named newname_1.jpg ... Like if all files was moved to a single one..

Do you have any tips please?

Veronika
  • 1
  • 2
  • 2
    the famous [delayed expansion trap](https://stackoverflow.com/a/30284028/2152082) strikes again. Note: `%%F` already contains the full qualified filename (including drive and path) See `for /?` for modifiers like `%%~nxF` – Stephan Jan 10 '18 at 14:46
  • Thank you, but my code still doesn't work. I tried to look at %%~nxF, but I still don't know how to use it in my code... Do you have any other tips? Or can you tell me what to do please? Thanks .. this is my first batch script :) – Veronika Jan 10 '18 at 15:15
  • OH, I understood.. I had to enable delayed expansion .. thanks :)) – Veronika Jan 10 '18 at 15:32
  • You said `from one folder to other`, but you use `for /R`, which works recursively (down all subfolders). Please clarify. – Stephan Jan 10 '18 at 15:36
  • I see.. I tried it just for simple folder (I didn't have any inside..). So I should remove /R? or can I use /F instead? Sorry for asking you many questions – Veronika Jan 10 '18 at 15:46
  • try just a plain `for`: `for %%F in (*.jpg) do ... – Stephan Jan 10 '18 at 16:26
  • Perfect!! it works correctly :)) Thank you very much, you saved me – Veronika Jan 10 '18 at 17:11

0 Answers0