【问题标题】:How to assign a file content to the variable? [duplicate]如何将文件内容分配给变量? [复制]
【发布时间】:2013-03-11 09:13:14
【问题描述】:
@echo off
SET str1=type %1
SET str2=type %2
echo File1: %str1%
echo 
echo File2: %str2%

你可能知道我想要什么,但命令行解释器不知道。怎么做才合适?

PS。我指的是整个内容,而不仅仅是第一行!

【问题讨论】:

  • 你的意思是从文件中读取变量吗?
  • 您可以将内容逐行放入一组变量中。

标签: windows if-statement for-loop batch-file cmd


【解决方案1】:

对于内容(所有行):

@echo off &setlocal enabledelayedexpansion
set "files=1"
if "%~1"=="" set "files="
if "%~2"=="" set "files="
if not defined files echo Missing filename(s^).&goto:eof
:: read the first file
set /a cnt1=0
for /f "usebackqdelims=" %%i in ("%~1") do set /a cnt1+=1 &set "str1!cnt1!=%%i"
:: read the second file
set /a cnt2=0
for /f "usebackqdelims=" %%i in ("%~2") do set /a cnt2+=1 &set "str2!cnt2!=%%i"
:: write the first file
echo File1:
for /l %%i in (1,1,%cnt1%) do echo !str1%%i!
:: write the second file
echo File2:
for /l %%i in (1,1,%cnt2%) do echo !str2%%i!
endlocal

【讨论】:

  • 我对“内容”进行了编辑。
  • %1%~1有什么区别?
  • ~ 参数确实会删除周围的双引号。
猜你喜欢
  • 1970-01-01
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
相关资源
最近更新 更多