simple script can execute on win 10 and server 2019 but not win 11 and server 2022

herschel 1 Reputation point
2022-05-07T06:48:01.67+00:00

Want to write a script that add current date on file name and cut to another folder, but can execute on win 10 and server 2019 but not win 11 and server 2022. How can I solve it?

cd E:\FGT_temp
E:

@set name=%date:10,4%%date:4,2%%date:~7,2%

ren Richlands.conf "%date:-4,4%%date:-7,2%%date:-10,2%Richlands.conf"
ren Manurewa.conf "Manurewa_%date:-4,4%%date:-7,2%%date:-10,2%.conf"

md "E:\FGT_backup%name%"

xcopy /s "E:\FGT_temp" "E:\FGT_backup%name%"
del /s /Q E:\FGT_temp
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,586 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,125 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 31,571 Reputation points
    2022-05-07T12:47:28.883+00:00

    I don't see how that .bat file is going work correctly on any Windows version. Your date substring commands are wrong.

    Save this as a test.bat file and run on all of your machines. It will verify that they all have the date set in the same format.

    @echo Here is the date variable.  
    @echo %date%   
    @echo Here is the year.  
    @echo %date:~10,4%   
    @echo Here is the month.  
    @echo %date:~4,2%   
    @echo Here is the day.  
    @echo %date:~7,2%   
    

    It should look like this.

    C:\Temp> test.bat  
    Here is the date variable.  
    Sat 05/07/2022  
    Here is the year.  
    2022  
    Here is the month.  
    05  
    Here is the day.  
    07  
    

    Build your file names using the desired date sequence.

    If some of your machines have different date formats, then I would recommend using Powershell instead of a .bat file.

    0 comments No comments