1.

Solve : Please help with COPY/RENAME command?

Answer»

Hello,

Trying to get back into writing batch files, been several years. I have several files in a folder that I would like to copy/rename from one destination to another. The file names VARY, here are a few examples of the SOURCE file names:

200167-010-Model.jpg
200169-008M-Model.jpg
200169-012C WITH LEG LEAD-Model.jpg

I would like the destination file names to be the same, with the exception of removing "-Model". From the source files above, the destination file names would be:

200167-010.jpg
200169-008M.jpg
200169-012C WITH LEG LEAD.jpg


In all cases the "-Model" in the source name is at the end (IE. *-Model.jpg).


Thanks in advance for any help,
ChiSoxFanThis little snippet will simply list the copy operations but not actually perform them. When you are satisfied with the results, remove the word echo from the highlighted line. I named the source folder source, and the target folder target, change as necessary.

Quote

echo off
setlocal enabledelayedexpansion

for /f "tokens=* delims=" %%i in ('dir c:\source /b') do (
  set newName=%%i
  set newName=!newName:-model=!
  echo copy "c:\source\%%i" "c:\target\!newName!"


Good luck. 
C:\test>TYPE  chi.bat
echo off
setlocal enabledelayedexpansion

for /f "delims=" %%i in ('dir c:\test\source\*.jpg /b') do (
  set newName=%%i
echo newname=!newname!
  set newName=!newName:-model=!
echo newname=!newname!
 copy "c:\test\source\%%i" "c:\test\target\!newName!"

)
cd c:\test\target\
dir  *.jpg

Output:

C:\test> chi.bat
newname=200167-010-Model.jpg
newname=200167-010.jpg
        1 file(s) copied.
newname=200169-008M-Model.jpg
newname=200169-008M.jpg
        1 file(s) copied.
 Volume in drive C has no label.
 Volume Serial Number is 0652-E41D

 Directory of c:\test\target

12/15/2010  08:42 PM                 9 200167-010.jpg
12/15/2010  08:43 PM                 9 200169-008M.jpg
               2 File(s)             18 bytes
               0 Dir(s)  291,049,746,432 bytes free
C:\test>That worked great.

Thanks to both of you.



Discussion

No Comment Found