I'm trying to write a .bat file that changes the location and name of some files I need to work with,(I will move them to the same folder the batch file is in) for the first one it works fine, but when the arguments of the function include parenthesis it crashes, I think there is probably an easy fix for this, but I have not been able to find it online in the last 3 days. Hope someone can help me with this, tanks!
Here is the code:
@echo off
set "grupo1c=Califica G1.xlsx"
set "grupo2c=Califica G2.xlsx"
set "grupo1cl=C22023 Calificaciones.xlsx"
set "grupo2cl=C22023 Calificaciones (1).xlsx"
set "carpeta=C:\Users\Jay\Desktop\"
call :funcionmovren "%grupo1cl%" "%grupo1c%"
call :funcionmovren "%grupo2cl%" "%grupo2c%"
pause
:funcionmovren
set "nombrelargo=%~1"
set "nombrecorto=%~2"
if exist "%carpeta%%nombrelargo%" (
echo The file "%nombrelargo%" exists. Proceding to move it.
move "%carpeta%%nombrelargo%"
if exist "%nombrecorto%" (
del "%nombrecorto%"
echo Renaming "%nombrelargo%" to "%nombrecorto%"
REN "%nombrelargo%" "%nombrecorto%"
echo Opening file "%nombrecorto%"
start "" "%nombrecorto%"
) else (
echo Renaming "%nombrelargo%" to "%nombrecorto%"
REN "%nombrelargo%" "%nombrecorto%"
echo Opening file "%nombrecorto%"
start "" "%nombrecorto%"
)
) else (
echo The file "%nombrelargo%" does not exist, opening last report.
if exist "%nombrecorto%" (
start "" "%nombrecorto%"
) else (
echo The file "%nombrecorto%" is not in the work folder, please add "%nombrelargo%" to downloads folder to update it.
)
)
goto :eof
I tried many things to find what was going wrong and finally I found out that the problem comes when the variable %nombrelargo% takes the name C22023 Calificaciones (1).xlsx, I think it has something to do wit parenthesis being present in the name but I have not found the solution so far.
I also tryed using
@echo off
setlocal enabledelayedexpansion
along with
call :funcionmovren "!grupo2cl!" "!grupo2c!"
but it did not solve te problem
Thanks for the help!
Your script causes errors but the cause is not the parenthesis because you already solved that by using double quoting your variables, especially within every echo command. What is causing errors is a missing exit command before the subroutine. In your code the subroutine is called three times!, 2 times because you call the subroutine with parameters and a third time because the script continues with the next lines after the pause command.
I have made some changes/additions to your script, mostly 'good practice' changes. I have added a check to prevent the script running from the desktop (your source directory for the move command). I have added the destination to the move command. Although destination does defaults to the current directory when omitted this is not documented in the help for the move command so it is better to specify the destination in your script. I have shortened the subroutine code by removing some duplicate code from the inner if statement. For more changes see the script below.
@echo off
set "carpeta=C:\Users\Jay\Desktop\"
if /i "%~dp0"=="%carpeta%" (
echo This script can not be run from the desktop!
exit /b
)
rem use setlocal/endlocal to prevent pollution of the environment with variables from previous runs of this script
setlocal
rem set current directory to script dir
pushd %~dp0
echo Current directory is %CD%
set "grupo1c=Califica G1.xlsx"
set "grupo2c=Califica G2.xlsx"
set "grupo1cl=C22023 Calificaciones.xlsx"
set "grupo2cl=C22023 Calificaciones (1).xlsx"
call :funcionmovren "%grupo1cl%" "%grupo1c%"
call :funcionmovren "%grupo2cl%" "%grupo2c%"
pause
rem restore original directory
popd
rem restore original environment
endlocal
exit /b
:funcionmovren
set "nombrelargo=%~1"
set "nombrecorto=%~2"
if exist "%carpeta%%nombrelargo%" (
echo The file "%nombrelargo%" exists. Proceding to move it.
rem I have added "." as destination, this is equivalent to, but shorter than "%CD%"
move "%carpeta%%nombrelargo%" "."
if exist "%nombrecorto%" del "%nombrecorto%"
echo Renaming "%nombrelargo%" to "%nombrecorto%"
REN "%nombrelargo%" "%nombrecorto%"
echo Opening file "%nombrecorto%"
start "" "%nombrecorto%"
) else (
echo The file "%nombrelargo%" does not exist, opening last report.
if exist "%nombrecorto%" (
start "" "%nombrecorto%"
) else (
echo The file "%nombrecorto%" is not in the work folder, please add "%nombrelargo%" to downloads folder to update it.
)
)
goto :eof