【问题标题】:Setting directories to null values for a batch file将批处理文件的目录设置为空值
【发布时间】:2018-05-04 00:13:00
【问题描述】:

我无法让这个批处理脚本按照我想要的方式运行。我想要它而不是手动输入每个参数,这样我就可以在 CMD 中按回车键,它会在代码的后半部分自动填充相应的 IF。

::Setting of Variables
@Set /P RScript=Set path to R:_
@Set /P RProgram=Set path to RScript:_   
@Set /P RStartDir=Set Start Directory:_   
@Set /P BeginSims=Begin on which Loan?:_       
@Set /P EndSims=End on which Loan?:_         
@Set /P OutputDir=Set Output Directory:_   
@Set /P Deal=Set Deal input file (.txt):_         
@Set /P OutputFile=Name Deal Output File:_  
@Set /P AsOfDate=As of Date?:_  
@Set /P ThirtyYrSpread=Thirty Year Mortgage Spread?:_

::Inputs for Variables
::  "c:\program files\r\r-3.4.3\bin\x64\rscript.exe"      RScript
::  "c:\cirt 2014 - 1\model.r"                            RProgram
::  "c:\cirt 2014 - 1"                                    RStartDir
::  1                                                     BeginSims
::  5                                                     EndSims
::  "C:\BatchAll"                                         OutputDir
::  "2014-1 Loan.txt"                                     Deal
::  "2014-1"                                              OutputFile
::  "62017"                                               AsOfDate
::  "135"                                                 ThirtyYrSpread
::Command Prompt, /c Carries out command specified by string and then terminates, processing the R script and outputting a CSV file.
cmd /c ""%RScript%" "%RProgram%" "%RStartDir%" "%Begin%" "%End%" "%OutputDir%" "%Deal%" "%OutputFile%" "%AsOfDate%" "%ThirtyYrSpread%""

::if [%RScript%]==[] Set RScript=c:\program files\r\r-3.4.3\bin\x64\rscript.exe
::if [%RProgram%]==[] Set RProgram=C:\Cirt 2014 - 1\0.Mortgage Model.R
::if [%RStartDir%]==[] Set RStartDir=C:\Cirt 2014 - 1
::if [%Begin%]==[] Set Begin=1
::if [%End%]==[] Set End=5
::if [%OutputDir%]==[] Set OutputDir=C:\Cirt 2014 - 1
::if [%Deal%]==[] Set Deal=2014-1 Loan.txt
::if [%OutputFile%]==[] Set OutputFile=2014-1
::if [%AsOfDate%]==[] Set AsOfDate=62017
::if [%ThirtyYrSpread%] == [] Set ThirtyYrSpread=135`

目前它在第一个变量 RScript 上引发错误。详细的,

files\r\r-3.4.3\bin\x64\rscript.exe]==[] was unexpected at this time.
C:\Users\msamuels\Desktop>if [c:\program files\r\r-3.4.3\bin\x64\rscript.exe]==[] Set RScript=c:\program files\r\r-3.4.3\bin\x64\rscript.exe

我一直在玩弄它一段时间,想知道是否有人有任何建议?所有必要的文件都在目录中,因为当我在没有 IF 的情况下运行它并手动输入变量时,一切正常。

【问题讨论】:

  • 您必须“双引号”包含空格的变量,例如错误中的路径而不是 [括号]
  • 那么,如果 ""RScript""==[] 而不是 [%RScript%]==[]?
  • 不,像这样if "%RScript%"=="" Set "RScript=c:\program files\r\r-3.4.3\bin\x64\rscript.exe"
  • 啊,非常感谢。现在就试试吧!
  • 有效!!非常感谢。

标签: r batch-file directory parameter-passing


【解决方案1】:

您需要做的就是在要求输入之前设置所有变量。如果您在输入提示符处按回车,则变量保持不变。

@echo off
:: defaults
Set "RScript=c:\program files\r\r-3.4.3\bin\x64\rscript.exe"
Set "RProgram=C:\Mortgage\Cirt 2014 - 1\0.Mortgage Model.R"
Set "RStartDir=C:\Mortgage\Cirt 2014 - 1"
Set "Begin=1"
Set "End=5"
Set "OutputDir=C:\Mortgage\Cirt 2014 - 1"
Set "Deal=Cirt 2014-1 Loan Level.txt"
Set "OutputFile=Cirt 2014-1d"
Set "AsOfDate=62017"
Set "ThirtyYrSpread=135"

::Setting of Variables
ECHO PRESS ENTER AT ANY INPUT TO ACCEPT the DEFAULT VALUE.
Set /P "RScript=Set path to R:_"
Set /P "RProgram=Set path to RScript:_"
Set /P "RStartDir=Set Start Directory:_"
Set /P "BeginSims=Begin on which Loan?:_"
Set /P "EndSims=End on which Loan?:_"
Set /P "OutputDir=Set Output Directory:_"
Set /P "Deal=Set Deal input file (.txt):_"
Set /P "OutputFile=Name Deal Output File:_"
Set /P "AsOfDate=As of Date?:_"
Set /P "ThirtyYrSpread=Thirty Year Mortgage Spread?:_"


:: Command Prompt, /c Carries out command specified by string and then terminates, processing the R script and outputting a CSV file.
echo cmd /c "%RScript%" "%RProgram%" "%RStartDir%" "%Begin%" "%End%" "%OutputDir%" "%Deal%" "%OutputFile%" "%AsOfDate%" "%ThirtyYrSpread%"
pause

这是在每个用户提示符下按回车的输出。

PRESS ENTER AT ANY INPUT TO ACCEPT the DEFAULT VALUE.
Set path to R:_
Set path to RScript:_
Set Start Directory:_
Begin on which Loan?:_
End on which Loan?:_
Set Output Directory:_
Set Deal input file (.txt):_
Name Deal Output File:_
As of Date?:_
Thirty Year Mortgage Spread?:_
cmd /c "c:\program files\r\r-3.4.3\bin\x64\rscript.exe" "C:\Mortgage\Cirt 2014 - 1\0.Mortgage Model.R" "C:\Mortgage\Cirt 2014 - 1" "1" "5" "C:\Mortgage\Cirt 2014 - 1" "Cirt 2014-1 Loan Level.txt" "Cirt 2014-1d" "62017" "135"
Press any key to continue . . .

我没有安装 R,所以我只是在最后回显命令。删除 CMD 之前的 ECHO 以执行您的 R 脚本。

【讨论】:

  • 另一种看待它的好方法,感谢您的回复。我明天试试这个。
  • 有效!只需要在 cmd /c " ""......" 之后在整个语句周围添加更多的 " "
  • @matrixfox,Rscript 一定很奇怪。通常,您需要这样做的唯一时间是运行CMD.exe 的串联命令。您只是在运行一个带有命令行参数的命令。
  • @matrixfox,实际上您甚至不需要使用CMD /C 来运行Rscript.exe
  • 我明白你在说什么。我会多玩一点,让它更“裸露的骨头”。感谢您的帮助。
猜你喜欢
  • 2013-11-21
  • 1970-01-01
  • 2017-10-29
  • 2023-03-17
  • 1970-01-01
  • 2023-03-12
  • 2010-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多