-1

I need a script to copy files from a location which contains files from all month. The thing is that I need to copy only the files from yesterday to a temporary location. I've searched alot, but couldn't find a solution.

I have tried in this form. The first part show me the day from yesterday.

@echo off
Set _Source=V:\IngOnline
Set _Dest=D:\SPEtransfer\IngOnline\Temp
set log=D:\SPEtransfer\logIngOnlineTransfer.log
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "data=%yyyy%-%mm%-%dd%"

echo Yesterday was "%data%"
pause

forfiles /p %_Source% /d -1 "cmd /c copy %_Source%\*.* %_Dest%"
JokyFlow
  • 13
  • 3

1 Answers1

0

This is simplified replacement code which should copy to %_D% any file in %_S% which was modified yesterday only:

@Echo Off

Set "_S=V:\IngOnline"
Set "_D=D:\SPEtransfer\IngOnline\Temp"

For /F UseBackQ %%A In (`PowerShell^
 "(Get-Date).AddDays(-1).ToString('dd/MM/yyyy')"`) Do ForFiles /P "%_S%" /D^
 %%A /C "Cmd /C If @IsDir==FALSE If @FDate==%%A Copy @Path 0x22%_D%0x22>Nul"

You just need to ensure the format in ToString(' ') matches that required by your ForFiles command, (enter ForFiles /? in a Command prompt window to find it).

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Yes, something like that. I have tried the version you've made, but I receive the following error: D:\SPEtransfer>IngOnlineT-1copy.bat Yesterday was "2017-07-25" Press any key to continue . . . ERROR: Invalid argument/option - 'Cmd /C Copy @path 0x22D:\SPEtransfer\IngOnline \Temp0x22'. Type "FORFILES /?" for usage. – JokyFlow Jul 26 '17 at 11:33
  • Why have you got an incorrect/not seen previously space character after `IngOnline` and before `\Temp` in that output? Have you made a typo somewhere? – Compo Jul 26 '17 at 11:47
  • It is a typo, when I have copied from CMD I've entered a space by mistake. – JokyFlow Jul 26 '17 at 11:59
  • I'm guessing that you have replaced **`-1`** with **`%data%`** but the format you have used for `%data%` is not correct for `ForFiles` which expects `dd/MM/yyyy`. I have updated the code accordingly; is that what you intended to do? As a side not, if you do not wish to also identify directories modified you may need to use `"Cmd /C If @IsDir==FALSE Copy @path 0x22%_Dest%0x22"` – Compo Jul 26 '17 at 12:02
  • Tried this also. I get the same error... D:\SPEtransfer>IngOnlineT-1copy.bat Yesterday was "25/07/2017" Press any key to continue . . . ERROR: Invalid argument/option - 'Cmd /C Copy @path 0x22D:\SPEtransfer\IngOnline\Temp0x22'. Type "FORFILES /?" for usage. I don't understand what is wrong. I want to copy the files from yesterday from source to destination. – JokyFlow Jul 26 '17 at 12:06
  • Tried again with the update code I get now: D:\SPEtransfer>IngOnlineT-1copy.bat Yesterday was "25/07/2017" Press any key to continue . . . ERROR: Invalid date specified. Type "FORFILES /?" for usage. – JokyFlow Jul 26 '17 at 12:22
  • I have replaced &data& with -1 and now it seems to work. But now it is copying files from all days. Forfiles /P "%_Source%" /D -1 /C "Cmd /C Copy @path 0x22%_Dest%0x22" – JokyFlow Jul 26 '17 at 12:28
  • Enter `ForFiles /?` into your Command prompt window and show me the date format your version of `ForFiles` is looking for! Mine says "dd/MM/yyyy" format which matches the output you've given me. – Compo Jul 26 '17 at 12:29
  • I have changed my system date to dd/MM/yyyy from MM/dd/yyyy (thanks for advice!). Now it's copying the files from 25/07/2017 but I see also files from 26/07/2017 (today). – JokyFlow Jul 26 '17 at 12:39
  • I had previously told you that, some 37 minutes earlier and altered the code accordingly. That means you weren't really using my code, _you had changed it_. Please mark my answer as the accepted solution. – Compo Jul 26 '17 at 12:46
  • @Echo Off Set "_Source=V:\IngOnline" Set "_Dest=D:\SPEtransfer\IngOnline\Temp" Set "day=-1" For /F UseBackQ %%A In (`PowerShell "(Get-Date).AddDays(%day%).ToString('dd/MM/yyyy')"`) Do Set "_ED=%%A" Forfiles /P "%_Source%" /D %_ED% /C "Cmd /C If @IsDir==FALSE Copy @Path 0x22%_Dest%0x22" Pause I've used this and it's bringing me files from today also. In my source location there is a big amount of files from the past 3 months. – JokyFlow Jul 26 '17 at 13:14
  • You didn't report back the date format specification requested by your version of `ForFiles`, you need to match that format in `ToString(' ')`. Also the code you have in your previous comment does not match what I have in my **Edit**, _(note the opening and closing back ticks in the `For` loop parentheses)_. – Compo Jul 26 '17 at 14:15
  • I have completely rewritten the code in the hope that a simplified version will be less prone to error. You need to check the footnote, try it and provide feedback. – Compo Jul 26 '17 at 17:36
  • My forfiles format is : FORFILES [/C command] [/D [+ | -] {dd/MM/yyyy | dd}] Also, I have tried the updated version of the script and now it's working properly. Thank you very much for help! – JokyFlow Jul 27 '17 at 05:14