-1

I'm attempting to rename all files in a folder via a batch script but I'm getting an incorrect syntax error that I can't seem to identify:

for %%f in (%Source%\*DebugLog.Config) do %%f ren %%f "%Source%\%ProjectName%.*DebugLog.Config"

The idea being that I prefix any file that ends in DebugLog.Config so that it becomes "projectName.DebugLog.Config"

Where is this going wrong?

DavidPostill
  • 162,382

2 Answers2

2

I'm getting an incorrect syntax error that I can't seem to identify

for %%f in (%Source%\*DebugLog.Config) do %%f ren %%f "%Source%\%ProjectName%.*DebugLog.Config".

There are several mistakes in your batch file (2 of which I made myself).

As pointed out to me in comments elsewhere by dbenham:

The REN target cannot include drive or path. It must be name and extension only

Use of the simple FOR runs the risk of renaming a file more than once - not good :-( Better to use FOR /F with DIR /B

See his answer add “text” to end of multiple filenames for more explanation on the above.

In addition do %%f is meaningless, it should be just do.

Here is a working batch file:

@echo off
pushd %source%
for /f "delims= eol=:" %%f in ('dir /b /a-d *DebugLog.Config') do ren "%%f" "%ProjectName%.%%f"
popd

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • dir - Display a list of files and subfolders.
  • for /f - Loop command against the results of another command.
  • popd - Change directory back to the path/folder most recently stored by the PUSHD command.
  • pushd - Change the current directory/folder and store the previous folder/path for use by the POPD command.
  • ren - Rename a file or files.
DavidPostill
  • 162,382
0

This is going wrong in more than one place. Assuming %Source% ends in a backslash (edit: it seems it doesn't, so adding the backslash:)

for %%f in (%Source%\*DebugLog.Config) do ren "%%f" "%ProjectName%.%%~nxf"

The ren target mustn't contain a path, so %%~nxf extracts Name and Extention of %%f.

ths
  • 602