基本问题:
如何获取最近创建的文件夹(在同一批次中完成),然后在文件夹中执行操作,然后使用时间戳重命名该文件夹并重复生成新时间戳的新文件?
我的批处理代码大纲以及预期结果与实际结果:
我正在为目录中的所有文件创建一个批处理文件,该文件将循环执行以下步骤。
步骤1(工作):遍历目录中的文件并提取数据,然后将这些数据输出到已创建的文件夹中,该文件夹必须名为“输出”(在此步骤中创建)。
步骤2(工作):我必须进入此“输出”文件夹并使用数据“处理”(我已经有一个脚本进入“输出”和“处理”的新文件路径)
步骤3(不起作用):将文件夹“ output”重命名为“ output_TimeStamp”(这是我的问题所在,我的循环采用创建的第一个文件夹的时间戳#1,并尝试将所有文件夹命名为timestamp#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
)
我的研究
我已经尝试了很多事情并使用了链接如何使用Windows批处理增加文件夹名称?和cmd行用日期和时间重命名文件。
我觉得写这个问题应该容易得多。
在不检查脚本逻辑的情况下,我修复了我已经在评论中提到的以下问题:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句