Gli amministratori di rete che hanno dimestichezza con i batch file si saranno sicuramentre trovati nella condizione di salvare un file con la data e l'ora prima di cancellarlo. Ma come si puo' creare un file prendendo la data del giorno o semplicemente utilizzando dalla data ultima modifica del file?
Windows mette a disposizione la possibilità di estrarre dalla data e dall'ora corrente parte di stringa. vediamo come.
Il seguente esempio inizia con una echo per visualizzare l'intera data, la seconda riga memorizza nella variabile dd i primi due caratteri della stringa data ottenendo il giorno.
La seconda riga estrae il mese così via.
L'ultima riga visualizza la variabile filename che potremmo utilizzare per rinominare o copiare un file.
Ad esempio ren install.log %filename%.log
echo %date% set dd=%date:~0,2% set mm=%date:~3,2% set yyyy=%date:~6,4% echo %time% set hh=%time:~0,2% set hh=%hh: =0% set mm=%time:~3,2% set ss=%time:~6,2% set filename=%yyyy%-%mm%-%dd%_%hh%-%mm%-%ss% echo %filename%
NOTES
REM How to remove blank when hour is less than of 10 REM 1) replacing blank with Zero set hh=%hh: =0% REM 2) or just testing the substring if it's blank IF "%hh:~0,1%" == " " SET hh=0%hh:~1,1%
In alternativa si potrebbe provare a leggere la data ultima modifica del file...l'esempio seguente rilascia la data del file e la memorizza in fileDate
FOR /f "tokens=1,2* delims= " %%a in ('dir myfilename.log^|find /i " myfilename.log"') DO SET fileDate=%%a
echo here the that of the file: %fileDate
Oggi ho testato 14 metodi per ottenere una stringa data e ora da utilizzare come parte del nome del file. I metodi esposti non sono "regional compliant". Significa che se la data non è nel formato gg/mm/aaaa non funzionano.
Finalmente ho trovato un modo che ho adattato e modificato e che volentieri aggiungo a quest'articolo al fine di condividerlo.
Il file sembra grande, ma tolti i commenti abbiamo 7 righe di codice compreso l'echo off. I commenti iniziano con "due punti due punti" :: , di solito si usa REM.
Nei commenti ci sono un bel pò di informazioni su come utilizzarlo. Aggiungo soltanto che viene creato un file vbs nella temp e che non cancello volutamente, utile per vedere meglio com'è fatto. Togliete il commento :: dall'ultima riga e il file verrà cancellato.
@echo off
:: [GOAL]
:: Create the Enviroment Variable FileNameDateTime
::
:: [FEATURES]
:: - 14# - ANOTHER WAY - Current Date Time
:: - 14# is the title of scripts tested, it means I tested 14 scripts and
:: this is my favorite way to create a datetime for file name.
:: - Regional independent solution generating the ISO date format
:: - Use a temp vbs autogenerated to create a Datetime Regional Indeipendent
::
:: [RETURN]
:: Return something like this 2018-08-07_17-36-56
:: The value will be stored in the var FileNameDateTime
::
:: [HOW TO USE]
:: From another batch just make a call and use the var
:: @echo off
:: call SetFileNameDateTime.cmd
:: echo %FileNameDateTime%
::
:: [REFERENCES]
:: https://www.dostips.com/DtTutoFunctions.php#FunctionTutorial.CreatingAFunction
:: https://ss64.com/nt/syntax-getdate.html
::
:: [CREDIT]
:: https://ss64.com/nt/syntax-getdate.html
:: byman - Some changes by myself
::
:: Create the ~getCurrentDateTime.vbs
ECHO Dim dt > %TEMP%\~getCurrentDateTime.vbs
ECHO dt=now >> %TEMP%\~getCurrentDateTime.vbs
ECHO 'output format: yyyymmddHHnn >> %TEMP%\~getCurrentDateTime.vbs
ECHO wscript.echo ( (year(dt)*10000000000) + (month(dt)*100000000) + (day(dt)*1000000) + (hour(dt)*10000) + (minute(dt)*100) + (second(dt)*1) ) >> %TEMP%\~getCurrentDateTime.vbs
:: Run ~getCurrentDateTime.vbs and read output
For /f %%G in ('cscript /nologo %TEMP%\~getCurrentDateTime.vbs') do set _dtm=%%G
:: Date Time without any format: 201808071623
:: Set _yyyy=%_dtm:~0,4%& Set _mm=%_dtm:~4,2%& Set _dd=%_dtm:~6,2%& Set _hh=%_dtm:~8,2%& Set _nn=%_dtm:~10,2%
::
set FileNameDateTime=%_dtm:~0,4%-%_dtm:~4,2%-%_dtm:~6,2%_%_dtm:~8,2%-%_dtm:~10,2%-%_dtm:~12,2%
:: echo fileNameDateTime=%fileNameDateTime%
:: echo Date Time without any format: %_dtm%
:: del %TEMP%\~getCurrentDateTime.vbs
- have fun -
