如何检查在Windows CMD重写文件请求的结果? [英] How to check the result of an overwrite file request in Windows CMD?

查看:303
本文介绍了如何检查在Windows CMD重写文件请求的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上搜索了这一点,但似乎无法找到答案。我有code,它要求1,他/她喜欢什么名字给文本文件后,创建一个文本文件。然后,文件被创建后打开。但是,如果让我选择N(否)覆写请求,我不希​​望要打开的文件。我反而想再次问说N(否)的覆盖请求后指定另一个文件名。

I have searched online for this, but can't seem to find an answer. I have code that creates a text file after asking one what name he/she would like to give the text file. The file is then opened after being created. However, if I choose N (No) to an overwrite request, I don't want the file to open. I would instead want to be asked again to specify another file name after saying N (No) to the overwrite request.

然而,我不知道是如何检查给予覆写请求的答案。

I however have no idea as to how to check the answer given to the overwrite request.

另外,我需要整个code是在一行中。

Also, I would need the entire code to be in one line.

这是我没有,我上面提到的演员:

This is what I have without the extras that I am mentioned above:

cmd /c @ECHO OFF & SET /P filename=What File name: & cmd /v /c copy /-y NUL !filename!.txt & cmd start /v /c start notepad !filename!.txt & Exit

后来补充:

我工作的以下事项,并不断收到错误消息(在这个时候意外的。文件名是呼应了。如果我通过注释的部分出来,这样它的作品编辑文件,然后继续进行编辑。什么我现在有,它仍然有效但是,如果我退出CMD框,然后再启动它,那么它不工作,我得到一个错误信息这是code迄今:

I am working on the following and keep getting the error message "( was unexpected at this time." after the filename is echoed. If I edit the file by commenting parts out so that it works, and then continue to edit it to what I currently have, it still works. However, if I exit the cmd box, and then start it again, then it doesn't work and I get an error message. This is the code thus far:

@echo off
SET /P filename=What File name?
echo %filename%
::loop
if exist !filename!.txt (
echo File Exists
SET /P overwrite=Overwrite File?
echo overwrite is %overwrite%
If %overwrite%==Y (
echo "Yes, Y"
)
)

echo finish

如果我把周围的%覆盖%单引号如果'%覆盖%'==是我没有再获得意外的错误消息。我仍然面临不过问题是退出会话时/ cmd的箱子,我不能得到上面的线呼应覆盖的价值。它只是说,覆盖是(不带引号和后无值)。如果我在运行批处理文件的同一会议上继续一遍又一遍,我得到覆盖的值。

If I put single quotes around %overwrite% in If '%overwrite%'==Y I no longer get the unexpected error message. The problem I still face though is when exiting the session / the cmd box, I can't get the line above to echo the value of overwrite. It just says "overwrite is" (without quotes and with no value after it). If I continue in the same session running the batch file over again, I get a value for overwrite.

解决方案无法呼应%覆盖%从if语句都可以找到(用户输入)<值href=\"http://stackoverflow.com/questions/13692916/unable-to-echo-user-input-values-to-file-in-batch-script\">here.

Solution to unable to echo value of %overwrite% (user input) from if statement can be found here.

推荐答案

编辑,以适应评论

cmd /v:on /q /c "for /l %a in (0 0 1) do (set /p "file=file? " & if exist "!file!" ( set "q=" & set /p "q=overwrite? " & if /i "!q!"=="y" ( type nul > "!file!" & start "" notepad "!file!" & exit ) ) else ( type nul > "!file!" & start "" notepad "!file!" & exit ))"

按照要求,在一行中。包括它的批处理文件中,替换%A %% A

编辑一行,具有输入验证和文件操作添加错误检查,并缩短了(变量长度缩短,不需要去掉空格,文件创建code重复数据删除,...),以适应进入Windows运行对话框

edited one line, with input validations and error checks added on file operations, and shortened (variables length reduced, unneeded spaces removed, file creation code deduplicated, ...) to fit into windows "Run" dialog

编辑添加初始 CD / D%USERPROFILE%来确保工作文件夹可写。为什么?因为从Windows 7中,包括 / V:从运行Windows调用 CMD (或关闭)对话框(OP的原因为单行),该命令的活动目录是 C:\\ WINDOWS \\ SYSTEM32 (或任何系统)。如果没有 / V 开关,活动目录是用户配置文件夹。

edited added initial cd /d "%userprofile%" to ensure the working folder is writeable. Why? Because from windows 7, including the /v:on (or off) in the call to cmd from the windows Run dialog (the OP reason for a one liner), the active directory for the command is c:\windows\system32 (or wherever the system is). Without the /v switch, the active directory is the user profile folder.

cmd /v:on /q /c "cd/d "%userprofile%"&for /l %a in () do ((set/p"f=file? "||set "f=")&(if defined f if exist "!f!" ((set/p"q=overwrite? "||set "q=0")&if /i not "!q!"=="y" (set "f=")))&if defined f (type nul>"!f!" &&(start "" notepad "!f!" &exit)))"

编辑大量的测试中,我让它在XP失败后。如果机器配置了禁用命令扩展的previous code将失败。此外,随着按Ctrl-C 可以是烦人的问题。这应该处理的第一个问题,并尽量减少第二。

edited After a lot of tests i make it fail in XP. The previous code will fail if the machine is configured with command extensions disabled. Also, the problem with Ctrl-C can be anoying. This should handle the first problem and minimize the second.

cmd /v:on /e:on /q /c "cd/d "%userprofile%"&for /l %a in ()do ((set/p"f=file? "||(set f=&cd.))&(if defined f if exist "!f!" ((set/p"q=overwrite? "||(set q=&cd.))&if /i not "!q!"=="y" (set f=)))&if defined f (cd.>"!f!"&&(start "" notepad "!f!" &exit)))"

这篇关于如何检查在Windows CMD重写文件请求的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆