Batch programming; Script writes to a file which writes to a file


  1. Posts : 52
    Windows 10
       #1

    Batch programming; Script writes to a file which writes to a file


    I have a script I run when reinstalling Windows. It creates a script called login.cmd which is executed via task manager when the user logs in.

    Since the primary script writes the login.cmd file based on the users preferences and custom installation it is different for each user. That part is fine.

    What I'm trying to accomplish is the have the login.cmd script write log file entries so I can debug the login.cmd and track its execution.

    This will write a command to login.cmd

    Code:
    Echo IF EXIST C:\ABC\XYZ\*.tmp DEL C:\ABC\XYZ\*.tmp /f/s >>C:\TOOL\LOGIN.CMD
    .
    But what I want to have in the login.cmd is
    Code:
    IF EXIST C:\ABC\XYZ\*.tmp DEL C:\ABC\XYZ\*.tmp /f/s >>%LOCALAPPDATA%\Temp\Login.log 2>&1
    I can't write this in the initial script;
    Code:
    Echo IF EXIST C:\ABC\XYZ\*.tmp DEL C:\ABC\XYZ\*.tmp /f/s >>%LOCALAPPDATA%\Temp\Login.log 2>&1 >>C:\TOOL\LOGIN.CMD
    as it crashes the script.

    How to I get the log file redirect into the login.cmd using the echo statement in the primary script?
      My Computer


  2. Posts : 8,111
    windows 10
       #2

    Very simple way now to program just put it into chatgpt or similar and it will do it for you
      My Computer


  3. Posts : 16,958
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #3

    You'll need to learn about escape characters before you can do a sophisticated job like this.
    You'll need to insert escape characters before the relevant characters in your commands.
    Escape Characters - RobvdW
    Quotes, Escape Characters, Delimiters - SS64
    Caret symbol as a continuation character - TenForums

    It is up to you but I think your current job is far too complex to be used as a tool for learning about using escape characters. I think you'll need to learn using much simpler scripts.


    ChatGPT is not a learning tool.
    It can produce the correct code.
    It can produce rubbish.
    It does not know the difference.



    Denis
      My Computer


  4. Posts : 782
    Windows 7
       #4

    While you can learn the proper escape sequences for CMD, it's a time waster for overly complicated lines like yours.

    The simple solution is two-fold:
    1. Wrap a complicated expression inside a variable, which (mostly) treats it as a literal line. The exception is you need to escape existing "%var%" expressions with "%%var%%".

    2. Use delayed expansion, so CMD isn't doing variable expansion on the fly.

    Code:
    @echo off
    setlocal enabledelayedexpansion
    
    REM Escape %LOCALAPPDATA% with %%LOCALAPPDATA%%
    
    set "line=IF EXIST C:\ABC\XYZ\*.tmp DEL C:\ABC\XYZ\*.tmp /f/s >>%%LOCALAPPDATA%%\Temp\Login.log 2>&1"
    echo !line! >> login.cmd
    login.cmd
    Code:
    IF EXIST C:\ABC\XYZ\*.tmp DEL C:\ABC\XYZ\*.tmp /f/s >>%LOCALAPPDATA%\Temp\Login.log 2>&1
      My Computer


  5. Posts : 52
    Windows 10
    Thread Starter
       #5

    Try3 said:
    You'll need to learn about escape characters
    Well that was embarrassingly simple.

    Code:
    Echo IF EXIST C:\ABC\XYZ\*.tmp DEL C:\ABC\XYZ\*.tmp /f/s ^>^>%LOCALAPPDATA%\Temp\Login.log 2^>^&1 >>C:\TOOLS\LOGIN.CMD
    thanks for your help.

    Sorry @garlin I saw your response after @Try3 response.

    "Overly complicated"? Lol. I have much more complicated code than that in the script.

    Code:
    DISM.exe /Online /Add-ProvisionedAppxPackage /PackagePath:Microsoft.HEVCVideoExtension_2.0.61931.0_neutral__8wekyb3d8bbwe.appxbundle /DependencyPackagePath:Microsoft.VCLibs.140.00_14.0.32530.0_x64__8wekyb3d8bbwe.Appx /DependencyPackagePath:Microsoft.VCLibs.140.00_14.0.32530.0_x86__8wekyb3d8bbwe.Appx /skiplicense >>%LogFile% 2>&1
      My Computer


  6. Posts : 782
    Windows 7
       #6

    As I used to ask PaulBlack, who was fond of asking how to escape long complicated expressions, which is more readable (for debugging)? Something that resembles the original command, or something with a bajillion escape characters?
      My Computer


  7. Posts : 52
    Windows 10
    Thread Starter
       #7

    garlin said:
    As I used to ask PaulBlack, who was fond of asking how to escape long complicated expressions, which is more readable (for debugging)? Something that resembles the original command, or something with a bajillion escape characters?
    Indeed. I've often been challenged to the point of tearing my hair out trying to find unbalanced ", % or ( ) in the script.
      My Computer


  8. Posts : 16,958
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #8

    jshs2020 said:
    Well that was embarrassingly simple.
    I'm glad you've worked it out.

    You might like to note this link for reference.
    my ditty - Batch file and PowerShell guides [post #16] - TenForums
    and take a look at the lines in that article beginning my ditty and demo for:


    All the best,
    And thanks for the rep,
    Denis
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 05:51.
Find Us




Windows 10 Forums