我正在编写一个小程序以获取并保存用户输入,以便可以参考它来更改库存。它将名称另存为Asset_Change。如果前一个存在,我该如何在它将创建新文件名的位置创建它,因此在下次运行该程序时,输出文件将更改为Asset_Change_1或类似的内容。
@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
每次运行该程序时,当前都会将更改添加到当前的Asset_Change.txt文件中。我基本上希望每次运行时都能创建一个新文件。
答案 0 :(得分:1)
有一个简单的解决方案来编号文件。该代码的唯一缺点是,如果有人删除文件2,并且文件1和文件3存在,它将重新创建文件2。我不确定为什么您不选择使用带有文件名的日期和时间戳。
@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
答案 1 :(得分:0)
最终代码应类似于:
@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
这是我找到的最佳解决方案。我知道它很复杂,但是我无法进一步简化它。
输入cmd:
for /?
setlocal /?
if /?
set /?
goto /?
有关使用的命令的更多信息。