Copy files from one folder to another except today's file.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gtsenthil
Posts: 7
Joined: 11 Oct 2013 08:47

Copy files from one folder to another except today's file.

#1 Post by gtsenthil » 11 Oct 2013 08:57

Hi ,
I have a folder with consist of log files.
I need to copy all the files to logarchive folder except today's log file .
I need to delete all the files except today's log file , the filename will be in YYYYDDMM format
Ex. 20131011.log

These are the two folders i have
C:\SystemLogs
C:\Systemlogs\Logarchive

Please help me to solve this issue.

Thanks
Senthil.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy files from one folder to another except today's fil

#2 Post by foxidrive » 11 Oct 2013 17:47

XP Pro and higher. This will move the log files from C:\SystemLogs to "C:\Systemlogs\Logarchive\"

Code: Select all

@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%"

pushd "C:\SystemLogs"
for /f "delims=" %%a in (' dir *.log /b /a-d ^|find /v "%YYYY%%MM%%DD%"  ') do (
move "%%a" "C:\Systemlogs\Logarchive\"
)
popd

gtsenthil
Posts: 7
Joined: 11 Oct 2013 08:47

Re: Copy files from one folder to another except today's fil

#3 Post by gtsenthil » 15 Oct 2013 07:53

Hi Foxi,
Sorry for the late reply. Gone for an vacation.
Thanks for your code. Its working as expected by the way what is the logic that you have built was bit confused. Can you explain me if you have time ?

With thankful,
Senthil E

gtsenthil
Posts: 7
Joined: 11 Oct 2013 08:47

Re: Copy files from one folder to another except today's fil

#4 Post by gtsenthil » 15 Oct 2013 08:04

Will it consider the Created date of the file or filename which contains the today's date.. i tested this code its deleting based on the Created date ?

gtsenthil
Posts: 7
Joined: 11 Oct 2013 08:47

Re: Copy files from one folder to another except today's fil

#5 Post by gtsenthil » 15 Oct 2013 08:17

This is the code which currently i have

IF EXIST D:\EXEDLL\MOVELOGS.R4.LOG COPY D:\EXEDLL\MOVELOGS.R4.LOG D:\EXEDLL\MOVELOGS.R5.LOG
IF EXIST D:\EXEDLL\MOVELOGS.R3.LOG COPY D:\EXEDLL\MOVELOGS.R3.LOG D:\EXEDLL\MOVELOGS.R4.LOG
IF EXIST D:\EXEDLL\MOVELOGS.R2.LOG COPY D:\EXEDLL\MOVELOGS.R2.LOG D:\EXEDLL\MOVELOGS.R3.LOG
IF EXIST D:\EXEDLL\MOVELOGS.R1.LOG COPY D:\EXEDLL\MOVELOGS.R1.LOG D:\EXEDLL\MOVELOGS.R2.LOG
IF EXIST D:\EXEDLL\MOVELOGS.R1.LOG DEL D:\EXEDLL\MOVELOGS.R1.LOG
REM *--------------------------------------------------------------------------------------------*
IF EXIST D:\SystemLog\download\NB\*.LOG COPY D:\SystemLog\download\NB\*.LOG D:\SystemLog\download\NB\logarchive\*.LOG
>> D:\EXEDLL\MOVELOGS.R1.LOG
REM *--------------------------------------------------------------------------------------------*
REM * THE FOLLOWING AUS COMMANDS ARE USED TO SLOW DOWN THE PROCESS
AUS
AUS
AUS
DIR
AUS
AUS
AUS
REM *--------------------------------------------------------------------------------------------*
IF EXIST D:\SystemLog\download\NB\logarchive\*.LOG
DEL D:\SystemLog\download\NB\*.LOG
>> D:\EXEDLL\MOVELOGS.R1.LOG
*--------------------------------------------------------------------------------------------*

EXIT

The code which is marked in red deleting all the log files including today's file. But i should keep todays log file in that folder rest should be moved.
How we can implement this logic over here

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy files from one folder to another except today's fil

#6 Post by foxidrive » 15 Oct 2013 08:28

gtsenthil wrote:Thanks for your code. Its working as expected by the way what is the logic that you have built was bit confused. Can you explain me if you have time ?



The code gets todays date and then moves every .log file from
"C:\Systemlogs\" to "C:\Systemlogs\Logarchive\"
which does not have todays date in the filename.

gtsenthil
Posts: 7
Joined: 11 Oct 2013 08:47

Re: Copy files from one folder to another except today's fil

#7 Post by gtsenthil » 22 Oct 2013 07:12

This code is not working in Windows 2000 Server .
Can you please help me ?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy files from one folder to another except today's fil

#8 Post by foxidrive » 22 Oct 2013 10:13

Post the code you have at the moment, and put it in code tags.

Also describe what problem you have, and what you see on the console screen.

gtsenthil
Posts: 7
Joined: 11 Oct 2013 08:47

Re: Copy files from one folder to another except today's fil

#9 Post by gtsenthil » 22 Oct 2013 10:51

I have Log archive folder in my SERVER ,
My *. Log files (Except today's log file) needs to be copied to log archive folder.
Source Folder :
C:\HOST

Destination :
C:\Host\Logarchive

Sample file Filename : 20131022.LOG
Your code is moving all the files to archive folder including today's file.
This code was perfectly worked on Windows XP. While I am moving this code for testing in server Its not giving the expected result.
Please help me to resolve.
"
IF EXIST C:\LOGS\MOVELOGS.R4.LOG COPY C:\LOGS\MOVELOGS.R4.LOG >> C:\LOGS\MOVELOGS.R5.LOG
IF EXIST C:\LOGS\MOVELOGS.R3.LOG COPY C:\LOGS\MOVELOGS.R3.LOG >> C:\LOGS\MOVELOGS.R4.LOG
IF EXIST C:\LOGS\MOVELOGS.R2.LOG COPY C:\LOGS\MOVELOGS.R2.LOG >> C:\LOGS\MOVELOGS.R3.LOG
IF EXIST C:\LOGS\MOVELOGS.R1.LOG COPY C:\LOGS\MOVELOGS.R1.LOG >> C:\LOGS\MOVELOGS.R2.LOG


REM *--------------------------------------------------------------------------------------------*

echo on
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%"


pushd "C:\HOST\"
for /f "delims=" %%a in (' dir *.log /b /a-d ^|find /v "%YYYY%%MM%%DD%" ') do (
move "%%a" "C:\HOST\logarchive"
)
popd"

gtsenthil
Posts: 7
Joined: 11 Oct 2013 08:47

Re: Copy files from one folder to another except today's fil

#10 Post by gtsenthil » 22 Oct 2013 10:54

Server OS : Windows 2000

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy files from one folder to another except today's fil

#11 Post by foxidrive » 22 Oct 2013 11:10

gtsenthil wrote:Server OS : Windows 2000


Win 2K doesn't have Wmic.exe so the date setting routine fails.

replace this code:

Code: Select all

echo on
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%"


pushd "C:\HOST\"
for /f "delims=" %%a in (' dir *.log /b /a-d ^|find /v "%YYYY%%MM%%DD%" ') do (
move "%%a" "C:\HOST\logarchive"
)
popd"



With this: see what it echoes to the screen before the pause command - it should be "20131022"

Code: Select all

@echo off
  :: date time using WSH/VBS
  :: datetime.bat V4.2
  ::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  @echo off
  set TmpFile="%temp%.\tmp.vbs"
  echo> %TmpFile% n=Now
  echo>>%TmpFile% With WScript
  echo>>%TmpFile% .Echo "set m1="   + monthname(month(n), true)
  echo>>%TmpFile% .Echo "set m2="   + monthname(month(n), false)
  echo>>%TmpFile% .Echo "set woy="  + CStr(datepart("ww", n))
  echo>>%TmpFile% .Echo "set year=" + CStr(Year(n))
  echo>>%TmpFile% .Echo "set yr="   + Right(Year(n),2)
  echo>>%TmpFile% .Echo "set month="+ Right(100+Month(n),2)
  echo>>%TmpFile% .Echo "set day="  + Right(100+Day(n),2)
  echo>>%TmpFile% .Echo "set hour=" + Right(100+Hour(n),2)
  echo>>%TmpFile% .Echo "set min="  + Right(100+Minute(n),2)
  echo>>%TmpFile% .Echo "set sec="  + Right(100+Second(n),2)
  echo>>%TmpFile% .Echo "set dow="  + WeekDayName(Weekday(n),1)
  echo>>%TmpFile% .Echo "set dow2=" + WeekDayName(Weekday(n))
  echo>>%TmpFile% .Echo "set iso="  + CStr(1 + Int(n-2) mod 7)
  echo>>%TmpFile% .Echo "set iso2=" + CStr(Weekday(n,2))
  echo>>%TmpFile% End With
  cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat"
  call "%temp%.\tmp.bat"
  del  "%temp%.\tmp.bat"
  del  %TmpFile%
  set TmpFile=

  set "stamp=%year%%month%%day%"

echo stamp="%stamp%"
pause

pushd "C:\HOST\"
for /f "delims=" %%a in (' dir *.log /b /a-d ^|find /v "%stamp%" ') do (
move "%%a" "C:\HOST\logarchive"
)
popd"

Post Reply