参考increment folder name link,我想问一下如何将其从创建文件夹更改为创建文本文件?
以下是代码的编辑版本:
@echo off
@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
)
@echo off
setlocal enableDelayedExpansion
set "baseName=testing-%Year%%Month%%Day%-"
set "n=0"
for /f "delims=" %%F in (
'2^>nul dir /b /ad "%baseName%*."^|findstr /xri "%baseName%[0-9]*"'
) do (
set "name=%%F"
set "name=!name:*%baseName%=!"
if !name! gtr !n! set "n=!name!"
)
set /a n+=1
md "%baseName%%n%"
所以它就像这样。 " Hello-20140716-1"," Hello-20140716-2"可以通过双击此批处理文件来创建etc文件夹。但是如何将其从文件夹更改为文本文件?
我尝试将md "%baseName%%n%"
更改为Echo "Hello" > %baseName%%n%.txt
和Echo "Bye" > %baseName%%n%".txt
但这不起作用,因为它只输出"再见"进入hello-20140716-1.txt而不创建" Hello"你好 - 20140716-1.txt和"再见"你好-20140716-2.txt
答案 0 :(得分:2)
测试一下:
@echo off
For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
)
set "baseName=testing-%Year%%Month%%Day%-"
set "n=0"
:loop
set /a n+=1
if exist "%baseName%%n%.txt" goto :loop
type nul > "%baseName%%n%.txt"
echo "%baseName%%n%.txt" was created
pause
答案 1 :(得分:2)
问题标题是关于' 使用批处理脚本增加文本文件名',所以我在文件名中删除了时间戳,只是增加名称。
这是我的剧本:
@echo off
echo.
setlocal
set N=0
set FILENAME=baseFileName.%N%.anyExtension
:loop
set /a N+=1
set FILENAME=baseFileName.%N%.anyExtension
if exist %FILENAME% goto :loop
echo You can safely use this name %FILENAME% to create a new file
endlocal
<强>信用卡:强>
我根据@foxidrive的回答制作了自己的风味,这意味着99%的功劳归于她/他