简体   繁体   中英

batch - append either date/time of creation or random to end of filename

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.

The naming is apparently not very important, so It's a bit random.

2 questions for you batch-geniuses out there.

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?

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

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 %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.html where theres a bit more details about it.

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.

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.

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.

Here's a vbscript you can adapt to your needs.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM