1

I am trying to create a text file via DOS commands. The commands asks one for the name of the file prior to creating it. I have looked here and here and elsewhere to get me started.

I would like the code to work in one line. So, I should be able to type the entire code in Windows Start > Run box.

This is what I have:

cmd /k @ECHO OFF & SET /P filename=What File name: & copy NUL %filename%.txt & :End

This however ignores the name of the file I gave when asked, and creates %filename%.txt.

I have tried changing the operator before the word copy to |, &&, and & but these don't even ask me for a file name and simply create %filename%.txt

Also, the cmd box stays open after the text file is created.

P.S. I know I can use /q before /k for echo off.

I look forward to your help.

Community
  • 1
  • 1
Rolo
  • 220
  • 2
  • 13

1 Answers1

1

This works here: delayed expansion is used in another cmd process

cmd /c @ECHO OFF & SET /P filename=What File name: & cmd /v /c copy NUL !filename!.txt & exit 

The command line has no path defined for the directory and when using the RUN box it will probably be created in the c:\windows\system32 folder, except you will not have write access to that folder.

foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • Thank you. Do you know what I should change so that the cmd box closes after I type in the file name and the file is created? Also, can you explain just a bit why the code you provided works and the one I had above doesn't--would like to better understand and learn what's happening. – Rolo Jun 02 '14 at 15:41
  • Try changing `:End` to `CLS` and see if it closes the window. You can't create or change and also use an environment variable in a single line, unless you use delayed expansion. That's what the `/v` does and the `!` around the variable name – foxidrive Jun 02 '14 at 15:46
  • The above code only works after I open up cmd. If I go to Start > Run, and paste the code there, though I am asked for the file name, no file gets created. The code also doesn't work in the explorer toolbar program that I have. Also, CLS didn't work. – Rolo Jun 02 '14 at 16:00
  • I got it--by trial and error. The first and last parenthesis need to be removed so that the code works when pasted into the run box. It also works with no parenthesis at all--meaning with the other two removed as well. It also worked when I replaced the ! (exclamation marks) with % (percentages), for which I don't know why. Also, the /c that your code has causes the cmd box to close after creating the file. When I first tested it, I was opening the cmd box first, which was also why I didn't realize that the code you gave didn't work when pasted in the Run box. – Rolo Jun 02 '14 at 17:13
  • I would love to choose your answer as the accepted solution if you can please mention the parenthesis issue or just remove it. Once again, thanks. I am very grateful. – Rolo Jun 02 '14 at 17:15
  • Your first comment asks that the cmd box be closed so that's what the code does now. The fact that `%` worked for you indicates that your test was flawed - that's the reason you posted your question in the first place. See my edited answer above. – foxidrive Jun 03 '14 at 00:48
  • Thanks for the update. Regarding the cmd box closing, I told you it wasn't with your first answer, but then realized the reason was because I was running the code after opening the cmd box. If I entered the code without parenthesis in the RUN box, the cmd box does close, and that's because of the /c. So, the closing portion was already correct. However, with the Exit command instead of :End, the cmd box also closes if I open it first & then paste the code, so Exit is better. Regarding the %, I say it works with the code you gave. Regarding saved file location, when executed from the – Rolo Jun 03 '14 at 05:29
  • RUN box on my Windows XP computer, it saves in C:\Documents and Settings\my computer's user account name. Once again, thank you. – Rolo Jun 03 '14 at 05:30
  • Hello. Maybe you can give me a helping hand at my other question: [link](http://stackoverflow.com/questions/24034608/how-to-check-the-result-of-an-overwrite-file-request-in-dos-cmd) Thanks. – Rolo Jun 04 '14 at 10:10