基本问题:
如何获取最近创建的文件夹(在同一批次中完成),然后在文件夹中执行操作,然后使用时间戳重命名该文件夹并重复生成新时间戳的新文件?
我的批次代码大纲和预期与实际结果:
我正在为目录中的所有文件创建一个批处理文件,该文件将循环执行以下步骤。
步骤1(工作):浏览目录中的文件并提取数据,然后将该数据输出到已创建的文件夹中,该文件夹必须命名为"输出" (在此步骤中创建)。
第2步(工作):我必须进入这个"输出"文件夹和"做东西"使用数据(我已经有一个脚本进入"输出&#34的新文件路径;"做东西")
步骤3(不工作):重命名文件夹"输出" to" output_TimeStamp" (这是我的问题,我的循环采用创建的第一个文件夹的时间戳#1,并尝试命名所有文件夹时间戳#1)
步骤4(半工作):返回步骤1处理下一个文件(循环直到所有文件都在目录中完成)
我的代码(至少我的一次尝试)
::Loop to perform tasks on files in current directory
for /R %%f in (*.mp4) do (
::Extracts data from file and leaves it in a created output folder
start "" /w Sample.exe --clip "%%f" --verbose 2 --outDir output
::This goes in created output folder and does stuff to data
start "" /w C:\Users\user\Documents\winPython\WinPython-64bit-3.4.4.2\python-3.4.4.amd64\python.exe "%CD%\DoStuff.py" "%CD%\output\folder\folder2\Do.file"
::This is supposed to rename the folder with a time stamp
rename output output-%date:~4,2%-%date:~7,2%-%date:~10,4%_at_%time:~0,2%%time:~3,2%
::This is what my research came to, which increments a number in the timestamp but doesnt work
set N=0
set FILENAME=output-%date:~4,2%-%date:~7,2%-%date:~10,4%_at_%time:~0,2%%time:~3,2%.%N%
:loop
set /a N+=1
set FILENAME=output-%date:~4,2%-%date:~7,2%-%date:~10,4%_at_%time:~0,2%%time:~3,2%.%N%
if exist %FILENAME% goto :loop
echo You can safely use this name %FILENAME% to create a new file
)
我的研究
我尝试了很多东西并使用了链接How do I increment a folder name using Windows batch? 和cmd line rename file with date and time 我觉得这应该比写出这个问题容易得多。
答案 0 :(得分:0)
不检查脚本的逻辑,我修复了my comment中已经提到的以下问题:
goto :Label
打破了带括号的代码块的上下文,您的for
循环构成了这样一个块;所以执行继续在:Label
,但在某种程度上它不再是一个块。您可以通过将包含goto
和:Label
的代码部分放在子例程中并通过call
命令调用它来解决此问题;这会隐藏goto
。::
注释在循环或其他带括号的代码块中使用时可能会导致意外行为;它们实际上构成无效标签。您应该使用rem
代替。date
,time
和FILENAME
,因为否则它们会一直扩展到解析整个循环时的值。所以这是修改后的代码:
setlocal EnableDelayedExpansion
rem // Loop to perform tasks on files in current directory
for /R %%f in (*.mp4) do (
rem // Extracts data from file and leaves it in a created output folder
start "" /w "Sample.exe" --clip "%%f" --verbose 2 --outDir output
rem // This goes in created output folder and does stuff to data
start "" /w "C:\Users\user\Documents\winPython\WinPython-64bit-3.4.4.2\python-3.4.4.amd64\python.exe" "%CD%\DoStuff.py" "%CD%\output\folder\folder2\Do.file"
rem // This is supposed to rename the folder with a time stamp
rename "output" "output-!date:~4,2!-!date:~7,2!-!date:~10,4!_at_!time:~0,2!!time:~3,2!"
call :SUB
echo You can safely use this name !FILENAME! to create a new file
)
endlocal
exit /B
:SUB
rem // This is what my research came to, which increments a number in the timestamp but doesnt work
set /a N=0
set "FILENAME=output-%date:~4,2%-%date:~7,2%-%date:~10,4%_at_%time:~0,2%%time:~3,2%.%N%"
:loop
set /a N+=1
set "FILENAME=output-%date:~4,2%-%date:~7,2%-%date:~10,4%_at_%time:~0,2%%time:~3,2%.%N%"
if exist "%FILENAME%" goto :loop
exit /B