1.

Solve : Need a batch file, searching and renaming?

Answer»

Hi,
I need to make a batch file which accepts two params.
First param is a file name (it can have spaces) which needs to be found (e.g. DIR /S /B | FINDSTR /C:%1)
and the second param is NEW file name a searched file will have.

The batch can make a new batch file with a list of commands for example: rename "fullpath/filename1" "newfilename1", ETC , or can do renaming directly.

Big thanks for Your help!
Regards,
Krissyou can use ren command which will do exactly the same

anyway:

Code: [Select]@echo off

set fileSearch=%~1
set fileRename=%~2

ren "%fileSearch%" "%fileRename%"
but remember if you want filename with spaces you need "" OK,
but first param is a file name without full path. I assume, the batch should first find it and after this rename it (?).

Regards,
Krisssearching for file name in all DRIVES will take alot of time...

and what if there are many files with that name ?

It will search only one DRIVE and quite flat structure of directories. Names of files are uniqe.
So search for a file from user input and rename it from user input?

Is that what your after?Code: [Select]@echo off

set dirSearch=C:\CMD
set fileSearch=%~1
set renName=%~2

for /f "TOKENS=*" %%a in ('dir /b /a-h /s "%dirSearch%" ^| findstr /I "%fileSearch%"') do (
set fileToRename=%%a
)

if "%fileToRename%" equ "" (
echo.No Such File!
pause >nul
exit
)

ren "%fileToRename%" "%renName%"

EDIT:

save this as somebat.bat and use like this:

somebat "filename" "newfilename"

and use "" if filenames have spaces Quote from: devcom on April 27, 2009, 11:39:07 AM

...

This is exactly what I needed!

devcom, big thanks !

Regards,
Kriss


Discussion

No Comment Found