Reputation: 620
Can you please help me with my problem. I need to rename some files in a directory and must do it in a Windows batch file.
The files have the format VXXXX__<some-name>
where XXXX
can be any number. I need to rename the files to some-name removing the V9999__
. I have the following but it does not work for me
REM Get all files from directory
for %%f in (C:\data\\*.sql) do (
echo %%~nf
how to split the filename and rename the file ???
)
Upvotes: 1
Views: 804
Reputation: 70961
Use a for command, giving delimiters and token (substring) indication: the first token (%g) will be the text to the left of the underscore, and the rest of the text will be placed in %h (the next token)
edited to remove the non needed echo
for %%f in (*_*.sql) do for /f "tokens=1,* delims=_" %%g in ("%%~f") do echo ren "%%~f" "%%~h"
Upvotes: 1
Reputation: 80213
@ECHO OFF
SETLOCAL
for /f "tokens=1*delims=_" %%f in ('dir /b /a-d U:\data\*_*.sql') do (
echo REN U:\data\%%f_%%g %%g
)
GOTO :EOF
First, execute a dir
on the directory (I've changed the drive...) of files matching *_*.sql, in
/b(bare) format - name only, no headers and
/ad` - no directorynames.
Read each line with a FOR /F
, assigning the part before the _
delimiter to %%f
(token 1) and the remainder of the line after the delimiter (token "*") to the next metavariable (%%g)
Then do your rename - I've just echo
ed it
Upvotes: 1