Getting creation date of image files on a path - powershell

I have to get the creation time of image files saved on a folder (around 4500 files) in milliseconds to evaluate errors and delays in a telescope instrumentation set up. The normal file attributes give out the time only in seconds, but WMIC gives a more precise time. I can get the creation time of a single file using
wmic datafile where "Name=''C:\\Users\\...\\test\\test.fit'" get creationdate
But when I change the where clause to get creationdate of all files, I get either invalid query or no instances found. I use the following command:
wmic datafile where "path='C:\\Users\\...\\test\\'" get creationdate
The path does not have spaces in it. What is the source of 'No instances available' error when using Where clause with path?
I tried changing the double and single quotation marks, and testing on a shorter test path and different file formats. But it did not change the result.

To elaborate on my comment:
Also the default format doesn't output milliseconds while they are of course present, just use a format string to include them
| Select-Object #{n='CreationTime';e={$_.CreationTime.ToString('yyyyMMdd HH:mm:ss.fff')}}
wmic outputs a different time stripped off the DST (daylight savings time) information.
> Get-Item C:\test\foo.zip| Select-Object Name,CreationTime
Name CreationTime
---- ------------
foo.zip 2019-07-29 19:18:00
> wmic datafile where "Name='C:\\test\\foo.zip'" get creationdate
CreationDate
20190729181800.928368+060
> gi C:\test\foo.zip| Select #{n='CreationTime';e={$_.CreationTime.ToString('yyyyMMddHHmmss.ffffffzzz')}}
CreationTime
------------
20190729191800.928368+02:00

This Batch script is just used to create a .csv file with the delimiter ; with those informations :
"File name";"File path";"The date of creation of this file"
#echo off
mode con cols=70 lines=3 & Color 0A
Title Get FilePath with their CreationDate by Hackoo 2019
Set "Target_dir=%userprofile%\Pictures\AutoSaveScreenShot\"
REM IMPORTANT Don't forget the final backslash in the path of your folder\
Call :Check_backslash %Target_dir% Target_dir
Set "LogFile=%~dpn0.csv"
If Exist "%LogFile%" Del "%LogFile%"
SetLocal EnableDelayedExpansion
#For /f "tokens=1,2 delims=:" %%a in ('Dir "%Target_dir%" /s /b /o:n /ad') do (
REM Set "Drive=%%a"
REM IMPORTANT Don't forget the final backslash in the path of your folder\
Set "Folder_Path=%%b\"
Call :Check_backslash !Folder_Path! Folder_Path
Call :Add_backSlash !Folder_Path! Folder_Path
#For /f "Tokens=1,2 Skip=1 Delims= " %%a in ('WMIC DATAFILE WHERE "PATH='!Folder_Path!'" GET CreationDate^,Caption') do (
REM The output of WMIC is unicode !
REM The trailing <CR> can be removed by passing the value through another FOR /F loop.
REM This also removes the phantom "blank" line (actually a <CR>) ie The second for /f loop
#For /f "delims=" %%f in ("%%a") do (
Set "FileName=%%~nxa"
Set "FilePath=%%a"
set "DT=%%b"
REM Convert ISO date to Date
set "DT=!DT:~0,4!_!DT:~4,2!_!DT:~6,2!_!DT:~8,2!-!DT:~10,2!-!DT:~12,2!"
Call :Scanning "!FileName!"
echo "!FileName!";"!FilePath!";"!DT!">>"!LogFile!"
)
)
)
If Exist "!LogFile!" Start "" /MAX Notepad "!LogFile!" & Exit
::---------------------------------------------------------------------
:Add_backSlash <String> <Var to Set>
Rem Subroutine to replace the simple "\" by a double "\\" into a String
Set "MyString=%1"
Set "String=\"
Set "NewString=\\"
Call Set "%2=%%MyString:%String%=%NewString%%%"
Exit /b
::---------------------------------------------------------------------
:Check_backslash
Set "datapath=%1"
IF NOT "%datapath:~-1%"=="\" SET "%2=%datapath%\"
Exit /b
::---------------------------------------------------------------------
:Scanning <file>
Cls & Color 0A
echo(
echo "%~1" ...
goto :eof
::---------------------------------------------------------------------
So, the Output file is like this one :
"Capture_06_04_2018_08_38_28.gif";"c:\users\hackoo\pictures\autosavescreenshot\2018-04\capture_06_04_2018_08_38_28.gif";"2018_04_06_08-38-28"
"Capture_06_04_2018_08_39_29.png";"c:\users\hackoo\pictures\autosavescreenshot\2018-04\capture_06_04_2018_08_39_29.png";"2018_04_06_08-39-29"
"Capture_06_04_2018_08_41_22.jpeg";"c:\users\hackoo\pictures\autosavescreenshot\2018-04\capture_06_04_2018_08_41_22.jpeg";"2018_04_06_08-41-22"
"Capture_06_04_2018_08_44_36.png";"c:\users\hackoo\pictures\autosavescreenshot\2018-04\capture_06_04_2018_08_44_36.png";"2018_04_06_08-44-51"
"Capture_06_04_2018_14_43_09.jpg";"c:\users\hackoo\pictures\autosavescreenshot\2018-04\capture_06_04_2018_14_43_09.jpg";"2018_04_06_14-43-09"
"Capture_06_04_2018_14_46_53.png";"c:\users\hackoo\pictures\autosavescreenshot\2018-04\capture_06_04_2018_14_46_53.png";"2018_04_06_14-47-02"
"Capture_06_04_2018_14_47_12.png";"c:\users\hackoo\pictures\autosavescreenshot\2018-04\capture_06_04_2018_14_47_12.png";"2018_04_06_14-47-19"
"Capture_07_04_2018_02_44_53.jpg";"c:\users\hackoo\pictures\autosavescreenshot\2018-04\capture_07_04_2018_02_44_53.jpg";"2018_04_07_02-44-53"

Related

Trying to rename files based on file size

I am trying to rename some MP4 files based on file size of mp4 files in another directory. I want to name all files with identical sizes to same name. Meaning if the file size of the source file matches the size of file in the comparison directory, the source file is renamed to whatever the compared file is named. Because both directories need to be read recursively I'm thinking it would be easier to make a list for comparison with the info in it in 2 columns by using the DIR /s /b echo %%~zs>>filesizelist.txt command giving me a list like
123456789 movie.mp4
987654321 movie2.mp4
Then pass all source mp4s to the batch file and if %%~za matches a value in first column then ren the file to the
corresponding filename. Is this the best path? I tried to script it to work on the fly and that was both a no-go and the source of my 3 day headache(plus the reference list rarely changes and is obviously easily updated). Can someone please assist me with the script?
I do some test with my mp4, and the code works, and for you perform your test, you w´ll need change/put this 2 line above with the path to your folder/directory (one to keep and another to compare), by replacing in the line code is like this:
`set "_target_to_keeped=C:\Users\ecker\Videos\Target"`
`set "_target_to_rename=C:\Users\ecker\Videos\Ren_it"`
You need add the folder where are files to keep and files to rename (if size+name match) on same lines where are the 2 lines code up in this test (sorry not explain in good English, my English is not help me). By now, is late 01:53, i need sleep... yep! so, have nice code!
#echo off && setlocal enableextensions enabledelayedexpansion
cd /d "%~dp0"
set /a _cnt_in_looping= 1 - 1
set /a _cnt_files_size= 1 - 1
set "_target_to_keeped=C:\Users\ecker\Videos\Target"
set "_target_to_rename=C:\Users\ecker\Videos\Ren_it"
cd /d "!_target_to_keeped!"
for /f "tokens=* delims=^ " %%i in ('^<nul dir /o-d /on /b "*.mp4" 2^> nul ^| find "" /v /c') do set _cnt_in_looping=%%i
for /f "tokens=* delims=^ " %%i in ('^<nul dir /o-d /on /b "*.mp4"') do (
set "_file_now_keep=%%i"
set "_file_now_keeped=%%~zi %%i"
call :_to_compare_:
)
set /a _total_files_renamed=!_cnt_in_looping! - !_cnt_files_size!
set /a _total_files_n_chang=!_total_files_renamed! - !_cnt_in_looping! * -1
echo/Total of files renamed = !_total_files_renamed!
echo/Total of files n chang = !_total_files_n_chang!
endlocal
goto :_end_of_file_:
:_to_compare_:
if not exist "!_file_now_keep!" exit /b
for /f "tokens=*" %%I in ('^<nul dir /o-d /on /b "!_file_now_keep!"') do (
set "_file_now_compare=%%~zI %%I"
set "_path_now_compare=%%~dpI"
if "!_file_now_compare!" == "!_file_now_keeped!" (
rename "!_path_now_compare!\%%I" "%%~zI %%I"
echo/ rename "%%~I" "%%~zI %%I"
if ["!errorlevel!"]==["0"] call set /a _cnt_files_size=!_cnt_files_size! + 1
timeout /t 10
)
)
exit /b
:_end_of_file_:

Batch Script to check first 5 char vs MM-DD

I'm trying to build a script for one of our remote media players and am trying to get it to update once a file shows up in the dropbox. I need it to check the first 5 of the title against MM-DD and if they match then play the video in question. Playing the video is no issue, neither are the archives. My issue right now is that when I try to make a for loop for the files in the location I'm getting the syntax of the command is incorrect or "x" was not expected at this time. also, my date is being formatted like this: 05 -02, and I dont know why.
:::::::::::::::::::::::::::::::::::::::::::::: Get Date :::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Use WMIC to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
IF "%%~L"=="" goto s_done
Set _mm=00%%J
Set _dd=00%%G
)
:s_done
:: Pad digits with leading zeros
Set _mm=%_mm:~-2%
Set _dd=%_dd:~-2%
::Finalize Date
Set date=%_mm%-%_dd%
echo %date%
::::::::::::::::::::::::::: Check the downloads folder for completed video files :::::::::::::::::::::::::::::::
:: Loop through all files
:loop
for %%a IN ("dir \Dropbox\Trailer") DO (
::IF the name is not blank, get the first 5 characters of the video name
set first5=%a:~0,5%
::Compare those characters to the date
IF "%first5%" == "%date%" (
taskkill /im wmplayer.exe /t
::::::::::::::: Archive all previous Videos :::::::::::::
for /r %%i in ("dir \Dropbox\Trailer") do (
xcopy /s (%%i) "dir \Archived_Shows\"
)
ping localhost
"C:\Program Files\Windows Media Player\wmplayer.exe" "C:\Dropbox\Trailer\%%a" /fullscreen
:::::::::::::::::::::::::::::::: Exit if new video is running ::::::::::::::::::::::::::::::::::::::
exit
)
)
goto :loop
I'm not sure exactly what your script is supposed to be doing but here is an example based on my understanding.
#Echo Off
CD "Dropbox\Trailer" 2>Nul || Exit /B
For /F "Tokens=1-2" %%A In (
'WMIC Path Win32_LocalTime Get Day^,Month^|FindStr [1-9]'
) Do Set /A _dd=10%%A,_MM=10%%B
Set "DString=%_MM:~-2%-%_dd:~-2%"
:Loop
If Not Exist "%DString%*" (
Timeout 300 /NoBreak
GoTo Loop
)
TaskKill /IM WMPlayer.exe /T 2>Nul
Timeout 10 /NoBreak >Nul
XCopy "." "%~dp0Archived_Shows" /S /D /I
For %%A In ("%DString%*") Do (
"%ProgramFiles%\Windows Media Player\wmplayer.exe" "%%A" /FullScreen
)
GoTo Loop
If no matching file is found it currently checks for new ones every 300 seconds, (5 minutes). If a matching file is found, the loop only resumes once you close WMPlayer. You can change that timespan on line 11 as per your preference.
You are setting the variable %date%, which is a system dynamic variable. Attempting to modify the contents is an unrecommended act, so instead, use another name.
Also, I have simplified the get-and-trim MM-DD part, and it works properly for me.
for /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
IF "%%~L"=="" goto s_done
Set _mm=00%%~J
Set _dd=00%%~G
)
:s_done
::Finalize Date
Set titleDate=%_mm:~-2%-%_dd:~-2%
echo %titleDate%
pause

Strip date from filename using batch script

I have multiple files with dates on them I would like to strip.
exOpTimer01232018.txt
exOpProcess01232018.txt
exOpFac01232018.txt
exOpProd01232018.txt
I would like to have a batch script remove the date and leave result such as
exOpTimer.txt
exOpProcess.txt
exOpFac.txt
exOpProd.txt
These are monthly file and the date stamp changes every month.
I have tried doing
RENAME C:\temp\*????????.txt *.txt
But wasn't successful.
Example based on my comment:
#Echo Off
For /F "Delims=" %%A In ('Where .:exOp*.txt 2^>Nul') Do Call :Loop "%%A"
Pause
Exit
:Loop
Set "fName=%~n1"
Ren %1 "%fName:~,-8%%~x1"

Batch file check file get updated to today's date(System Date)

I want to create a batch to check if the file have been modified to today's date, what i did was to "bring in a system's date and compare it with the modified date, if they match, then trigger something. My batch file works well and displays two right dates, but the IF statement saying the date mismatch.
#ECHO OFF
for /f "tokens=1,2,3,4 delims=. " %%i in ('date /t') do set date=%%k%%j
echo %date%
pause
FOR %%a IN (D:\MyFile.txt) DO SET FileDate=%%~ta
set DATEONLY=%FileDate:~0,10%
echo %DATEONLY%
pause
if DATEONLY==date (
echo date ok
)
else (
cls
ECHO Wrong
)
PAUSE
There are the following problems:
do not use variable name date as this is a built-in variable containing the current date (type set /? for help);
the first for statement is useless, because %date% is already available;
the strings DATEONLY and date are compared literally in your if statement, you need to state %DATEONLY%==%date% instead;
the else statement must be in the same line as the closing parenthesis of the if body (type if /? for help);
So try this:
#ECHO OFF
echo %date%
pause
FOR %%a IN (D:\MyFile.txt) DO SET FileDate=%%~ta
set DATEONLY=%FileDate:~0,10%
echo %DATEONLY%
pause
if %DATEONLY%==%date% (
echo date ok
) else (
ECHO Wrong
)
PAUSE
Note: Regard that all those dates in the batch file are locale-dependent.
Here is a completely different approach:
forfiles /P . /M MyFile.txt /D +0 /C "cmd /C echo #fdate #file"
The forfiles command is capable of checking the file date. In the above command line, it:
walks through the current directory (.),
lists all files named MyFile.txt (of course there is onlyone),
but only if it has been modified +0 days after today,
and then executed the command line after the /C switch.
If MyFile.txt has been modified today (or even in future), the given command line is executed;
if it has been modified earlier than today, an error message is displayed and ERRORLEVEL is set to 1.
Notice that forfiles is not a built-in command and might not be available on your operating system.

Batch file to rename all files in a folder by adding current date/time to beginning of filename

I can't seem to understand how batch files add yyyy/mo/dd/hh/mm/ss to the beginning of filenames. (Using Windows 7) Accuracy to the second is important.
It doesn't actually have to be a batch file, it just has to be a small program which can be executed by Directory Monitor whenever I add files to a folder: http://brutaldev.com/page/Directory-Monitor.aspx
I only imagine that a batch file would be the simplest and most efficient approach, but any other suggestions are welcome.
I work with many sequentially numbered files with overlapping filenames and I need a quick way to rename them whenever I add them to a folder such that there will never be any file with the same name yet they will still remain in sequential order. This is how I thought of adding the current date and time to the beginning of the filename and why seconds are important, since I can easily add multiple sets to a folder in under a minute but certainly not under a second. It would be ideal if the batch file could ignore file extensions and simply add the current date/time to the beginning of any file added to the folder.
The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
:: this line will rename the files in the current folder which haven't already
:: been renamed by checking for the fullstamp format at the start of the line
:: but it will skip this batch file
for /f "delims=" %%a in ('dir /b /a-d ^|findstr /v "^[0-9]*-[0-9]*-[0-9]*_[0-9]*-[0-9]*-[0-9]*" ') do if /i not "%%a"=="%~nx0" ren "%%a" "%fullstamp% - %%a"
pause
#ECHO off
SETLOCAL
IF [%1] NEQ [] goto s_start
:: Author - Simon Sheppard, July 2003
:: Tested for Windows NT, 2K, XP
ECHO STAMPME.cmd
ECHO REName a file with the DATE/Time
ECHO.
ECHO SYNTAX
ECHO STAMPME TestFile.txt
ECHO.
ECHO STAMPME "Test File.txt"
ECHO.
ECHO STAMPME "c:\docs\Test File.txt"
ECHO.
ECHO In a batch file use CALL STAMPME ...
:: To change the filename format just change around the last line below
GOTO :eof
:s_start
SET _file=%~n1%
SET _pathname=%~f1%
SET _ext=%~x1%
::Get the date
:: note ISO 8601 date format would require 4 digit YYYY Year)
FOR /f "tokens=6-8 delims=/ " %%G IN ('NET TIME \\%computername%') DO (
SET _mm=%%G
SET _dd=%%H
SET _yy=%%I
)
:: Get the time
FOR /f "tokens=2-4 delims=:." %%G IN ('cmd /c "time<nul"') DO (
SET _hr=%%G
SET _min=%%H
SET _sec=%%I
GOTO :done
)
:done
ECHO Today is Year: [%_yy%] Month: [%_mm%] Day: [%_dd%]
ECHO The time is: [%_hr%]:[%_min%]:[%_sec%]
REN "%_pathname%" "%_hr%-%_min%-%_sec%#%_file%%_ext%"
This seems to work for me
I'd prefer solutions, that are not dependent to local settings (wmic gives always the same format):
#echo off
setlocal EnableDelayedExpansion
for %%a in (a.*) do (
for /f %%i in ( 'wmic os get localdatetime /value ^|find "Local"' ) do set %%i
set ldt=!LocalDateTime:~0,4!-!LocalDateTime:~4,2!-!LocalDateTime:~6,2!-!LocalDateTime:~8,2!-!LocalDateTime:~10,2!-!LocalDateTime:~12,2!-!LocalDateTime:~15,3!
echo seconds ### ren %%a !LocalDateTime:~0,14!%%a
echo milliseconds ### ren %%a !LocalDateTime:~0,18!%%a
echo with separator ### ren %%a !ldt!-%%a
)