Batch file to check time stamp of a file - windows

I am looking for a batch file script that can check the last modified time stamp of a file in a folder and check if it belongs to the current month & year.
I may have to check for multiple files in a single batch.
If it satisfies this condition, I need the batch file to copy them into a different folder.
Really appreciate any help.
Thanks.

You can try this:
#echo off
setlocal EnableDelayedExpansion
mkdir "someNewFolder"
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "compareDate=%dt:~0,6%"
FOR %%i IN (*) DO (
for /f "tokens=2,3 delims=-: " %%g in ("%%~ti") do if "%%h%%g"=="!compareDate!" copy "%%i" "someNewFolder\%%~nxi"
)
pause
EDIT
This should also work...
echo D | xcopy "." "someNewFolder" /D:02-01-2016 /Y
Or, not hardcoded:
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%"
echo D | xcopy "." "someNewFolder\." /D:%MM%-01-%YYYY% /Y
EDIT 2 To only copy files with a certain name from a certain directory use this:
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%"
echo D | xcopy "C:\Directory\Filename.*" "C:\someNewFolder" /D:%MM%-01-%YYYY% /Y
Or
echo D | xcopy "C:\Directory\Filename.*" "C:\someNewFolder" /D:02-01-2016 /Y
So something like this for your four different files:
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%"
echo D | xcopy "C:\ABC.txt" "C:\myname\Destination_folder" /D:%MM%-01-%YYYY% /Y
echo D | xcopy "C:\GHR.xls" "C:\myname\Destination_folder" /D:%MM%-01-%YYYY% /Y
echo D | xcopy "C:\DEF.csv" "C:\myname\Destination_folder" /D:%MM%-01-%YYYY% /Y
echo D | xcopy "C:\XYZ.txt" "C:\myname\Destination_folder" /D:%MM%-01-%YYYY% /Y
EDIT 3
And finally, a script that allows drag and drop of all files you want to check:
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%"
:loop
if "%~1"=="" goto :end
echo D | xcopy "%~1" "someNewFolder" /D:%MM%-01-%YYYY% /Y
shift
goto :loop
:end
pause
You can use this by dragging the files on top of the .bat file

Related

Batch: for files greater than, for files less than

I am looking to create a batch script that will take all image files that are larger than x, compress them in one .zip.
Then take all remaining image files that are less than that same x size and compress them in one .zip
I've tried several, several different ways and can't figure it out. I've searched on here and all mighty Google lol
My latest thoughts are listing the files that are larger in a text file temporarily and then using that list for 7zip to compress them, but can't figure out how to echo/print/list them to 7zip.
#ECHO OFF
SETLOCAL EnableDelayedExpansion
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
for /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
SET FOLDER=%~dp1
REM Sets current directory
SET LOG="log.txt"
pushd %FOLDER%
REM Changes directory to current
for %%a in ("%cd%") do SET NAME=%%~na
REM Gets the last directory name and sets it as a variable
SET ZIPNAME=%NAME% - Originals.zip
SET ZIPNAME2=%NAME% - Photos.zip
if exist ".smaller.txt" del /F ".smaller.txt"
if exist ".larger.txt" del /F ".larger.txt"
if exist "%ZIPNAME%" del /F "%ZIPNAME%" | echo %MYDATE%_%MYTIME%: %COMPUTERNAME% deleted %ZIPNAME%>>%LOG%
if exist "%ZIPNAME2%" del /F "%ZIPNAME2%" | echo %MYDATE%_%MYTIME%: %COMPUTERNAME% deleted %ZIPNAME2%>>%LOG%
REM Deletes old zips if they exists
FOR /F "usebackq delims=;" %%A IN (`dir /b *.jpg *.jpeg *.png *.gif *.bmp`) DO (
IF %%~zA LSS 1048576 (
ECHO %%A >>.smaller.txt
) ELSE (
ECHO %%A >>.larger.txt
)
)
FOR /F "tokens=* delims=" %%x IN (.smaller.txt) DO SET SMALLER=!SMALLER!%%x
FOR /F "tokens=* delims=" %%x IN (.larger.txt) DO SET LARGER=!LARGER!%%x
PAUSE
Here's my end product..
Thanks for all the help!
#ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /f "tokens=2-4 delims=/ " %%a IN ('date /t') DO (SET mydate=%%c-%%a-%%b)
FOR /f "tokens=1-2 delims=/:" %%a IN ("%TIME%") DO (SET mytime=%%a%%b)
REM My timestamp
SET FOLDER=%~dp1
REM Sets current directory
IF NOT EXIST %SYSTEMDRIVE%\Logs MKDIR %SYSTEMDRIVE%\Logs
SET LOG="%SYSTEMDRIVE%\Logs\.picture_log.txt"
REM Logging file and directory
PUSHD %FOLDER%
REM Changes directory to current
FOR %%A IN ("%CD%") DO SET NAME=%%~NA
REM Gets the last directory name and sets it as a variable
SET ZIP_O=%NAME% - Originals.zip
SET ZIP_R=%NAME% - Photos.zip
REM Zip file names
IF EXIST ".larger.txt" DEL /F ".larger.txt"
IF EXIST ".smaller.txt" DEL /F ".smaller.txt"
REM Delete old temporary files
IF EXIST "%ZIP_O%" DEL /F "%ZIP_O%" | ECHO %MYDATE%_%MYTIME%: %COMPUTERNAME% deleted %ZIP_O%>>%LOG%
IF EXIST "%ZIP_R%" DEL /F "%ZIP_R%" | ECHO %MYDATE%_%MYTIME%: %COMPUTERNAME% deleted %ZIP_R%>>%LOG%
REM Deletes old zips if they exists
FOR /F "usebackq delims=;" %%A IN (`dir /b *.jpg *.jpeg *.png *.gif *.bmp`) DO (
IF %%~zA LSS 1048576 (
ECHO %%A >>.smaller.txt
) ELSE (
ECHO %%A >>.larger.txt
)
)
REM Generate list of files, based off size
FOR /F "tokens=* delims=" %%x IN (.larger.txt) DO SET LARGER=!LARGER!%%x
FOR /F "tokens=* delims=" %%x IN (.smaller.txt) DO SET SMALLER=!SMALLER!%%x
REM Read lists, put contents into variables
"C:\Program Files\7-Zip\7z" a -mx9 -tzip "%ZIP_O%" #.larger.txt -sdel | ECHO %MYDATE%_%MYTIME%: %COMPUTERNAME% compressed %ZIP_O%>>%LOG%
"C:\Program Files\7-Zip\7z" a -mx9 -tzip "%ZIP_R%" #.smaller.txt -sdel | ECHO %MYDATE%_%MYTIME%: %COMPUTERNAME% compressed %ZIP_R%>>%LOG%
REM Compress images in separate .zips
IF EXIST ".larger.txt" DEL /F ".larger.txt"
IF EXIST ".smaller.txt" DEL /F ".smaller.txt"
REM Delete temporary files
The forfiles feature has a #fsize attribute, you can check.
For each file, which is large enough, you perform a 7z.exe a (add to archive).

store part of outcome command to variable

Hello dear people and others,
Today i wanted to create a simple script, thought it would be easy to store the outcome to var of the following command:
wmic bios get serialnumber | findstr /N /V SerialNumber
Outcome:
2:H3GK4S1
3:
The problem is when i try to get the serial with wmic, it returns the string as expected but also an empty string/line. When i try to store the serial to a variable it stores it and then directly overwrites it with the empty string. This is the function i nearly got working now:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber ^| FINDSTR /N /V SerialNumber') DO (SET serial=%g & ECHO %g)
And this gives the following output:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber ^| FINDSTR /N /V SerialNumber') DO (SET serial=%g & ECHO %g)
2:H3GK4S1
3:
As can be seen above, the loop overwrites the serial var, if someone can help me towards the right directon to get this working, would be mad.
At the Command Prompt:
For /F "Tokens=1* Delims==" %g In ('WMIC BIOS Get SerialNumber /Value') Do #For /F "Tokens=*" %i In ("%h") Do #Set "serial=%i" & Echo %i
Or in a batch file:
#For /F "Tokens=1* Delims==" %%g In ('WMIC BIOS Get SerialNumber /Value'
) Do #For /F "Tokens=*" %%i In ("%%h") Do #Set "serial=%%i" & Echo %%i
#Pause
EditIf you're happy to use a labelled section in your batch file:
#Echo Off
Set "serial="
For /F "Skip=1 Delims=" %%A In ('WMIC BIOS Get SerialNumber') Do If Not Defined serial Call :Sub %%A
Set serial 2>Nul
Pause
GoTo :EOF
:Sub
Set "serial=%*"
GoTo :EOF
Try like this:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber /format:value') DO for /f "tokens=* delims=" %# in ("%g") do set "serial=%#"
echo %serial%
Mind that's a command that should be executed in the command prompt directly.For a batch file you'll need to double the % in the for loop tokens.
In a batch file, you can also use a goto to end the loop after the first iteration :
#echo off
for /f "tokens=2 delims=:" %%a in ('wmic bios get serialnumber ^| findstr /N /V SerialNumber') do (
set "$var=%%a"
goto:next
)
exit/b
:next
echo Result=^> [%$var: =%]

Store randomized folder in a batch script?

I have a script that automatically backup NAPS2 (Scanner program) newest scan result to another drive, and also rename it according its timestamp...
Problem is, NAPS2 uses randomized folder name as the parentfolder of its scan result... e.g
"C:\Users\username\AppData\Roaming\NAPS2\recovery\4j4v.fbmv\scanresult001.jpg"
Here's the code, how do i point to that randomized folder?
SET ROOTDIR=C:\Users\Operator\AppData\Roaming\NAPS2\recovery\
for /f %%i in ('dir %ROOTDIR% /b /AD /od /t:w') do set LATEST_DIR=%%i >NUL
SET RANDOMDIR=%ROOTDIR%%LATEST_DIR%
for /F "delims=" %%a in ('dir /b /od "%RANDOMDIR%\*.jpg"') do set Youngest=%%a
echo Backing up %Youngest%
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
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 mydate=%YYYY%%MM%%DD%#%HH%%Min%%Sec%
copy "%Youngest%" "D:\Backup\%mydate%.jpg"
Output
C:\>for /F "delims=" %a in ('dir /b /od "C:\Users\Operator\AppData\Roaming\NAPS2
\recovery\jfwkui0s.pmd \*.jpg"') do set Youngest=%a
File Not Found
C:\>echo Backing up
Backing up
C:\>for /F "delims=" %a in ('wmic OS Get localdatetime | find "."') do set dt=%a
:\>set dt=20160818125756.950000+420
C:\>set YYYY=2016
C:\>set MM=08
C:\>set DD=18
C:\>set HH=12
C:\>set Min=57
C:\>set Sec=56
C:\>set mydate=20160818#125756
C:\>copy "" "D:\Backup\20160818#125756.jpg"
The system cannot find the path specified.
I think it's probably because the randomized folder has "." in it.. I tried "New Folder" which has space in it and it doesnt work as well
To do this use the approach shown in https://stackoverflow.com/a/30311479/6550457
You'll want to do it only for directories though so instead of a-d use ad
SET ROOTDIR=C:\Users\username\AppData\Roaming\NAPS2\recovery\
for /f %%i in ('dir %ROOTDIR% /b /AD /od /t:w') do set LATEST_DIR=%%i>NUL
echo Latest Dir = %LATEST_DIR%
so in your script you'd use it as:
SET ROOTDIR=C:\Users\username\AppData\Roaming\NAPS2\recovery\
for /f %%i in ('dir %ROOTDIR% /b /AD /od /t:w') do set LATEST_DIR=%%i>NUL
SET RANDOMDIR=%ROOTDIR%%LATEST_DIR%
echo Backing up %RANDOMDIR%
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
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 mydate=%YYYY%%MM%%DD%#%HH%%Min%%Sec%
copy "%RANDOMDIR%" "E:\Backup\%mydate%.png"

Copy a txt file with today date as file name (YYMMDD)

Everyday I create a data log with today's date (eg. ERR150921.txt). I need to copy this file to the server like a backup.
This is my current code:
SET ServerPath=\\IYA-PC\Shared
SET ClientPath=%USERPROFILE%\Documents\Data Log
echo.
echo Copying Files to Server...
FOR /F "delims=|" %%I IN ('DIR "%ClientPath%\*.txt" /B /O:F') DO SET NewestFile=%%I
xcopy "%ClientPath%" "%ServerPath%\%NewestFile%" /s /c /d /e /h /i /r /y
I got it from a post here. It copies the recently updated files.
The problem is that I do not need all the updated files. I just need to copy the file created today.
This sample will copy every files that have today date as name before extension. ie it will copy any of this;
"ERR150921.txt"
"21184150921.txt"
"2526150921.txt"
"29056-150921.txt
"150921.txt"
"ABCDEF-150921.txt"
.
#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 "_timestamp=%YY%%MM%%DD%"
SET "ServerPath=\\IYA-PC\Shared"
SET "ClientPath=%USERPROFILE%\Documents\Data Log"
echo.
echo Copying Files to Server...
setlocal enabledelayedexpansion
FOR /F "delims=|" %%I IN ('DIR "%ClientPath%\*.txt" /B') DO (
set "filename=%%~nI"
if "!filename:~-6!"=="%_timestamp%" (
echo xcopy "%%~I" "%ServerPath%\%%~nxI" /s /c /d /e /h /i /r /y
)
)
Get today's date, and transform it to your file name
SET ServerPath=\\IYA-PC\Shared
SET ClientPath=%USERPROFILE%\Documents\Data Log
echo.
echo Copying Files to Server...
SET FILE_NAME=ERR%date:~2,2%%date:~5,2%%date:~8,2%.txt
FOR /F "delims=|" %%I IN ('DIR "%ClientPath%\%FILE_NAME%" /B /O:F') DO SET NewestFile=%%I
xcopy "%ClientPath%" "%ServerPath%\%NewestFile%" /s /c /d /e /h /i /r /y

How to exclude the bat-file itself from the renaming

following scenario:
i have written a batch script whitch adds the datestamp to the filename. There are lots of different file-types in the folder.
#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 "datestamp=%YYYY%-%MM%-%DD%_"
:: naming like ISO 8601
pause
for %%i in (*.*) do ren %%i %datestamp%%%i*
is there a good way to exclude the bat-file itself from the renaming? It would be good enought if there is a way to exclude all .bat-files.
Your trailing * in the REN statement is not doing anything - it is not needed.
You should enclose the REN filenames in quotes in case you run into names with spaces.
Filtering out (skipping) your running batch file is easy. DIR /B can be piped to FINDSTR /V to get a list of files except for the running script. %~nx0 gives the file name and extension of the running script. FOR /F is used to capture and process the list of files.
for /f "eol=: delims=" %%F in ('dir /b /a-d ^| findstr /vixc:"%~nx0"') do ren "%%F" "%datestamp%%%F"

Resources