Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file “为什么不是?”;“移动”;命令在我的批处理文件中工作?_Batch File_Cmd - Fatal编程技术网

Batch file “为什么不是?”;“移动”;命令在我的批处理文件中工作?

Batch file “为什么不是?”;“移动”;命令在我的批处理文件中工作?,batch-file,cmd,Batch File,Cmd,因此,我试图创建一个批处理文件,以移动我选择的文件。 但是“move”命令不起作用,我是这样编码的: @echo off title file mover cls goto filemover :filemover cls echo Please type what you want to put in the .txt file set /p input=Type: echo %input% >> inputfile.txt ::And here i want it to mo

因此,我试图创建一个批处理文件,以移动我选择的文件。 但是“move”命令不起作用,我是这样编码的:

@echo off
title file mover
cls
goto filemover

:filemover
cls
echo Please type what you want to put in the .txt file
set /p input=Type: 
echo %input% >> inputfile.txt
::And here i want it to move the file to the Desktop
move "inputfile.txt" Desktop
::but when i do that it just removes the txt file (inputfile.txt) and replaces it with "Desktop.File"

原来问题的原因是使用
桌面
作为命令移动的目标,该命令应引用用户的桌面目录,但在批处理文件执行时被解释为当前目录中的子目录名
桌面
,并且该目录确实存在于当前目录中,或者在当前目录中没有子目录时被解释为目标文件名。因此,这个问题描述了一个问题

真正的问题是:

如何在用户的桌面目录中创建或移动文件?

下面是一个代码,它直接在用户的桌面目录中创建文件,与用户的
桌面
shell文件夹的Windows默认值无关:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "DesktopFolder="
for /F "skip=1 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop 2^>nul') do if /I "%%I" == "Desktop" if not "%%~K" == "" if "%%J" == "REG_SZ" (set "DesktopFolder=%%~K") else if "%%J" == "REG_EXPAND_SZ" call set "DesktopFolder=%%~K"
if not defined DesktopFolder for /F "skip=1 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Desktop 2^>nul') do if /I "%%I" == "Desktop" if not "%%~K" == "" if "%%J" == "REG_SZ" (set "DesktopFolder=%%~K") else if "%%J" == "REG_EXPAND_SZ" call set "DesktopFolder=%%~K"
if not defined DesktopFolder set "DesktopFolder=\"
if "%DesktopFolder:~-1%" == "\" set "DesktopFolder=%DesktopFolder:~0,-1%"
if not defined DesktopFolder set "DesktopFolder=%UserProfile%\Desktop"

echo Please type what you want to put in the .txt file.
echo/
set UserInput=
set /P "UserInput=Type: "
setlocal EnableDelayedExpansion
echo(!UserInput!>"%Desktopfolder\inputfile.txt"
endlocal
endlocal
文件
inputfile.txt
不是首先在当前目录中创建,然后移动,因为批处理文件执行时的当前目录可以为当前用户提供写保护

请参阅有关批处理代码为什么使用这些命令行来确定用户的桌面目录,以便在Windows XP和所有较新的Windows上工作(即使在非常罕见的异常情况下)的答案

要了解所使用的命令及其工作方式,请打开一个窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • 呼叫/?
  • echo/?
  • endlocal/?
  • 获取/?
  • 如果/?
  • reg/?
  • reg query/?
  • 设置/?
  • setlocal/?
另见:

  • Microsoft文档关于
  • Microsoft文档关于
  • Wikipedia关于环境变量的文章,解释了预定义的
  • DosTips论坛主题:

桌面路径为
C:\users\username\desktop
。有两个环境变量可以帮助您以编程方式获取该路径。您可以使用:
C:\users\%username%\desktop
%userprofile%\desktop
对不起,我只是尝试了一下,但它们似乎不起作用:(如果它“不起作用”,您应该会收到一条错误消息,或者您应该能够描述发生了什么。@当我尝试使用move命令时,cmd会对我说这句话“系统找不到指定的路径。”这意味着您尝试移动的文件与脚本所在的目录不在同一目录中。请在
move
命令之前添加
echo%cd%
,以确定脚本认为它在哪里。