I want to copy the particular logfile generated for each day in one of my server which i have access to my local daily automatically.
I prepared the batch commands as follows but didn't work as expected.
echo off
SET logfiles=\\PWWAS0015\UMS_Logs\server1\ums\service-*.log
rem set var = C:\Users\L068699\Desktop\test\src
echo %logfiles%
copy %logfiles% C:\Users\L068699\Desktop\test\
set yesterday = [DateTime]::Today.AddDays(-1).ToString("yyMMdd")
echo %yesterday%
pause
The problem is that I cana ble to extract all the logs but couldn't get the log which are like service-2015-04-17.log. How can I extract this kind of log which are one day behind i.e. if today is 2015-04-24 I should get the previous day log file service-2015-04-23.log
try like this:
pushd \\PWWAS0015\UMS_Logs\server1\ums\
rem set var = C:\Users\L068699\Desktop\test\src
::echo service-*.log
for /f "usebackq" %%a in (`"powershell (Get-Date).AddDays(-1).ToString('yyyy-MM-dd')"`) do set yesterday=%%a
echo %yesterday%
copy service-%yesterday%.log C:\Users\L068699\Desktop\test\
pause
Eventually you'll need NET command to map the network drivre:
NET USE \\PWWAS0015\UMS_Logs /PERSISTENT:YES
(put the net use before the pushd if it does not work)
Try this, may help you..
#echo
set source1="\\xxx"
set dest="\\yyy"
pushd %source1%
for /f "usebackq" %%i in (`"powershell (Get-Date).AddDays(-1).ToString('yyyy-MM-dd')"`) do set yesterday=%%~i
copy "yourfilename %yesterday%.log" "%dest%"
popd
Related
I am trying to run a batch script on my local machine that will take care of some log archiving on some servers. I can access the servers via file explorer "\SERVERNAME\C$\SOME FOLDER." When I attempt to xcopy from the source to the destination locally and append a timestamp its like the TIMESTAMP variable doesn't store my date/time concatenation.
This is for windows 2012r2 servers, I've tried to append just the date\time to the end which works fine, however, its not the desired format I am looking for and it starts nesting the directory with the date but no time and it looks like a mess. :(
I've also tried to use the wmic however this is the first time I am writing a batch file to automate some tasks so all this has been a great learning experience.
I've tried to echo %TIMESTAMP% and nothing returns? I've even tried to add the concatenation (%CUR_YYYY%%CUR_MM%%CUR_DD%-%CUR_HH%%CUR_NN%%CUR_SS%) directly to the file directory and its doesn't work :(
REM Check to see if a service on the machine is stopped (it is always stopped by the time it gets here) before we move the files from the logging directory to a new one.
for /F "tokens=3 delims=: " %%H in ('sc \\REMOTESERVER query "SOME SERVICE NAME" ^| findstr " STATE"') do (
if /I "%%H" == "STOPPED" (
REM substring the date and time and then concat it together at the end to make the desired timestamp variable
set CUR_YYYY = %date:~10,4%
set CUR_MM = %date:~4,2%
set CUR_DD = %date:~7,2%
set CUR_HH = %time:~0,2%
set CUR_NN = %time:~3,2%
set CUR_SS = %time:~6,2%
set CUR_MS = %time:~9,2%
set TIMESTAMP = %CUR_YYYY%%CUR_MM%%CUR_DD%-%CUR_HH%%CUR_NN%%CUR_SS%
REM copy files from the servers source directory and then move the files to a newly created logging folder with a timestamp appened at the end
echo d | xcopy /f /y "\\REMOTE SERVER\src" "\\REMOTE SERVER\dest\Logging_%TIMESTAMP%" /E /I
REM delete the contents of the servers source directory to keep things nice and clean
pushd \\REMOTE SERVER\src && del . /F /Q popd
)
)
The expected result would look like:
SourceFolder on the server will be there but empty
DestinationFolder will have a new Logging folder created Logging_20190325010101 and within the newly created logging folder all the contents from the SourceFolder should be there.
You need to get rid of the whitespace before and after your = in your set commands, also, You need delayedexpansion in the codeblock with changing variables, and there is a better way to get rid of the colons and comma.
#echo off
setlocal enabledelayedexpansion
REM Check to see if a service on the machine is stopped (it is always stopped by the time it gets here) before we move the files from the logging directory to a new one.
for /F "tokens=3 delims=: " %%H in ('sc \\REMOTESERVER query "SOME SERVICE NAME" ^| findstr " STATE"') do (
if /I "%%H" == "STOPPED" (
REM substring the date and time and then concat it together at the end to make the desired timestamp variable
set "CUR_YYYY=%date:~10,4%"
set "CUR_MM=%date:~4,2%"
set "CUR_DD=%date:~7,2%"
set "mytime=!time::=!"
set "mytime=!mytime:,=!"
set "TIMESTAMP=!CUR_YYYY!!CUR_MM!!CUR_DD!-!mytime!"
REM copy files from the servers source directory and then move the files to a newly created logging folder with a timestamp appened at the end
echo d | xcopy /f /y "\\REMOTE SERVER\src" "\\REMOTE SERVER\dest\Logging_!TIMESTAMP!" /E /I
REM delete the contents of the servers source directory to keep things nice and clean
pushd \\REMOTE SERVER\src && del . /F /Q popd
)
)
To explain your issue however, when you set a variable, the whitespace comes as part of the variable.. So:
set variable = value
Will result in a variable with a trailing space %variable % and a value with a leading space <space>value So we always get rid of the whitespace and best to use double quotes to eliminate possible whitespace after the value. for instance:
set "variable=value"
which will create %variable% and value
Within parenthetical code blocks you have to delay expansion when retrieving variable values in the same block in which they were set. For example:
(
set "test=1"
echo [%test%]
)
... would echo "[]" because %test% was retrieved within the same parenthetical code block in which it was set. At the time %test% is evaluated, it has no value. You must delay expansion either by using setlocal enabledelayedexpansion or by using call echo [%%test%%] (or cmd /c or similar). When using setlocal enabledelayedexpansion, you delay expansion by using ! instead of % to denote variables. See setlocal /? and set /? in a cmd console for more information -- particularly the section of set /? that begins "Finally, support for delayed environment variable expansion has been added."
Also, it's much simpler and more locale agnostic to compose your timestamp using wmic os get localdatetime. Example:
for /f "delims=." %%I in ('wmic os get localdatetime /value ^| find "="') do set "%%~I"
echo %localdatetime%
That should result in %localdatetime% containing the current numeric value of YYYYMMDDHHMMSS.
I am trying to set a housekeeping for different type of file. PFB scenario.
I have below set of files in a network path(\NAS.domain.local\data\Arasan)
test.sql
test1.txt
test2.log
I am trying to rename the files as shown below and move the files to .\Archive\ directory
Test_15-12-2016_151428.sql
Test1_15-12-2016_151428.txt
Test_15-12-2016_151428.log
([Filename]_DD-MM-YYYY_HHMISS.[ext])
I have built the below batch script.
#echo ON
SET Log_Path=\\NAS.domain.local\data\Arasan
SET Script_Path=C:\Scripts
REM ###########################################REM
REM ## Do make any changes to the text below ##REM
REM ###########################################REM
cd %Script_Path%
pushd %Log_Path%
dir /b /a-d > %Script_Path%\list.txt
set HH=%TIME:~0,8%
for /F %%A in (%Script_Path%\list.txt) do (
setlocal EnableExtensions EnableDelayedExpansion
set ffname=%%A
set "fname=%%~nA"
set "fext=%%~xA"
ren !ffname! !fname!_%DATE%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%!fext!
endlocal
)
del %Script_Path%\list.txt
move *.* Archive\
popd
exit
Now my issue is it is working perfectly in my machine but when I transfer this to server and execute. It is
throwing "The syntax of the command is incorrect." Error.
And it is moving the files without renaming. As for as i know, file name and extension is not being assigned to the variable properly in below part
set ffname=%%A
set "fname=%%~nA"
set "fext=%%~xA"
Server OS: Windows server 2008 R2 Standard(SP1)
Can anyone please help me. Thanks a lot.
Here is the part of the code you should be using to get a consistent date and time output:
For /F "Skip=1" %%A In ('WMIC OS GET LocalDateTime') Do For %%B In (%%~nA
) Do Set "DTS=%%B"
Set "DTS=_%DTS:~6,2%-%DTS:~4,2%-%DTS:~,4%_%DTS:~-6%"
Echo( [%DTS%]
Pause
Just incorporate this into the rest of your codeā¦
#ECHO OFF
CLS
REM Start Backup
TITLE Backup
SETlocal EnableDelayedExpansion
REM Capture the date/time(right down to the second) and then assign it to a variable
SET yy=%date:~-4%
SET dd=%date:~-7,2%
SET mm=%date:~-10,2%
SET newdate=%dd%%mm%%yy%_%Time:~0,8%
SET newdate=%newdate::=%
SET foldername=svetlana_backup_%newdate%
REM Variables
SET drive=R:
SET sevenZip=%USERPROFILE%\7z.exe
SET destination=R:\Backup
ECHO Running Backup Batch File
ECHO Please Plug in %drive%
PAUSE
ECHO %foldername%
MKDIR %destination%\%foldername%
FOR /F "tokens=1,2 delims=," %%i IN (backuplist.txt) DO (
SET completeSource=%%i
SET completeDestination=%destination%\%foldername%\%%j
ECHO Source: "!completeSource:"=!"
ECHO Destination:"!completeDestination:"=!"
MKDIR "!completeDestination:"=!"
XCOPY "!completeSource:"=!" "!completeDestination:"=!" /E /F
)
REM Zip the folder using the 7z command line utility
%sevenZip% a -tzip %destination%\%foldername%.zip %destination%\%foldername%
REM Remove the unzipped backup folder
RMDIR /Q /S %destination%\%foldername%
PAUSE
EXIT
This is a backup batch file that I've been using for last couple of days. It worked well up until this morning. For some reason, when it creates the variable foldername, it contains a space in the string where there was none before. It ends up like this:
svetlana_backup_22092016_ 93829
The space between the dash and the 93829 was never there before until today for some reason. How would I remove it and prevent it from happening again?
You can parse the file-/foldername like this
set foldername=%foldername: =%
This will replace all spaces with an empty string
The problem was likely caused because the test was run at a time where the hour contained just one digit. With that %Time:~0,8% will output the time including an extra space, as the time will be stored like this: 9:38:29 which are 7 characters and you read the last 8 ones.
I need help to create a batch file to copy files with specifc date and version. And my date will be as of yesterday. Example file name:- abcde-20150811-v1.csv
I have tried xcopy with /d:08-11-2015 ( it picks all files with date modified as 08-11-2015 MM-DD-YYYY)
Is there a way that my batch automatically picks the date and gets changed everyday.
You might take a look at this: How to get and display yesterday date?
If I were you, I'd go with a scripting language like Perl, or use PowerShell.
Try this piece of code, haven't tried it yet though.
#echo off
set completepath=c:\users\microsoft\desktop\source
set destination=c:\users\microsoft\desktop\destination
set /a yesterday=%date:~4,2% - 1
set yesterday_date=%date:~10,2%%date:~7,2%%yesterday%
FOR /R %completepath% %%G IN (*.csv) DO call :process "%%~dpG" "%%~nG"
pause >nul
:process
SET %name%=%~2
SET chkname=%name:*%yesterday_date%=?%
IF "%chkname:~0,1%"=="?" (
xcopy %~1 %destination% /y
)
You may change the completepath and the destination variable.
I have a folder that gets a new file added everyday to the folder with the same file name but incremental extension such as .001, .002, .003, etc. However, if there's no file within the folder it starts at .001 again.
The problem is they are all named the same and if I move them to another folder to archive them it would just overwrite the same file over and over again. I could create a folder each day with the date with only one file in it, but that seems a bit redundant.
Is there a way to look at the create date of each file and rename it to the create date?
I've gotten this far, but it looks like for this situation I have to use a static file name, how to loop through the entire directory?
SET filename = C:\test.001
FOR %%f IN (%filename%) DO SET filedatetime=%%~tf
rename c:\test.001 C:\test_%filedatetime%.txt
move C:\*.txt C:\archive\
this provides the correct sort order:
#echo off &setlocal disableDelayedExpansion
set "startfolder=%userprofile%\test"
cd /d "%startfolder%"
for %%a in (*) do (
for /f "delims=." %%b in ('wmic datafile where "name='%startfolder:\=\\%\\%%~a'" get lastmodified^|find "."') do (
echo(ren "%startfolder%\%%~a" "%%~b.txt"
)
)
Remove echo to get it working.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "targetdir=c:\sourcedir"
SET "destdir=c:\destdir"
PUSHD "%targetdir%"
FOR %%a IN (*.*) DO (
SET "timestamp=%%~ta"
SET "timestamp=!timestamp:/=_!
SET "timestamp=!timestamp::=_!
SET "timestamp=!timestamp:.=_!
SET "timestamp=!timestamp:,=_!
SET "timestamp=!timestamp: =_!
ECHO MOVE "%%a" "%destdir%\%%~na.!timestamp!"
)
GOTO :EOF
This should work with any file in the nominated target directory where the name does not include ! or ^.
The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)
The gymnastics around timestamp are intende to replace /: with _ since these are illegal filename characters. Space., are similarly replaced - they're legal but often painful.
If you want the destination filename to be name.003.timestamp, remove the ~na from the destination name.
Try like this :
SET $path=The_path_who_contain_the_FILES
FOR /F "DELIMS=" %%f IN ('dir "%$path%" /a-d/b') DO (
SET filedatetime=%%~tf
move "%%~dpnxf" "C:\archive\test_%filedatetime%.txt")