windows 批处理 - 将创建日期/时间或随机附加到文件名的末尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4713831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
batch - append either date/time of creation or random to end of filename
提问by Maclovin
Damsel in distress needing help with a batch-script.
处于困境中的少女需要批处理脚本的帮助。
I have a bunch of files that one system creates. Either in one of 2 directories, or in the directory above that.
我有一堆由一个系统创建的文件。在 2 个目录之一中,或在其上方的目录中。
The naming is apparently not very important, so It's a bit random.
命名显然不是很重要,所以有点随意。
2 questions for you batch-geniuses out there.
有 2 个问题要问你批处理天才。
a) How can I append date/time of creation to the end of the filename with a batch-script? b) How can I append a random filename (so I make the files unique) with a batch-script?
a) 如何使用批处理脚本将创建日期/时间附加到文件名的末尾?b) 如何使用批处理脚本附加随机文件名(以便使文件唯一)?
Thanks in advance, dudes and dudettes.
提前致谢,伙计们和伙计们。
Yours sincerely, the Maclovin!
您真诚的,麦克洛文!
I have decided in my wisdom to not give a sh*t about the date of creation. I gather it follows the file anyway. What I want to do instead, is to append todays date/time to the file.
我以我的智慧决定不对创作日期发表任何评论。无论如何,我认为它遵循文件。我想做的是将今天的日期/时间附加到文件中。
This is what I have:
这就是我所拥有的:
SetLocal EnableDelayedExpansion
set kvitt1="c:\a"
set kvitt2="c:\b"
set tmpDir="c:\temp"
set LASTMOD=
set DATO=%DATE%
set KLOKKE=%TIME%
pause
REM lets go to this directory, and scan for files, and copy to the temp
pushd %kvitt1%
:ONE
for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do set lastmod=%%a
if "%lastmod%"=="" echo Could not locate files.&goto :TWO
COPY "%Lastmod%" %tmpDir%
pause
@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul
popd
@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul
REM Let's go to the next directory, and scan for files to copy to the temp
:TWO
REM G? til ny nettverksstasjon
pushd %kvitt2%
for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do set lastmod=%%a
if "%lastmod%"=="" echo Could not locate files.&goto :EOF
COPY "%LASTMOD%" %tmpDir%
pause
@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul
popd
REM we have copied the files we need, lets skip to the temp directory for renaming and filenaming
pushd %tmpDir%
echo %tmpDir%
pause
REM this is clearly not doing much.
REM g?setegn foran tmpDir fordi det kan finnes filer med mellomrom. dir/b lister opp filene i mappen, og lagrer det i filelist.txt
dir /b "%tmpDir%" >>filelist.txt
pause
REM g?r igjennom alle linjene i fillist.txt. %%i inneholder filnavnet
pause
REM for /f %%i in (filelist.txt) do
REM (
REM This is clearly not working
for /F "tokens=2,3,4 delims=/ " %%a in ('date /t') do set filedate=%%a-%%b-%%c
ren %filedate%_"%%T" "%T"
REM ren "%%T" %!random%"%%~nT.kvi")
pause
回答by msrxthr
Try this for a:
试试这个:
ECHO Making the copy...
COPY C:\file.txt c:\file_%time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%.txt
PAUSE
CLS
EXIT
The time parsing is done by (leave out the ECHO if using it within naming a file):
时间解析是通过以下方式完成的(如果在命名文件时使用 ECHO,则省略它):
ECHO %time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%
I got this from http://www.ozzu.com/mswindows-forum/can-batch-files-use-date-for-filename-creation-t75600.htmlwhere theres a bit more details about it.
我从http://www.ozzu.com/mswindows-forum/can-batch-files-use-date-for-filename-creation-t75600.html那里得到了这个,那里有更多关于它的细节。
As for b, the time here records up to seconds so appending the time will make it unique unless your doing more than 1 per second.
至于 b,这里的时间最多记录秒,因此附加时间将使其独一无二,除非您每秒执行 1 次以上。
EDIT: This gets only the current date. For the batch rename to file date try this instead:
编辑:这仅获取当前日期。对于批量重命名为文件日期,请尝试以下操作:
for /f "delims=|" %%f in ('dir /b C:\temp') do call :runsub %%f
goto EOF
:runsub
set t=%~t1
set t=%t::=%
set t=%t: =%
set t=%t:/=%
set renameto=%~n1-%t%%~x1
Copy %1 %renameto%
:EOF
PAUSE
You can change C:\temp to the path you want to change the files in. Let me know if theres any changes you need.
您可以将 C:\temp 更改为要更改文件的路径。如果您需要进行任何更改,请告诉我。
回答by ghostdog74
you are better off using vbscript (or powershell if you have) , rather than batch. this is because date manipulation in batch is dependent on regional settings of your computer. You will have to use tricks like going into the registry and stuff as workaround.
你最好使用 vbscript(或 powershell,如果你有),而不是批处理。这是因为批量日期操作取决于您计算机的区域设置。您将不得不使用诸如进入注册表之类的技巧作为解决方法。
Here's a vbscript you can adapt to your needs.
这是一个 vbscript,您可以根据自己的需要进行调整。
Set objFS = CreateObject( "Scripting.FileSystemObject" )
strFolder = WScript.Arguments(0)
Set objFolder = objFS.GetFolder(strFolder)
t = Now
strDate = Year(t) & Month(t) & Day(t) & Hour(t) & Minute(t) & Second(t)
' by random number
Randomize
upperbound = 99999
lowerbound = 55555
For Each strFile In objFolder.Files
strExtension = objFS.GetExtensionName( strFile )
strName = objFS.GetBaseName( strFile)
rand = Int((upperbound - lowerbound + 1) * Rnd + lowerbound) 'gen random number
' uncomment to use date
' strFile.Name = strName & strDate & "." & strExtension
' uncomment to use random number of your choosing
' strFile.Name = strName & rand & "." & strExtension
Next