我需要创建一个接收3个参数的批处理文件(让我们称之为p1
,p2
和p3
)并将它们逐行写入用户桌面的新文件中文件夹(最后一部分很重要)
预期用途:
D:\Temp\WriteFile -p1=aaa -p2=bbb -p3=ccc
C:\User\Desktop\output.txt
中的输出(或用户桌面文件夹可能位于何处):
aaa
bbb
ccc
我更关心的是找到当前用户的桌面文件夹并在那里写文件。
答案 0 :(得分:2)
首先,您尝试错误地传递参数。批处理文件的Windows标准不是命名参数,而是直接在命令行上传递它们(例如下面的例子)。
其次,用户的桌面文件夹位于桌面子文件夹中的%USERPROFILE%
中(再次,如下例所示)。
这会将您想要的内容写入当前用户的Desktop文件夹,名为testdesktop.txt
的文件中。第一行创建文件并写入作为第一行传递的第一个参数。其他行将连续参数附加到同一文件。输出文件名周围的双引号处理具有空格的用户名(例如“John Smith”),其中桌面文件夹为C:\Users\John Smith\Desktop
。
将此内容保存到批处理文件中 - 出于演示目的,我们称之为MakeDesktopFile.bat
及其创建的文件testdesktop.txt
。
@echo off
echo %1 > "%userprofile%\Desktop\testdesktop.txt"
echo %2 >> "%userprofile%\Desktop\testdesktop.txt"
echo %3 >> "%userprofile%\Desktop\testdesktop.txt"
这样称呼:
MakeDesktopFile aa bb cc
输出:
C:\>type %userprofile%\Desktop\testdesktop.txt
aa
bb
cc
C:\>
答案 1 :(得分:2)
获取桌面位置的最安全方法是从注册表中获取:
@Echo Off
If "%~1"=="" GoTo Next
Set "SF=Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
For /F "EOL=H Tokens=2*" %%A In ('Reg Query "HKCU\%SF%" /V Desktop') Do (
For %%C In (%*) Do Echo %%~C)>"%%~B\output.txt"
:Next
您可以运行此脚本WriteFile.cmd
,如下所示:
D:\Temp\WriteFile aaa bbb ccc
如果任何参数包含空格或有问题的字符,例如 , ; =
,您应该用双引号将它们包围起来:
D:\Temp\WriteFile "aa a" "b,bb" "cc;c" "d=dd"
答案 2 :(得分:1)
@echo off
setlocal
for %%A in (%*) do call :args %%A
echo p1=%p1% p2=%p2% p3=%p3%
call :shellfolder desktop
if errorlevel 1 exit /b 1
(
echo Desktop: "%desktop%"
echo p1: %p1%
echo p2: %p2%
echo p3: %p3%
) > "%desktop%\output.txt"
exit /b
:args
set arg=%~1
if "%arg:~,1%" == "-" set "prefix=%arg:~1%" & exit /b
set %prefix%=%arg%
exit /b
:shellfolder
for /f "tokens=1,2*" %%A in (
'reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "%~1"'
) do if /i "%%~A" == "%~1" set "%~1=%%~C"
exit /b
用户桌面是 Shell文件夹,因此它可能不在用户个人资料中 夹。获取位置需要一些注册表阅读。
上述脚本的说明:
脚本参数与每个参数一起传递给1st for循环
由=
字符以及通常的空格字符分隔。
被调用的:args
标签指定传递给arg
的参数。
如果第一个字符为-
,则设置为prefix
,否则设置为%prefix%=%arg%
它会设置%prefix%
,其中p1
是参数
前缀名称,即:shellfolder
。
被调用的reg query
标签接受Shell文件夹的值
作为一个论点。
for
循环中的errorlevel
获取Shell文件夹的数据值
并将参数名称设置为数据值。
未使用ie exit /b 1
作为错误级别设置显式reg query
来自errorlevel
可能足够合适。检查:shellfolder
已完成
output.txt
致电后确保继续安全。
如果成功,则会将//Method on the client side (React.JS Component)
callRatesConvert(fromCurrency, toCurrency, amount) {
const call = this.props.call;
let resCall = 0; // Outer variable
let settings = {};
settings.fromCurrency = fromCurrency;
settings.toCurrency = toCurrency;
settings.amount = amount;
settings.accuracy = 10;
//Calls Backend method API that returns res succesfully
call('rates.convert', settings, (err, res) => {
if (err) {
//Show Error UI to user
} else if (res) { //res value is fetched from backend method properly
console.log('result from call', res) //this wont be undefined.
resCall = res; //this is assigned but it takes some time because it is still fetching
}
});
console.log('resCall', resCall); //this will print undefined because it is outside the call method, and it is not assigned yet.
return resCall; // this obviously will be undefined.
}
写入用户桌面。
答案 3 :(得分:0)
可能最简单的方法是使用for循环。它不依赖于参数的数量,它可以是3或5等。
@echo off
for %%i in (%*) do (
echo %%i >> "%USERPROFILE%\Desktop\output.txt"
)
如果你想在每次运行时重写输出,那么只需清空文件。
@echo off
type > nul "%USERPROFILE%\Desktop\output.txt"
for %%i in (%*) do (
echo %%i >> "%USERPROFILE%\Desktop\output.txt"
)