Solved Get files date stamp within for loop

Compaq / EVO
January 6, 2017 at 06:43:22
Specs: Windows 2008 R2, 3 ghz / 16 gig ram
Hi guys, please help me find the date stamp of a file within a for loop. Below is my code. Thank you. (Apologies if I am meant to embed code, it has been so long since I last posted - Please let me know how to embed if required.)

setLocal enableDelayedExpansion

for /f "delims=|" %%a in ('dir /b "\\sourceServer\path\"') do (
set file=%%a
::Find only files that have file extension .evt
if !file:~-4!==.evt (
::Find only files with today's datestamp (THIS IS NOT WORKING!)
IF DATESTAMP==TODAYSDATE (
copy "\\sourceServer\path\%%a" "\\destinationServer\path\"
) else (
REM Skipping file as this is not the current date file.
)
) else (
REM echo Skipping %%a as this is not a .evt file.
)
)


See More: Get files date stamp within for loop

Report •

✔ Best Answer
January 11, 2017 at 07:01:52
Hi Lee,

My script was a barebones punt.

::====== script starts here ===============
::
:: today2.bat 2017-01-10 9:45:45.60
@echo off & setLocal enableDELAYedeXpansioN

pushd SOURCE_PATH_HERE
@echo off > #.evt

:main
for /f "tokens=* delims= " %%a in ('dir/b/a-d/o-d *.evt') do (
call :sub1 %%~Ta %%a
)
goto :eof

:sub1
for /f "tokens=1-2* delims= " %%i in ('echo %*') do (
if defined STAMP if %%i equ !STAMP! echo copy "%%k" e:\mystuff
if not defined STAMP set STAMP=%%i
)
goto :eof
::====== script ends here =================

=====================

M2 Get custom script or take private lessons



#1
January 6, 2017 at 09:20:18
When processing files with FOR you can expand the variables to only contain certain data. In this case I've used %%~tA, which would be the timestamp of %%A, and %%~fA, which is the path name for %%A. You can also tell DIR to only list a certain type of file, *.evt. Since DIR is told to order the list by date, you can break the FOR loop once the timestamp no longer is equal to the current date, as there won't be any more files that have the current date as a timestamp.

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%A in ('dir /b /a:-d /o:-d "\\sourceServer\path\*.evt"') do (
	set timestamp=%%~tA
	set timestamp=!timestamp:~0,10!
	if !timestamp!==!date! (
		copy "%%~fA" "\\destinationServer\path\"
	) else (
		goto :break
	)
)
:break
pause

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)


Report •

#2
January 6, 2017 at 10:48:34
Thank you for responding. This doesn't work though. I originally marked this as answered because looking at the code it should have worked but it doesn't. The variable set timestamp=%%~tA isn't saving the timestamp. Any ideas?

Keep up the good work.
Lee

message edited by lee123abc


Report •

#3
January 9, 2017 at 19:11:30
::====== script starts here ===============
::
:: today.bat 2017-01-10 9:45:45.60
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in ('dir/b/a-d/o-d') do (
call :sub1 %%~Ta %%a
)
del NEWFILE
goto :eof

:sub1
for /f "tokens=1-2* delims= " %%i in ('echo %*') do (
if defined STAMP if %%i equ !STAMP! echo copy "%%k" e:\mystuff
if not defined STAMP set STAMP=%%i
)
goto :eof
::====== script ends here =================

=====================

M2 Get custom script or take private lessons


Report •

Related Solutions

#4
January 9, 2017 at 20:55:12
Different approaches - all work if tweaked, esp. for local-machine date/time setups. All fail if any local date/time format is not accounted for (just ask M2, ha ha!).
First, you might use Xcopy with the /d option (see: xcopy /?)
Second:
::------ start batch snip - run from source directory for testing then fix paths as needed
@echo off
set dest=\\somewhere
echo these files would be copied:
for /f "tokens=1,5" %%a in ('dir *.evt^|findstr "^%date%"') do (
echo %%a %%b
echo copy %%b %dest%
)
:: === end batch
Findstr helps eliminate the DOFW often delivered by %date%. The ^ helps eliminate the possibility of files like "01/09/2017" getting into the mix, since it stipulates that the string must be at the beginning of the line (see: findstr /?)

message edited by nbrane


Report •

#5
January 11, 2017 at 02:57:50
Hi guys, I hang my head in shame but I can't get either suggestion to work.

@nbrane: Thanks for your suggestion but %%b isn't returning anything (see copy below)
these files would be copied:
11/01/2017
copy D:\test\copyHere
11/01/2017
copy D:\test\copyHere
11/01/2017
copy D:\test\copyHere
11/01/2017
copy D:\test\copyHere
Press any key to continue . . .

@M2: I get a newfile created in the directory but I really can't follow your code (apologies). I tried adding a PAUSE to see what was happening but I am nowhere near the required level to understand what is going on in the script! Please could you break down what the steps do so I could attempt to edit the code to suit my needs?

Thanks guys, once again, I just need the .evt files in a directory that match the current day's date.


Report •

#6
January 11, 2017 at 04:07:56
If you're really using Win2008R2, it'd be easier to use PowerShell.
Get-ChildItem \\sourceServer\path\*.evt | 
 Where-Object { $_.LastWriteTime.Date -eq [datetime]::Today } |
 Copy-Item -Destination d:\test\copyHere

How To Ask Questions The Smart Way


Report •

#7
January 11, 2017 at 05:10:10
Lee,

The pause was a good idea; where did you put it?

Post the output of the script.

=====================

M2 Get custom script or take private lessons


Report •

#8
January 11, 2017 at 05:55:46
Hi M2:
I am a complete tool. I see you put an ECHO in front of the COPY command which I removed and viola! So thank you very much for your help.
I do need to know where I can include a source path, would this be:
('dir %SOURCE_PATH_HERE% /b/a-d/o-d')
Please would you explain the line:
if defined STAMP if %%i equ !STAMP! echo copy "%%k" e:\mystuff
...as I don't understand this. Also, I copied the bat file into the same folder as all the evt files and the bat file copies as well. This isn't an issue because when I put the code to work properly the script won't exist in the same directory as the files. However, do you know why this happens? I see you are only searching for .evt files so I am confused.

Hi Razor2.3:
Thanks for your suggestion and this does look easy to follow. I have a lot more in the batch script that I am sure could be rewritten in PS but for now it is all in a bat file. I don't know PS well enough to be calling snippets from command prompt. I know I need to learn this as I am an IT admin, I just like the old school stuff!
Cheers for your help guys.


Report •

#9
January 11, 2017 at 07:01:52
✔ Best Answer
Hi Lee,

My script was a barebones punt.

::====== script starts here ===============
::
:: today2.bat 2017-01-10 9:45:45.60
@echo off & setLocal enableDELAYedeXpansioN

pushd SOURCE_PATH_HERE
@echo off > #.evt

:main
for /f "tokens=* delims= " %%a in ('dir/b/a-d/o-d *.evt') do (
call :sub1 %%~Ta %%a
)
goto :eof

:sub1
for /f "tokens=1-2* delims= " %%i in ('echo %*') do (
if defined STAMP if %%i equ !STAMP! echo copy "%%k" e:\mystuff
if not defined STAMP set STAMP=%%i
)
goto :eof
::====== script ends here =================

=====================

M2 Get custom script or take private lessons


Report •

#10
January 11, 2017 at 09:54:20
WORKING! Thanks M2. Legendary.

Just wanted to add that I added a Del #.evt at the end for the first for loop.
for /f "tokens=* delims= " %%a in ('dir/b/a-d/o-d *.evt') do (
call :sub1 %%~Ta %%a
)
Del #.evt

message edited by lee123abc


Report •

Ask Question