Batch File Rename TXT File if file exists

Solved/Closed
Stickboy0916 - Jan 11, 2010 at 09:32 AM
Richard.Williams Posts 25 Registration date Saturday November 7, 2009 Status Member Last seen July 18, 2012 - Jan 30, 2010 at 02:52 PM
Hello,
I am writing a batch file for submiting helpdesk tickets. I need the script to see if the file that is going to be created already exists (%nameuser%) and if so then name the file that is going to be created %nameuser1% and consectutive after that 1,2,3,4,5 ect.

My code looks as follows:
@echo off
echo. HelpDesk Program Written By PFC Torrey, Stephen 7:46 PM 1/4/2010
color 3
echo.
echo.
echo.
::maps network drive
echo. Mapping Network Drive
net use s: \\10.10.4.69\shared
pause
echo.
echo.
echo.
echo.
::prompt for user name
echo "Who Is Submitting this Ticket?, (Last Name First Name NO SPACES )?"
set /p nameuser=
echo.
echo.
echo.
echo.
::makes dir off of user name prompt in mapped drive
mkdir s:\CAIN\helptickets\%nameuser%
set drive=S:\CAIN\helptickets\%nameuser%
echo.
echo.
echo.
echo.
:: prompt for unit
echo "What Unit do you belong to?"
set /p unit=
:: prompt for problem
echo "What are you having a problem with?, (Computer, Radio, BFT, Other)
set /p problem=
:: prompt for discription
echo "Please discribe the problem in detail,"
echo "including any steps needed to trigger the problem."
set /p discription=
:: prompt for building number
echo "What building number are you in?"
set /p buildingnumber=
::prompt for phone number
echo "Please provide a valid phone number, or extention"
set /p phonenumber=
echo.
echo. "Creating Ticket please stand-by"
pause
echo. %unit% > %drive%\%nameuser%.txt
echo. %nameuser% >>%drive%\%nameuser%.txt
echo. %problem% >>%drive%\%nameuser%.txt
echo. %discription% >>%drive%\%nameuser%.txt
echo. %buildingnumber% >>%drive%\%nameuser%.txt
echo. %phonenumber% >>%drive%\%nameuser%.txt
::Prints IP Configuartion to Ticket
echo. Creating IP Configuration
ipconfig /all >> %drive%\%nameuser%.txt
echo.
echo.
echo.
echo.
echo. "Your Ticket has been submitted, please stand-by for a S6 rep to contact you."
echo. "-PFC Torrey, Stephen R"
@pause



Any idea's would be awesome. Thanks in Advance
Related:

1 response

Richard.Williams Posts 25 Registration date Saturday November 7, 2009 Status Member Last seen July 18, 2012 14
Jan 30, 2010 at 02:52 PM
Here is one batch script that will find the latest directory called $usernameXXX.

# Script FindNextTicket.txt
var str username, dir ; var int count ; var bool found
cd "S:\CAIN\helptickets"
while ( NOT($found) ) 
do
    if ($count==0)
        set $dir = "S:\CAIN\helptickets"+"/"+$username
    else
        set $dir = "S:\CAIN\helptickets"+"/"+$username+makestr(int($count))
    endif
    af $dir
    if ( NOT ($fexists) )
    do
         set $found = true
         echo "The correct directory path is " $dir
    done
    endif
    echo -e "DEBUG: Directory " $dir " already exists."
    set $count = $count + 1
done



This batch script is in biterscripting. Save the script in file "C:/Scripts/FindNextTicket.txt". Then add the following commands in your batch script.

"C:/biterscripting/biterscripting.exe" "C:/Scripts/FindNextTicket.txt" username("%nameuser%") -oo "output.txt"

type putput.txt
1