1

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.

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

Later Added:

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

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.

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

1
  • Suggestion: when you get to the point of such complicated CMD files, it's time to switch to PowerShell, which was made for this. CMD wasn't originally designed to do anything this complicated. Such features as multi-line if statements are additions (a.k.a., "hacks") Commented Jun 5, 2014 at 19:19

4 Answers 4

2

edited to adapt to comments

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 ))"

As requested, in one line. To include it inside a batch file, replace %a with %%a

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

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)))"

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)))"
Sign up to request clarification or add additional context in comments.

20 Comments

How can this be modified so that it creates the file and then opens it, so I am not bothered with a prompt telling me the file doesn't exist and asking me if to create it. My multi line solution (well, from examples online and lots of help) does this. In it I have copy NUL "!filename.txt" and on the line below it start "" notepad "!filename!.txt". I tried adding copy NUL "!file! & " before your final start, but that doesn't seem to work.
@Rolo, answer updated. The code in your comment has some quoting problems that makes it fail.
Just wondering if there is any safe place to get rid of spaces so that it can be at 259 characters, which seems to be the limit for the Windows run box. I tried experimenting by removing spaces in a few different spots, but it then causes problems. This is 5 characters too long at 264 characters. (Just curious: I don't need to run it this way, plus I learned that I could put the code in a batch file, and then the batch file in the Windows directory and then call in from the Run box.)
@Rolo, try the added line. More code, less characters. Can be reduced more, but it is harder to read
Thanks I see that you caught your error of the double %%. I guess maybe it's a security issue on Windows 8, but when I type the code directly in Windows 8's Run box, I get an error message of Access Denied when the file is trying to be created. I don't have this problem on a Win XP computer. On the Windows 8 computer, if I copy the code in the the regular or Administrator command prompt, everything works fine. There's still one issue though, which is the file gets created without any extension. In notepad, I can't simply do Ctrl+S or File > Save, because I am saving to an extension-less file.
|
0

It is better to avoid overwriting by copy in this case (because it's ERRORLEVEL ignores overwriting status). You can do everything on your side:

  • You can check existance of file (IF EXISTS !filename!.txt),
  • If file exists, you can ask user what to do (SET /P userInput=Overwrite? (Yes/No/All)),
  • After it you can analyzed %userInput% to decide what to do (delete existent file and create empty one with the same name + open editor or ask file name again).

2 Comments

Thanks for pointing me in the right direction. Unfortunately I do not know how to code in DOS. I have in other languages, but nonetheless, that was a long time ago. The following is what I am working on, but the filename doesn't get set or it remembers a previous filename. Also, I took echo off out so that I can test printing messages: cmd /c SET /P filename=What File name? & cmd /v /c echo !filename! & IF EXIST !filename!.txt SET /P overwrite=Overwrite File? & IF !overwrite!==N echo No ask new file name ELSE echo unique
I recommend you to add flag /I after IF for case insensitive string comparison.
0

If %overwrite%==Y : if %overwrite% is empty, this is executed as If ==Y - obviously a syntax error.

With the singlequoutes you mentioned: If '%overwrite%'==Y is executed as If ''==Y - this is proper syntax, so your code doesn' fail (but is not running as intended)

The reason why %overwrite% is empty: you are using it inside a block (between if ( and the corresponding ), so for parsing reason it' easy (search for delayed expansion). You can easily avoid that:

@echo off
SET /P filename=What File name?
echo %filename%
:loop
if not exist %filename%.txt goto finish
echo File Exists
SET /P overwrite=Overwrite File?
echo overwrite is %overwrite%
If "%overwrite%"=="Y" ( echo "Yes, Y" ) else ( goto loop )
this line is never reached    
:finish   
echo finish

1 Comment

Thanks for your input. I had already mentioned the solution to the overwrite variable problem and delayed expansion by providing a link in the original question above.
0

When using a single line you can't loop back to the start - but this will only create the file AND start notepad with the file, if the file does not exist.

If the file exists it will just exit and NOT start notepad so you know that you have to try again.

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

9 Comments

hmm. Well, this does work and since I will be able to easily call up these commands again, I guess I wouldn't mind doing so. No message is displayed to me that the cmd box is exiting because the file already exists, but since this is just for me, I suppose that's fine and that I will remember the code being setup this way. This is interesting. I never thought of looking at it logically from this way of checking if the file does not exist. Question: What are the two quotation marks after start for? Also, so for statements couldn't be used in a single line code? What about multiple if
statements? Such as (If file exists, prompt user if to overwrite, if answer is yes, overwrite, else if no, ask for new name, then create file and open it) else create file and open it. So this would be a nested (I think this is the correct term) if statement (an if statement within an if statement and would only give the user one chance to come up with a new name. Could that work? If yes, how would I group the if statements--with quotation marks like you did? Thanks.
If that was implemented it would only prompt once, and if the second filename existed you'd be at the same spot. The "" after the start command stops it failing if you ever quote the filename, as the first set of quotes become the Window Title.
You know, you can use a regular batch file and place it on the path, such as c:\windows and then just paste in the batch filename into the RUN box. If you call it a.bat then you just have to type a into the run box, and you will have looping available to do what you need.
Yes, I know what I suggested would only work one more time. I did not know one can store a bat file in Windows and that it would work in the RUN box. That's cool Thanks. With the toolbar explorer program I am using, I could also store the bat file anywhere and then tell it where to look. I thought what I wanted to do was simple--create a text file quickly--until I realized I would prefer overwrite protection and a chance to specify another file name in the same session.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.