在众多文件中获取最新文件,然后重命名最新文件名,并保留前33个字符 [英] Get Newest File Among Many then Rename the Newest File Name Keeping First 33 Characters
问题描述
这正是我所拥有的:在一个文件夹中,可以有许多名称相同但日期不同的文件:
AAAAAAA.BBB.CCC_DDD.EEEE.20200908 123945
AAAAAAA.BBB.CCC_DDD.EEEE.20200827 14411
This is exactly what I had: In one folder, there can be many files with same names but different dates:
AAAAAAA.BBB.CCC_DDD.EEEE.20200908123945
AAAAAAA.BBB.CCC_DDD.EEEE.2020082714411
我只需要获取最新文件即可将它通过SFTP传输到另一台服务器.
需要重命名最新的文件,并删除文件名的最后一部分.我应该只有日期戳,没有时间戳.就是说,只保留前33个字符.理想的结果是:
AAAAAAA.BBB.CCC_DDD.EEEE.20200908
I need to get only the newest file to SFTP it to another server.
The newest file needs to be renamed and removed the last part of file name. I should have date stamp only, no time stamp. That meant, keeping only the first 33 characters. The desired result is:
AAAAAAA.BBB.CCC_DDD.EEEE.20200908
请您帮忙吗?我可以确认1/.时间戳不同:更长或更短.那不是我的控制. 2/.文件没有扩展名.文件名看起来与我上面所述的完全一样.
Would you please help? I can confirm that 1/. the time stamps are different: longer or shorter. That was not under my control. 2/. the files had no extension. the file names look exactly like I described above.
我已经尝试过以下脚本,但是它不起作用:
I have tried these script however it did not work:
FOR /F %%I IN ('DIR c:\test\AAAAAAA.BBB.CCC_DDD* /B /O:D') DO SET NEWEST_FILE=%%I <br/>
echo "Newest Filename = %NEWEST_FILE%" <br/>
ren "%NEWEST_FILE%" "?????????????????????????????????"
推荐答案
@Compo,非常感谢您对我的帮助.多亏了您,它才有效.这就是我所做的:
@Compo, thank you very much for helping me. Thanks to you, it worked. And this is what I did:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F Delims^=^ EOL^= %%G In ('Dir /B /A:-D "<.<.<.>>>>>>>>>>>>>>>"') Do (
Set "DateStamp=%%~xG"
SetLocal EnableDelayedExpansion
Ren "%%G" "%%~nG!DateStamp:~,9!"
EndLocal
)
FOR /F %%I IN ('DIR c:\test\AAAAAAA.BBB.CCC_DDD.EEEE* /B /O:D') DO SET NEWEST_FILE=%%I
echo %NEWEST_FILE%
这篇关于在众多文件中获取最新文件,然后重命名最新文件名,并保留前33个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!