1

I have a file by the name yyyymmddFile1.txt where date prefix is added in a particular format.

I need to copy that file from one location to another and I need to pick today's and yesterday's file

For today, I wrote

SET TODAY=%date:~10,4%%date:~4,2%%date:~7,2%

SET MAINPATH=D:\%TODAY%File1.txt
if exist %MAINPATH% (
COPY D:\%TODAY%File1.txt D:\NDM\InFileQueue\%TODAY%File1.txt
)

so, This will set the filename to 20190502File1.txt

But I can't figure out how to pass yesterday's date as a variable. I need to do something like this -

SET YESTERDAY= ??

SET MAINPATH=D:\%YESTERDAY%File1.txt
if exist %MAINPATH% (
COPY D:\%YESTERDAY%File1.txt D:\NDM\InFileQueue\%YESTERDAY%File1.txt
)
Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
geek
  • 65
  • 9
  • The problem with what you want happens on the first of every month. So you will have to do date maths with strings. Choose an appropriate technology, batch is not it. – Noodles May 02 '19 at 05:42
  • There's no reason why a [search of this site](https://stackoverflow.com/search?q=%5Bbatch-file%5D+yesterday%27s+date) couldn't have gotten you a satisfactory answer, because there are too many duplicates to suggest only one. – Compo May 02 '19 at 07:31
  • Also, you really do not need to use any of those methods because knowing the date yesterday is not needed for the task. You could simply use `RoboCopy` with its `/MaxAge:2` option, or perform the `Copy` through `ForFiles` with `/D -1`. Open a Command Prompt window and enter each command followed by `/?` to find out how to use them. – Compo May 02 '19 at 08:00
  • 1
    Possible duplicate of [How to use previous date in batch script with yyyymmdd format](https://stackoverflow.com/questions/48463044/how-to-use-previous-date-in-batch-script-with-yyyymmdd-format) –  May 02 '19 at 11:36
  • 1
    lots of duplicates if you've searched before asking this: [How to get and display yesterday date?](https://stackoverflow.com/q/2954359/995714), [Date arithmetic in cmd scripting](https://stackoverflow.com/q/355425/995714), [Subtract days in batch file](https://stackoverflow.com/q/19980034/995714), [How to get 3 days past date from current date Using Batch file](https://stackoverflow.com/q/22191084/995714)... – phuclv May 02 '19 at 13:41

3 Answers3

0

You can incorporate vbs and get a proper date format.Here you simply have to change the value of set day=-1 to toggle the number of days you want to remove from today's date.

@echo off
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 "final=%yyyy%%mm%%dd%"
echo Just doing an echo to screen of %final% for testing.
set "MAINPATH=D:\%final%File1.txt"
if exist %MAINPATH% (
    copy D:\%final%File1.txt D:\NDM\InFileQueue\%final%File1.txt
)
Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

To expand a little on my comment…

For files from both today and yesterday as a :

@Set "Src=D:\."
@Set "Dst=D:\NDM\InFileQueue"
@Set "Ext=.txt"
@RoboCopy "%Src%" "%Dst%" "*%Ext%" /MaxAge:2 > Nul

Or as a single command from either .exe or a , (prepend the line with @ to turn off its echoing if your wish):

RoboCopy "D:\." "D:\NDM\InFileQueue" "*.txt" /MaxAge:2 > Nul

For files from yesterday only, (ignoring todays), as a :

@Set "Src=D:\."
@Set "Dst=D:\NDM\InFileQueue"
@Set "Ext=.txt"
@RoboCopy "%Src%" "%Dst%" "*%Ext%" /MaxAge:2 /MinAge:1 > Nul

Or as a single command from either .exe or a , (prepend the line with @ to turn off its echoing if your wish):

RoboCopy "D:\." "D:\NDM\InFileQueue" "*.txt" /MaxAge:2 /MinAge:1 > Nul

You will note that there is one oddity, D:\., RoboCopy doesn't like the source or destination paths to end with a trailing backslash, so when one of them is the root of a drive, e.g. D:\, it is necessary to add the period, (you cannot use D:). Non root drive sources and destinations would simply use the path as I have in the example above for %Dst%.

Compo
  • 36,585
  • 5
  • 27
  • 39
0

Another way would be to use PowerShell. When you are satisfied that the files will be copied correctly, remove the -WhatIf switches.

=== get2files.ps1

$sourcedir = 'C:/the/original/directory'
$destinationdir = 'C:/the/new/directory'
$filetoday = (Join-Path -Path $sourcedir -ChildPath (Get-Date -Format yyyyMMdd)) + 'File1.txt'
$fileyesterday = (Join-Path -Path $sourcedir -ChildPath (Get-Date (Get-Date).AddDays(-1) -Format yyyyMMdd)) + 'File1.txt'
if (Test-Path -Path $filetoday) { Copy-Item -Path $filetoday -Destination 'C:/the/new/directory' -WhatIf }
if (Test-Path -Path $fileyesterday) { Copy-Item -Path $filetoday -Destination 'C:/the/new/directory' -WhatIf}

=== get2files.bat

powershell -NoLogo -NoProfile -File "%~dp0get2files.ps1"
lit
  • 14,456
  • 10
  • 65
  • 119