Reputation: 173
I am writing a little program to take and save user input so I can reference it for changes to inventory. It saves the name as Asset_Change. How do i make it where it will create a new file name if the previous one exists, so the next time the program is run, the output file would be changed to Asset_Change_1, or something along those lines.
@echo off
set Output="T:\Helpdesk\Documentation\HDI - Help Desk Inventory\Assets to File into 1213"
set /p Date_Sent=Date Sent:
set /p Item=Item:
set /p Model_Number=Model Number:
set /p Serial_Number=Serial Number:
set /p Item_Location=Inventory Location:
set /p Details=Details:
set /p RMA=RMA Number:
set /p Repair_Cost=Repair Cost:
set /p Shipping_Cost=Shipping Cost:
set /p Date_Received=Date Received:
echo Date Sent: %Date_Sent% >> %Output%\Asset_Change.txt
echo Item: %Item% >> %Output%\Asset_Change.txt
echo Model Number: %Model_Number% >> %Output%\Asset_Change.txt
echo Serial Number: %Serial_Number% >> %Output%\Asset_Change.txt
echo Item Location: %Item_Location% >> %Output%\Asset_Change.txt
echo Details: %Details% >> %Output%\Asset_Change.txt
echo RMA: %RMA% >> %Output%\Asset_Change.txt
echo Repair Cost: %Repair_Cost% >> %Output%\Asset_Change.txt
echo Shipping Cost: %Shipping_Cost% >> %Output%\Asset_Change.txt
echo Date Received: %Date_Received% >> %Output%\Asset_Change.txt
echo Thank you. This will now close.
pause
Every time the program is run, it currently adds the changes into the current Asset_Change.txt file. I'm basically looking to have it create a whole new file every time it is run.
Upvotes: 1
Views: 88
Reputation: 5504
The final code should look like:
@echo off
setlocal EnableDelayedExpansion
set "Output=T:\Helpdesk\Documentation\HDI - Help Desk Inventory\Assets to File into 1213"
set /p Date_Sent=Date Sent:
set /p Item=Item:
set /p Model_Number=Model Number:
set /p Serial_Number=Serial Number:
set /p Item_Location=Inventory Location:
set /p Details=Details:
set /p RMA=RMA Number:
set /p Repair_Cost=Repair Cost:
set /p Shipping_Cost=Shipping Cost:
set /p Date_Received=Date Received:
if exist "%Output%\Asset_Change.txt" (
set "file=Asset_Change.txt"
) else (
set "file=Asset_Change_1.txt"
goto :calc
)
:calc
if exist "%file%" (
goto :out
) else (
for /F "tokens=3 delims=_." %%A IN ("%file%") do set "num=%%A"
set /a "num+=1"
set "file=Asset_Change_!num!.txt"
)
goto :calc
:out
(
echo Date Sent: %Date_Sent%
echo Item: %Item%
echo Model Number: %Model_Number%
echo Serial Number: %Serial_Number%
echo Item Location: %Item_Location%
echo Details: %Details%
echo RMA: %RMA%
echo Repair Cost: %Repair_Cost%
echo Shipping Cost: %Shipping_Cost%
echo Date Received: %Date_Received%
)>>"%Output%\%file%"
echo Thank you. This will now close.
pause
This is the best solution I found. I know it is complicated, but I couldn't further simplify it.
Type in cmd:
for /?
setlocal /?
if /?
set /?
goto /?
for more information about the commands used.
Upvotes: 0
Reputation: 14290
There is a simple solution to number up a file. The only drawback to this code is if someone deletes file 2 and File 1 and File 3 exists it will recreate file 2. I am not sure why you are not choosing to use a date and time stamp with the file name.
@echo off
set "Output=T:\Helpdesk\Documentation\HDI - Help Desk Inventory\Assets to File into 1213"
set /p Date_Sent=Date Sent:
set /p Item=Item:
set /p Model_Number=Model Number:
set /p Serial_Number=Serial Number:
set /p Item_Location=Inventory Location:
set /p Details=Details:
set /p RMA=RMA Number:
set /p Repair_Cost=Repair Cost:
set /p Shipping_Cost=Shipping Cost:
set /p Date_Received=Date Received:
SET "num="
set "us="
:loop
IF EXIST "Asset_Change%us%%num%.txt" (
set /A "num+=1"
set "us=_"
goto loop
)
set "file=Asset_Change%us%%num%.txt"
(
echo Date Sent: %Date_Sent%
echo Item: %Item%
echo Model Number: %Model_Number%
echo Serial Number: %Serial_Number%
echo Item Location: %Item_Location%
echo Details: %Details%
echo RMA: %RMA%
echo Repair Cost: %Repair_Cost%
echo Shipping Cost: %Shipping_Cost%
echo Date Received: %Date_Received%
)>>"%Output%\%file%"
echo Thank you. This will now close.
pause
Upvotes: 2