Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am currently trying to write a script to prefix 100's of files with a random number on a regualr basis.
I need to keep the file name integrity and just chnage the random number prefixing the files.
I have the script so close to working but get a syntax error which I was hoping someone might be able to help me resolve. I am finding it problomatic to troubleshoot as the CALL label only works when run as a batch file not in the command prompt!I have attached the script so far....
@echo off
REM Sets the batchname as the same as file name
set BatchName=%~f0
REM Sets the current directory to be the directory the batch file resides in.
set CurPath=%~dp0
echo CurPath=%CurPath%
for /R . %f in (. *.wmv) do (
set PathSpec=%%~dpf
set FileName=%%~nxf
call :PROCESS
)set PathSpec=
set FileName=
set BatchName=
set CurPath=goto EXIT
:PROCESS
if "%PathSpec%%FileName%\"=="%CurPath%" goto EXIT
if "%PathSpec%%FileName%"=="%BatchName%" goto EXIT
set random ="%RANDOM%"
set TempFileName="%random%%FileName%":~0%
echo %TempfileName%REM ** You'd want to remove the echo where
ren "%FileName%" "%TempFileName%"set TempFileName=
:EOF
REM stop
Thanks for you help in addvance....Andy

At first sight I suppose you missed to declare the :EXIT label and instead coded a :EOF one.
I suggest however you change the EXIT into EOF and do not declare :EOF as :EOF is the built in exit point for main and routines in NT scripts.
I hope I shot your trouble.

I agree with IVO.
Also, I don't see the point of this:
set random ="%RANDOM%"
When you do this:
echo %TempfileName%
aren't you getting double double quotes?
If at first you don't succeed, you're about average.M2

Thanks for your replies BUT I am sorry to say they have not worked.
The echo was there purely for troubleshooting I wanted to make sure the filename was working and correct and forgot to remove it DOH.
I have altered the EOF error but still get the same errors I have pasted them below.
I take it the invalid attempt to call batch label outside the batch script errors are purely because I am running the code in a command window and not as the batch file.
The error that is causing my isuue is the system cannot find the file specified one at the end for the ren......
C:\Documents and Settings\Administrator>cd C:\wmpub\WMRoot\vidtest\TEST1
C:\wmpub\WMRoot\vidtest\TEST1>@echo off
REM Sets the batchname as the same as file name
set BatchName=%~f0
REM Sets the current directory to be the directory the batch file resides in.
set CurPath=%~dp0
echo CurPath=%CurPath%
CurPath=%~dp0
for /R . %f in (. *.wmv) do (
More? set PathSpec=%%~dpf
More? set FileName=%%~nxf
More? call :PROCESS
More? )
Invalid attempt to call batch label outside of batch script.
Invalid attempt to call batch label outside of batch script.
Invalid attempt to call batch label outside of batch script.
Invalid attempt to call batch label outside of batch script.
Invalid attempt to call batch label outside of batch script.
Invalid attempt to call batch label outside of batch script.
Invalid attempt to call batch label outside of batch script.
Invalid attempt to call batch label outside of batch script.
Invalid attempt to call batch label outside of batch script.
Invalid attempt to call batch label outside of batch script.
Invalid attempt to call batch label outside of batch script.set PathSpec=
set FileName=
set BatchName=
set CurPath=goto :EOF
:PROCESS
if "%PathSpec%%FileName%\"=="%CurPath%" goto EXIT
if "%PathSpec%%FileName%"=="%BatchName%" goto EXIT
set random ="%RANDOM%"
set TempFileName="%random%%FileName%":~0%
REM ** You'd want to remove the echo where
ren "%FileName%" "%TempFileName%"
The system cannot find the file specified.set TempFileName=
:EOF

May be I'm limited by my pooor knowledge of English, but your posts sound to me quite obscure. Anyway the following statement is incorrect and then it leads to the failure of the Ren command
set TempFileName="%random%%FileName%":~0%
as a substring must be embraced by % not starting with ". And more why :~0% meaning that the full variable?
To continue it is unnnecessary to set random=%random% as the random variable is managed by the system itself.

"I am running the code in a command window and not as the batch file"
?????and how do expect to have a lable, if not in a batch file?

::== randREN.bat
@echo off
setlocal enabledelayedexpansionfor /f %%F in ('dir /a-d/b') do echo ren %%F !RANDOM!%%F
:: DONE
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |