Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6076129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:34:21+00:00 2026-05-23T10:34:21+00:00

I have a windows cmd batch file that uses a variable, but everywhere that

  • 0

I have a windows cmd batch file that uses a variable, but everywhere that variable is used after setting it is failing.

Here is my batch script:

@echo off

if exist Transactions.csv (
    set datestr=%date:~10,4%%date:~7,2%%date:~4,2%%time:~0,2%%time:~3,2%%time:~6,2%
    rename Transactions.csv Transactions-%datestr%.csv
    curl -s -o curl.log --form upload=@Transactions-%datestr%.csv http://192.168.5.217/test_upload.php
    set datestr=
)

The file gets renamed to “Transactions-.csv”, and the curl command works OK because it’s sending the file as is, but I’d like it to properly rename the file.

If I comment out the echo off line, this is what I get back:

if exist Transactions.csv (
    set datestr=20112206112720
    rename Transactions.csv Transactions-.csv
    curl -s -o curl.log --form upload=@Transactions-.csv http://192.168.5.217/test_upload.php
    set datestr=
)

Note the missing areas where %datestr% should be.

Any ideas why these variables are not properly being parsed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:34:21+00:00Added an answer on May 23, 2026 at 10:34 am

    When you have a block of commands enclosed in parentheses, the entire block is parsed before its first command executes. That means, all the %var% expressions inside that block are replaced with their values at that time. That’s why you could see nothing in place of %datestr% when you ran the script with @echo off commented out.

    So your decision to move the body of the IF statement outside the brackets was right.

    Another solution to that would be to use delayed expansion:

    @echo off
    
    setlocal EnableDelayedExpansion
    
    if exist Transactions.csv (
        set datestr=!date:~10,4!!date:~7,2!!date:~4,2!!time:~0,2!!time:~3,2!time:~6,2!
        rename Transactions.csv Transactions-!datestr!.csv
        curl -s -o curl.log --form upload=@Transactions-!datestr!.csv http://192.168.5.217/test_upload.php
        set datestr=
    )
    

    As you can see, the delayed expansion mode is introduced with the setlocal EnableDelayedExpansion command. Note also the change of syntax: ! instead of % denote the delayed expansion of the corresponding variable/expression. When delayed expansion is enabled, you can still use % for immediate expansion, where appropriate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a batch file that I usually invoke like this: longjob.cmd >result.txt 2>&1
I have a batch file that calls a Windows GUI application. The GUI app
I have a batch file that I need to run within my NSIS installer.
I have a sh/bash script that needs to call a batch file with parameters
I have a batch file with for example cd c:\test But there is more
I have a batch file that creates a scheduled task using schtasks like this:
I am debugging a windows batch command file. It is failing when extended (>
I have a batch file (in windows XP, with command extension activated) with the
I have a windows cmd.exe script that runs a child process and needs to
I have a batch file that is using the exit command to return an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.