Reputation: 213
I have .csv file with 3 columns, delimiter is "|", . I need to use it to rename files, last(3rd) column is current filename. First two field joined should be new file name. As in example, some fields contain space, don't know is that could be a problem or not. Files are in folder C:\all\ as well in sub folders in C:\all.
example:
New|Name 1|CurrentName1
CurrentName1
> New Name 1
@echo off
for /f "tokens=1,2,3 delims=|" %%A in (C:\all\c.csv) do (
for /R "C:\all\" %%C in ("%%~C") do (
ren "%%~C" "%%A %%B"
)
)
:END
when I test, it started renaming sub folders, not files. I don't want to rename sub folders only files, how to fix that?
Upvotes: 0
Views: 242
Reputation: 34909
A for [/R]
loop accesses the file system only when there is at least a wildcard *
or ?
in the set (that is the part in between parentheses behind the keyword in
). So for example, for %%I in (test.txt) do echo %%I
will always return test.txt
, independent on whether or not such a file exists in the current working directory.
Instead of for /R
to find a certain item in a directory tree you could use the dir
command, which does not require a wildcard to actually check the file system:
for /F "usebackq tokens=1-3 delims=| eol=|" %%I in ("C:\all\c.csv") do (
for /F "delims=" %%F in ('dir /S /B /A:-D "%%K" 2^> nul') do (
ECHO ren "%%F" "%%I %%J"
)
)
This code check the full file name against the list file. If you want to check the base name only, replace "%%K"
in the dir
command line by "%%K.*"
. If you want to keep the original file extension, replace "%%I %%J"
in the ren
command line by "%%I %%J%%~xF"
.
After having tested the code, remove the upper-case ECHO
in front of the ren
command.
Upvotes: 1