Computing.Net > Forums > Programming > Batchfile prefix multiple filenames

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batchfile prefix multiple filenames

Reply to Message Icon

Name: pro_pyrahana
Date: February 16, 2006 at 02:35:55 Pacific
OS: Windows 2003 SE SP1
CPU/Ram: Intel Pro III / 512 Mb
Product: Compaq DL380
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: February 16, 2006 at 03:01:12 Pacific
Reply:

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.


0

Response Number 2
Name: Mechanix2Go
Date: February 16, 2006 at 03:28:40 Pacific
Reply:

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


0

Response Number 3
Name: pro_pyrahana
Date: February 16, 2006 at 08:43:15 Pacific
Reply:

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


0

Response Number 4
Name: IVO
Date: February 16, 2006 at 10:08:01 Pacific
Reply:

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.


0

Response Number 5
Name: shahramsh
Date: February 17, 2006 at 16:06:03 Pacific
Reply:

"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?


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: March 3, 2006 at 03:19:52 Pacific
Reply:

::== randREN.bat
@echo off
setlocal enabledelayedexpansion

for /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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batchfile prefix multiple filenames

batch script to parse filenames www.computing.net/answers/programming/batch-script-to-parse-filenames/15286.html

remove spaces in multiple filenames www.computing.net/answers/programming/remove-spaces-in-multiple-filenames/14892.html

Multiple filenames in a cmd line www.computing.net/answers/programming/multiple-filenames-in-a-cmd-line/18464.html