Date: /current year/month/day-1 under DOS

richy -  
brucine Posted messages 25285 Registration date   Status Member Last intervention   -

Hello,

Under Linux, I have the variable "REPO" which defines the path of the files by date J-1 (directory and subdirectory).

REPO="/var/lib/docker/volumes/config_frontoffice_storage_exports/_data/"`date -d "yesterday" +"%Y"`"/"`date -d "yesterday" +"%m"`"/"`date -d "yesterday" +"%d"`

is equivalent to:

REPO="/var/lib/docker/volumes/config_frontoffice_storage_exports/_data/2023/10/16

I want to reproduce the same variable "REPO" in a Dos script (batsh windows).

Thank you in advance.

3 answers

  1. brucine Posted messages 25285 Registration date   Status Member Last intervention   4 190
     

    Hello,

    You specify, bash is like Hebrew to me.

    The question is about performing some processing (what kind, list, copy somewhere...?) on all the files in the directory structure that would be named toto-aaaa-mm-jj (with what date syntax?) or to extract all those that are named as we want but that were created or modified on the specified date?

    0
    1. richy
       

      Hello,

      Thank you for your interest in my request; here is my DOS script that is not working yet:

      @echo off
      setlocal enabledelayedexpansion

      rem Calculate the date one day ahead
      for /f %%A in ('powershell -Command "(Get-Date (Get-Date).AddDays(1)).ToString('yyyyMMdd')"') do (
          set DATE=%%A
      )

      rem Create the FTP script file
      echo open ftp.example.com>ftp_script.txt
      echo USER>>ftp_script.txt
      echo PASSWORD>>ftp_script.txt
      echo cd /var/lib/docker/volumes/config_frontoffice_storage_exports/_data/%DATE%>>ftp_script.txt
      prompt>>ftp_script.txt
      lcd C:\Dispatsh\DF108>>ftp_script.txt
      echo mget 00108*>>ftp_script.txt
      prompt>>ftp_script.txt
      echo bye>>ftp_script.txt

      rem Run FTP with the script
      rem ftp -s:ftp_script.txt
      ftp -s:C:\scripts\ftp_script.txt -v 192.168.1.13

      rem Delete the FTP script file
      del ftp_script.txt
       

      The need is to retrieve files "00108*" from the path:  /var/lib/docker/volumes/config_frontoffice_storage_exports/_data/%DATE%   (via ftp)

      I need to retrieve every day the "00108" files from the previous day, which is populated at the end of each day; for example, today (20231018) my script should connect via ftp to the target machine at the address:  192.168.1.13 at the path: (  /var/lib/docker/volumes/config_frontoffice_storage_exports/_data/2023/10/17)

      to retrieve the "00108*" files.

      Under Linux the variable:

      REPO="/var/lib/docker/volumes/config_frontoffice_storage_exports/_data/"`date -d "yesterday" +"%Y"`"/"`date -d "yesterday" +"%m"`"/"`date -d "yesterday" +"%d"`

      which represents the target path works well.

      The issue is converting this Linux command of the target path into a DOS script.

      Thank you in advance.

      0
      1. brucine Posted messages 25285 Registration date   Status Member Last intervention   4 190 > richy
         

        I trust you with your FTP syntax; I no longer have a server to check.

        The command setlocal enabledelayedexpansion doesn't hurt but is useless: it only serves to bring back multiple successive values inside a FOR loop (which is not the case), each of these values instead of just the last one, and it only works for variables specified by !var! instead of %var% in any case.

        If you are worried that such a command won't be interpreted correctly, replace it with:

        setlocal EnableExtensions

        You should not use DATE, which is a system variable, but define your custom variable, for example _DATE; I'm sure that one doesn't already exist.

        Unless I'm mistaken, you calculate tomorrow's date AddDays(1) instead of yesterday's date AddDays(-1), and your date format yyyyMMdd is not consistent with that of the target folders, which I understand to be yyyy/MM/dd.

        For this, I use (I've modified the format according to yours), a variable that I call HIER (everyone chooses), where I retrieved the date not into a Batch variable %_DATE% but into a PowerShell variable $date

        FOR /F "usebackq" %%i in (`PowerShell $date ^= Get-Date^; $date ^= $date.AddDays^(-1^)^; $date.ToString^('yyyy/MM/dd'^)`) do set HIER=%%i

        Put a REM in front of @echo off and successively pause after each command or sequence of commands to look for any potential errors.

        Start, in my case, by putting after the FOR loop a

        echo %HIER% and even, let's be tricky, an echo x%HIER%x: some commands can insert an unexpected space that could cause issues without me seeing it, and that would thus be localized.

        0
  2. richy
     

    Hello again, thank you for your response:

    The execution in DOS (cmd) of the command:

    FOR /F "usebackq" %%i in (`PowerShell $date ^= Get-Date^; $date ^= $date.AddDays^(-1^)^; $date.ToString^('yyyy/MM/dd'^)`) do set HIER=%%i

    returns the following result:

    %%i was unexpected at this time.

    Best regards.

    0
    1. brucine Posted messages 25285 Registration date   Status Member Last intervention   4 190
       

      In a Batch, a variable has two % signs, while in the command line (but why?) there is only one:

      FOR /F "usebackq" %i in (`PowerShell $date ^= Get-Date^; $date ^= $date.AddDays^(-1^)^; $date.ToString^('yyyy/MM/dd'^)`) do set HIER=%i & echo %i
      0
  3. richy
     

    Hello again,

    It's working, it's very kind of you, thank you!

    Have a nice day.

    0
    1. brucine Posted messages 25285 Registration date   Status Member Last intervention   4 190
       

      You're welcome if everything works as expected.

      Have a good evening.

      0