1

I have the following batch file which renames a text file by appending an underscore and characters from the contents of the file to the file name.

The contents are 5 characters from the first line in the file from position 56.

I can get this to work for a single file (1.spl) in my example, but I can't work out how to get this to run for all *.spl files in a folder.

@ECHO OFF
SETLOCAL EnableDelayedExpansion

for /F "delims=" %%i in (1.spl) do (  
  set Z=%%i
  goto BREAK1
)
:BREAK1

SET Z=%Z:~56,5%
RENAME 1.spl 1_%Z%.spl
Mofi
  • 46,139
  • 17
  • 80
  • 143
sstainy
  • 19
  • 5

1 Answers1

0

You could use another FOR loop which calls for each *.spl file a subroutine for renaming the file.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "delims=" %%# in ('dir *.spl /A-D /B 2^>nul') do call :RenameFile "%%#"
endlocal
goto :EOF

:RenameFile
for /F "usebackq delims=" %%I in (%1) do set "FirstLine=%%I" & goto GetNamePart
:GetNamePart
set "NamePart=%FirstLine:~56,5%"
if not exist "%~n1_%NamePart%%~x1" ren %1 "%~n1_%NamePart%%~x1"
goto :EOF

The FOR loop in main code - third line - uses command DIR to get a list of *.spl files before starting the rename operations. It is not advisable to use here just for %%# in (*.spl) do ... as the files in the current directory are renamed which means the directory entries change while FOR loop is running. It is strongly recommended to get first the complete list of *.spl files using DIR and then process this list independent on the file renames made in the directory.

The main FOR calls the subroutine RenameFile to process each file from the list.

A file is not renamed if there is already an existing file with same name. This is very unlikely but should be nevertheless checked first.

Please note that the batch code as is does not prevent for appending an underscore and the five characters from first line multiple times to a file name if the batch file is executed more than once on the directory. An additional IF condition would be needed to avoid multiple renames of files.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • ren /?
  • set /?
  • setlocal /?

The operator & as used on FOR loop in subroutine makes it possible to run multiple commands on a single command line, see Single line with multiple commands using Windows batch file for details.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Would it be safer to use `%%~I` instead of `%%I`? –  Aug 17 '17 at 05:11
  • @SteveFest In general yes, but in this case I doubt that the first line of a file is ever enclosed in double quotes. And even if the first line is enclosed in double quotes, using `%%~I` would remove first and last character from line which could result in wrong extraction of the 5 characters from column 57 (with first column having column number 1) as one character from line was already removed before. The questioner has not posted what a file usually contains as first line. So we can only assume that the code as posted works for the data in the *.spl files. – Mofi Aug 17 '17 at 05:28