Batch File - Rename TXT file if file exists

March 2017




Issue


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.69shared 
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:CAINhelptickets%nameuser% 
set drive=S:CAINhelptickets%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 would be awesome.
Thanks in Advance

Solution


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:CAINhelptickets"
while ( NOT($found) )
do
if ($count==0)
        set $dir = "S:CAINhelptickets"+"/"+$username
    else
        set $dir = "S:CAINhelptickets"+"/"+$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

Note


Thanks to Richard.Williams for this tip on the forum.

Related


Published by aakai1056.
This document, titled "Batch File - Rename TXT file if file exists," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).