简体   繁体   中英

Set value and clear value in batch while reading from file in windows

I am trying to read from file and assign the read value to variable and perform a task. However I see that I succeed in reading value correctly from file, however the assignment part doesn't work fine. Also I am struggling to clear the set value post assignment. I have looked at SO-link1 for reading and assigning value and SO-link2 for clearing the assigned value

Based on the link provided I added below line but it doesnt solve the problem

setlocal enabledelayedexpansion

To add more my key file has numbers like below

12456

23890

45389

12690

Code Snippet:

for /F "delims=" %%x in (key.txt) do 
( 
echo value read from file -- %%x            **Shows correct value**
set "SERIAL=%%x"
echo number which got assigned -- %SERIAL%  **Shows incorrect value**
set "SERIAL="                               ** Doesnt clear the set value**
)

Try with enabledelayedexpansion

@echo off
setlocal enabledelayedexpansion
for /F "delims=" %%x in (key.txt) do ( 
  echo value read from file -- %%x
  set "SERIAL=%%x"
  echo number which got assigned -- !SERIAL!
  set "SERIAL=" 
)

For more on the subject, you can perform setlocal /? from cmd.exe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM