Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I would like to copy files from multiple sub directories to one folder so I used;
FOR /R "%1" %%G IN (*.JPG) DO COPY %%G C:\TEST\
However, if files of the same name exist they are overwritten.
How can I increment the filename or change the filename to something random as it isn't important?
I have tried multiple ways using the time as a random number generator but failed.
SET RANDOM=="%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%%TIME:~9,2%"
Please Help!

First, NT has a %random% built in.
For this purpose, incrementing wil work OK.
::== incNAME
@echo off
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in ('dir/s/b/a-d \files\*.txt') do (
set /a N+=1
echo ren "%%a" "!N!%%~Na.txt"
)
=====================================
If at first you don't succeed, you're about average.M2

well... almost
::==
@echo off
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in ('dir/s/b/a-d \files\*.txt') do (
set /a N+=1
echo copy "%%a" "c:\test\!N!%%~Na.txt"
)
=====================================
If at first you don't succeed, you're about average.M2

I can't seem to get this to work..
It's performing the command on each file but I don't understand your method of incrementation to fix this?
set /a N+=1 echo copy "U:\Visio Drawings\cisco6509.JPG" "c:\test\!N!cisco6509.jpg"
Missing operator.

this:
set /a N+=1 echo copy "U:\Visio Drawings\cisco6509.JPG" "c:\test\!N!cisco6509.jpg"
needs to be TWO lines within the FOR.
As iis, it doesn't DO the copy, it just previews it. To activate it, remove the ECHO.
set /a N+=1
copy "U:\Visio Drawings\cisco6509.JPG" "c:\test\!N!cisco6509.jpg"
=====================================
If at first you don't succeed, you're about average.M2

The original line was:
echo copy "%%a" "c:\test\!N!%%~Na.txt"
The fact that it expanded the %%a but not the !N! means that you did not put this line in your file:
setLocal EnableDelayedExpansion
Did you think that line was not important?

Thanks for all your help M2 it's working perfectly now!
setLocal EnableDelayedExpansion was in my file, it was simply a case of not being on two lines within the FOR as noted by M2.
Thanks again!

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |