2017-09-04 121 views
0

我正在处理基于批处理的程序,我实际上无法绕过这个: 我想用变量测试和读取文件。这是排在我的脑海里,但它不工作:在批处理文件中使用变量读取文件

set /p target=Input: 
if exist %target% (
    more %target%.txt 
) else (
    echo File does not exist! 
) 
+2

'不work'并不能说明你的问题是什么,可以请你展开...... – Compo

+0

你不应该使用相同的文件名以'是否存在...'和'更多...'? – Stephan

回答

0

你可以这样做:

@echo off 
set /p src=Enter file name: 

IF exist %src% (
    more "%src%" 
    ) ELSE (echo File does not exists) 

结果是:

Z:\>test.bat 
Enter file name:src.txt 
aaa 
bbb 
ccc 

Z:\> 

Z:\>test.bat 
Enter file name:wrong.txt 
File does not exists 

Z:\> 

用bat文件test.bat和测试文件src.txt包含克以下文字:

aaa 
bbb 
ccc 
+0

谢谢!我真的没有想过把这些...... A.K.A“%src%” –

相关问题