Batch Script issue assistance

I have this script I created to rename and move a file for a client.  The issue is that it works but it is not renaming the file correctly.

I have triple checked the lines in my commands but can't seem to see where and why it is inserting a "y".

Can I get a few fairs of eyes to help me find the issue?

TIA..

@ECHO OFF
setlocal
REM Clear Screen of information
cls
REM Prompt for Filename
:FileName

REM If fileName is blank go to Error prompt.
SET fileName=
SET /P Filename=Please enter your filename: 
IF "%fileName%"=="" (goto FileNameError) else (goto confirm)

REM Confirm file Renaming
:confirm
REM SET FileName=
SET /P FileName=Would you like to rename the file to Post-%FileName%.asc? (y or press n):
IF /i "%Filename%"=="y" (goto renamePost.asc) else IF "%FileName%"=="n" (goto success)

REM Rename File
:renamePOST.asc
rename c:\post.asc Post-%filename%.asc
echo ..file renamed to Post-%FileName%.asc
copy c:\Post-%FileName%.asc to c:\TM463
echo ...File copied to c:\TM463 directory

REM Filename is Blank Error MEssage
:FileNameError
ECHO You did not enter a filename.
SET FileNameRetry=
SET /P FileNameRetry=Retry? (y, then enter or press enter to exit):
IF /i "%FileNameRetry%"=="y" (goto Filename) else (go to success)

:success
echo SUCCESS!! The file has been rename to Post-%filename%.asc
echo SUCCESS!! The file has been copied to c:\TM463


:end

Open in new window

LVL 32
nappy_dThere are a 1000 ways to skin the technology cat.Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
It looks like you are reusing the FILENAME variable for two purposes, try using a different variable for the second prompt below so that you don't end up with Y in FILENAME.

SET /P Filename=Please enter your filename:
 . . .
SET /P FileName=Would you like to rename the file to Post-%FileName%.asc? (y or press n):


~bp
1
jmcgOwnerCommented:
Batch is not case-sensitive, so FileName, fileName, and filename are the same variable.

In your :confirm section, you re-use this variable to accept the user's confirmation.

---

Or maybe it's just getting cranky with you for not using past perfect tense correctly in the :success section.
0
Andrej PirmanCommented:
As I see you are doing real mess with all variants of "filename" variable, with lower and uppercase letters. Batch files are selectively cAseSenSitiVe, but I recommend you to stick with strict case sensitive syntax.

Try this (I haven't tested it, but just corrected variables):
@ECHO OFF
setlocal
REM Clear Screen of information
cls

:FileNameProc
REM Prompt for Filename
REM If fileName is blank go to Error prompt.
SET FileName=
SET /P FileName=Please enter your filename: 
IF "%FileName%"=="" (goto FileNameError) else (goto confirm)

:confirm
REM Confirm file Renaming
REM SET FileName=
SET /P FileNameConfirm=Would you like to rename the file to Post-%FileName%.asc? (y or press n):
IF /i "%FileNameConfirm%"=="y" (goto RenamePost) else IF "%FileNameConfirm%"=="n" (goto Success)

:RenamePOST
REM Rename File
rename c:\post.asc Post-%FileName%.asc
echo ..file renamed to Post-%FileName%.asc
copy c:\Post-%FileName%.asc to c:\TM463
echo ...File copied to c:\TM463 directory

:FileNameError
REM Filename is Blank Error MEssage
ECHO You did not enter a filename.
SET FileNameRetry=
SET /P FileNameRetry=Retry? (y, then enter or press enter to exit):
IF /i "%FileNameRetry%"=="y" (goto FileNameProc) else (go to Success)

:Success
echo SUCCESS!! The file has been rename to Post-%FileName%.asc
echo SUCCESS!! The file has been copied to c:\TM463

:end

Open in new window

1
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

oBdACommented:
Aside from the multiple use of the Filename variable already mentioned, you had two syntax errors.
Line 23: The "to" in the copy command is incorrect.
Line 32: "go to" must be "goto"
Then if someone enters anything other than "n", the file will be renamed, because you only check for y and n, and everything else will "fall through" to RenamePOST.
Anyway, this should work:
@ECHO OFF
setlocal
REM Clear Screen of information
cls
REM Prompt for Filename
:FileName

REM If fileName is blank go to Error prompt.
SET FileName=
SET /P FileName=Please enter your filename: 
IF NOT "%FileName%"=="" (goto Confirm)
ECHO You did not enter a filename.
SET Retry=
SET /P Retry=Retry? (y, then enter or press enter to exit):
IF /i "%Retry%"=="y" (goto FileName) else (goto end)

REM Confirm file Renaming
:Confirm
SET Confirm=
SET /P Confirm=Would you like to rename the file to 'Post-%FileName%.asc'? (y or press n): 
IF /i "%Confirm%"=="y" (goto renamePost.asc)
IF /i "%Confirm%"=="n" (goto Canceled)
goto :Confirm

:Canceled
ECHO You canceled the rename.
goto end

REM Rename File
:renamePost.asc
rename "c:\post.asc" "Post-%FileName%.asc"
echo ..file renamed to 'Post-%FileName%.asc'
copy "c:\Post-%FileName%.asc" "c:\TM463"
echo ...File copied to 'c:\TM463' directory
if errorlevel 1 (
	echo ERROR! Could not copy 'c:\Post-%FileName%.asc' to 'c:\TM463'.
) else (
	echo SUCCESS! The file has been renamed and copied to 'C:\TM463\Post-%FileName%.asc'
)

:end

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewCommented:
I've never been a fan of GOTO when I can avoid it, see what you think of this slightly different approach.

I also prefer to display the message about what I am getting ready to do before the actual command, rather than after it.  That way if an error occurs I have a better indication of where the script was.  So instead of this:

   rename c:\post.asc Post-%filename%.asc
    echo ..renamed file to Post-%FileName%.asc


I do:

   echo ..renaming file to Post-%FileName%.asc
    rename c:\post.asc Post-%filename%.asc


@echo off
setlocal

REM Clear Screen of information
cls

REM Prompt for filename to rename to (try 5 times, then exit if no file entered)
set Filename=
for %%I in (1,1,5) do (
    set /p "Filename=Please enter your filename: "
    if "%Filename%"=="" (
        REM No filename entered, see if they want to retry
        set FileNameRetry=
        set /p "FileNameRetry=Missing filename, retry? (Y, then enter or press enter to exit): "
        if /i "%FileNameRetry%"=="" (
            echo Quitting...
            goto :EOF
        )
    )
)

REM If still no filename exit now
if "%Filename%"=="" (
    echo No filename entered, quitting...
    goto :EOF
)

REM Confirm file Renaming
set Confirm=
set /P "Confirm=Would you like to rename the file to Post-%FileName%.asc? (y or press n): "
if /i "%Confirm%"=="y" (
    REM Rename Files
    :renamePOST.asc
    echo ..renaming file to Post-%FileName%.asc
    rename c:\post.asc Post-%filename%.asc
    echo ...copying file to c:\TM463 directory
    copy c:\Post-%FileName%.asc c:\TM463

    echo SUCCESS!! The file has been rename to Post-%filename%.asc
    echo SUCCESS!! The file has been copied to c:\TM463
) else (
    echo Warning, no rename performend...
)

Open in new window

~bp
0
nappy_dThere are a 1000 ways to skin the technology cat.Author Commented:
Thanks everyone.  It was shoot from the hip brainstorm work-around to get client working and help quickly automate and improve on a process they use everyday.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.