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
Related
I found a post where the user wanted to copy the 2 most recent log folders to another location answered by Mofi. I am attempting something similar except I have a folder that includes multiple types of troubleshooting logs. I have attempted to copy only specific logs with mixed success. I am able to copy two of the most recent logs for two of the log types but am having no success with the others. It also seems that using a pause in the batch is not stopping it to see any error messages.
I was successful in copying the localhost log and the server log. However, the diagnostic log does not copy or any other logs in the folder I attempt to move. All files in the folder are .log extensions but and after a certain size roll over to a name.log.date format, but that hasn't seemed to matter for the localhost and server logs. I have tried putting in pauses at the end of each block and even within the blocks but the batch doesn't stop so I do not get a chance to see any errors.
#echo off
mkdir N:\Copy_logs
setlocal EnableExtensions EnableDelayedExpansion
REM -----------------
REM localhostlog
REM -----------------
set FileCount=02
set "SourcePath=D:\applications\server\log"
set "TargetPath=N:\Copy_logs"
set "SourcePath=%SourcePath:/=\%"
set "TargetPath=%TargetPath:/=\%"
if not "%SourcePath:~-1%" == "\" set "SourcePath=%SourcePath%\"
if not "%TargetPath:~-1%" == "\" set "TargetPath=%TargetPath%\"
for /F "eol=| delims=" %%I in ('dir "%SourcePath%/localhost_access_log.*" /A-D /B /O-D 2^>nul') do (
%SystemRoot%\System32\xcopy.exe "%SourcePath%%%I" "%TargetPath%" /C /I /Q /H /R /Y >nul
set /A FileCount-=1
if !FileCount! == 0 goto serverLog
)
REM -----------------
REM serverLog
REM -----------------
:serverLog
set FileCount=02
set "SourcePath=D:\applications\server\log"
set "TargetPath=N:\Copy_logs"
set "SourcePath=%SourcePath:/=\%"
set "TargetPath=%TargetPath:/=\%"
if not "%SourcePath:~-1%" == "\" set "SourcePath=%SourcePath%\"
if not "%TargetPath:~-1%" == "\" set "TargetPath=%TargetPath%\"
for /F "eol=| delims=" %%I in ('dir "%SourcePath%/server.*" /A-D /B /O-D 2^>nul') do (
%SystemRoot%\System32\xcopy.exe "%SourcePath%%%I" "%TargetPath%" /C /I /Q /H /R /Y >nul
set /A FileCount-=1
if !FileCount! == 0 goto diagLog
)
REM -----------------
REM Diagnostic Log
REM -----------------
:diagLog
set FileCount=02
set "SourcePath=D:\applications\server\log"
set "TargetPath=N:\Copy_logs"
set "SourcePath=%SourcePath:/=\%"
set "TargetPath=%TargetPath:/=\%"
if not "%SourcePath:~-1%" == "\" set "SourcePath=%SourcePath%\"
if not "%TargetPath:~-1%" == "\" set "TargetPath=%TargetPath%\"
for /F "eol=| delims=" %%I in ('dir "%SourcePath%/diagnostic.*" /A-D /B /O-D 2^>nul') do (
%SystemRoot%\System32\xcopy.exe "%SourcePath%%%I" "%TargetPath%" /C /I /Q /H /R /Y >nul
set /A FileCount-=1
if !FileCount! == 0 goto :FileCopyDone
)
:FileCopyDone
endlocal
The expected result is the copying of two of the most recent files of each type of log. The first two types of logs copy but any of the remaining logs do not copy.
I would suggest that you use a sub function.
Quick untested example:
#Echo Off
Set "source=D:\applications\server\log"
Set "destination=N:\Copy_logs"
Set "number=2"
PushD "%source%" 2>NUL || GoTo :EOF
For %%A In ("localhost_access_log" "server" "diagnostic") Do If Exist "%%~A.*" Call :Sub %%A
PopD
GoTo :EOF
:Sub
For /F "Tokens=1*Delims=[]" %%A In (
'Dir /B/A-D/O-D/TW "%~1.*" 2^>NUL^|"%__AppDir__%find.exe" /N /V ""') Do (
If %%A GTR %number% Exit /B
"%__AppDir__%xcopy.exe" "%source%\%%B" "%destination%\" /Y)
I need to go through a list to copy the specified files into another location.
This is the used .bat file:
for /f "delims=" %%i in ('C:\Users\Documents\test\data\list_1.4.txt') do copy "C:\Users\Documents\test\data\Golden\%%i" "C:\Users\Documents\test\data\data_1.4"
But this does not work.
Any help?
I tried this too
#echo off
set src=C:\Users\Documents\test\data\Golden
set dst=C:\Users\Documents\test\data\data_1.4
set file=C:\Users\Documents\est\data\list_1.4.txt
for /F "usebackq tokens=*" %%a in ("%file%") do xcopy "%src%\*%%~a*" "%dst%" /C /Q /H /R /K /Y 1>nul 2>nul
pause
Based on your comment, the file content is something like:
"C:\Users\Documents\test\data\Golden\file1.txt"
"C:\Users\Documents\test\data\Golden\file2.pdf"
"C:\Users\Documents\test\data\Golden\file3.exe"
If that is the case, then:
#echo off
set "dest=C:\Users\Documents\test\data\data_1.4"
set "file=C:\Users\Documents\est\data\list_1.4.txt"
for /f "usebackq delims=" %%i in ("%file%") do (
if exist "%%~fi" copy /Y "%%~fi" "%destination%"
)
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"
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
I do have a xcopy statement in bat file..
would you please help me to append today's date to one of directories in destination
xcopy /S /E /I %sourceFolder% "C:\Shared\copy\%destinationFolder%"
today date is 06072013 so I want my destination look like below
C:\Shared\copy-today's date........
Thanks
This is method of getting a date stamp that doesn't depend on the regional settings. Wmic is available in Windows XP Pro and higher.
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
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 stamp=%YYYY%-%MM%-%DD%
md "C:\Shared\copy-%stamp%"
xcopy here...
xcopy /S /E /I %sourceFolder% "C:\Shared\copy-%date:/=%\%destinationFolder%"
Just use %date% in your command:
xcopy /S /E /I %sourceFolder% "C:\Shared\copy\copy-%date%"
Note: this will keep the date in the original format.
Assuming your local date format is Fri 06/07/2013 you can format it into 06072013 by cutting up the string like this:
%date:~4,2%%date:~7,2%%date:~10,4%
So the final command will be:
xcopy /S /E /I %sourceFolder% "C:\Shared\copy\copy-%date:~4,2%%date:~7,2%%date:~10,4%"
Something like this...
for /f "tokens=2-4 delims=/ " %%A in ('echo.%Date%') do set Dest=C:\Shared\copy-%%A%%B%%C
xcopy /S /E /I "%sourceFolder%" "%Dest%"