Reputation: 23
I am trying to remove a string from multiple file names in multiple directories. This is the file as with a few files being created so that I can check what values are actually being SET.
When I get to the rename
at the very bottom I get an error:
The syntax of the command is incorrect.
I don't see anything amiss in the _intf
and _fnlf
text files that get created. Is there some trick to using variables as the file path in a rename?
@ECHO off
echo Delete dir.txt?
PAUSE
del dir.txt
del item.txt
del file.txt
::paths for all directories in root (where filerename.bat is run)
::get root dir path
SET "_c=%CD%"
SET /A "counter=0"
::get dir list and concats root dir path before
FOR /F "tokens=*" %%A in ('DIR /on /b /a:d /p %svnLOCAL%') DO (
SET "_dirp=%_c%\%%A"
CALL :sub1
SET /A "_counter+=1"
)
::finds each file in dir
:sub1
::make file to check dir path
ECHO "%_dirp%" > dir.txt
FOR /F "tokens=*" %%B in ('DIR /b %_dirp%') DO (
ECHO "%%B" > item.txt
SET "_item=%%B"
SET "_filep=%_dirp%\"
CALL :sub2 %%~nB
PAUSE
)
ECHO "%_counter%"
EXIT /b 0
::builds file paths
:sub2
set "str=%*"
set "str=%str:[1]=%"
SET "_intf=%_filep%%_item%"
SET "_fnlf=%_filep%%str%"
CALL :sub3
EXIT /b 0
::Renames each file
:sub3
ECHO "%_intf%" > _intf.txt
ECHO "%_fnlf%" > _fnlf.txt
ren "%_intf%" "%_fnlf%.jpg"
EXIT /b 0
Upvotes: 2
Views: 5778
Reputation: 38622
Your error is because you're not using the REName
command correctly.
This can be noted by entering Ren /?
at the Command Prompt, which shows the syntax as:
REN [drive:][path]filename1 filename2
You however seem to be using:
REN [drive:][path]filename1 [drive:][path]filename2
…which will produce the error:
The syntax of the command is incorrect.
As a simple fix for your script, you'd need to change the following line:
set "str=%str:[1]=%"
to:
set "_fnlf=%str:[1]=%"
Then remove the line:
SET "_fnlf=%_filep%%str%"
If you wanted to tidy the code up you could probably change it to:
@Echo Off
Set "i=0"
For /F "Delims=" %%A In ('Dir /B/AD "%svnLOCAL%" 2^>Nul') Do (
Set /A i+=1
For /F "Delims=" %%B In ('Dir /B/A-D "%~dp0%%A\*[1]*" 2^>Nul') Do (
Set "$=%%~nB"
Call Ren "%~dp0%%A\%%B" "%%$:[1]=%%.jpg"
)
)
Echo "%i%"
Pause
Or Using DelayedExpansion:
@Echo Off
Set "i=0"
For /F "Delims=" %%A In ('Dir /B/AD "%svnLOCAL%" 2^>Nul') Do (
Set /A i+=1
For /F "Delims=" %%B In ('Dir /B/A-D "%~dp0%%A\*[1]*" 2^>Nul') Do (
Set "$=%%~nB"
SetLocal EnableDelayedExpansion
Ren "%~dp0%%A\%%B" "!$:[1]=!.jpg"
EndLocal
)
)
Echo "%i%"
Pause
In the two examples above, I've taken your comment, where filerename.bat is run
, to mean the directory in which this script, filerename.bat
, is located. If you meant the current working directory, which is not necessarily the same, you should replace the instances of %~dp0
above with %__CD__%
.
Additionally, as is not clear from the information you've provided, I have assumed that %svnLOCAL%
has already been defined.
Upvotes: 7