1

I'm working with the command prompt for nearly the first time and am required to create a batch file that prompts the user for a filename with an extension. The bat is supposed to be able to copy and rename said file. The instructor has directed us to use environmental variables to accomplish this task, but I keep getting directory or syntax errors.

I've tried using the variable that the user sets with a previous prompt, but unfortunately this particular instructor hasn't given us practical examples about how to accomplish this particular goal, so I'm flailing. I've tried attaching the variable to the target directory with a generic name for the file. The file and the copy should be in the dame directory.

set /P file_var=Please enter a file name and extension: 
copy %file_var% Templatecopy.doc

The file should be copied with the new default name of "Templatecopy.doc" in the target directory.

Churns out syntax and directory errors.

Compo
  • 36,585
  • 5
  • 27
  • 39
tiram001
  • 11
  • 1
  • 3
    I guess, the filename contains spaces (else your code would work - it's syntactically correct) Whenever there are spaces in filenames or paths, you have to quote them to mark them as one entity (better get used to always quote them): `copy "%file_var%" "Template copy.doc" – Stephan Sep 23 '19 at 19:52
  • Do you want to copy a file to the default destination if there is no input or something copy it from the input to `Templatecopy.doc`? – dan1st Sep 24 '19 at 05:05

1 Answers1

0

I suggest the following commented code to make this batch file fail-safe as described in answer on How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?

@echo off
:FileNamePrompt
rem Undefine environment variable FileToCopy.
set "FileToCopy="
rem Prompt user for the file name.
set /P "FileToCopy=Please enter a file name and extension: "
rem Has the user not entered anything, prompt the user once more.
if not defined FileToCopy goto FileNamePrompt
rem Remove all double quotes from user input string.
set "FileToCopy=%FileToCopy:"=%"
rem Has the user input just one or more ", prompt the user once more.
if not defined FileToCopy goto FileNamePrompt
rem Check if the user input string really references an existing file.
if not exist "%FileToCopy%" (
    echo File "%FileToCopy%" does not exist.
    goto FileNamePrompt
)
rem Check if the user input string really references
rem an existing file and not an existing directory.
if exist "%FileToCopy%\" (
    echo "%FileToCopy%" references a directory.
    goto FileNamePrompt
)
copy /B /Y "%FileToCopy%" "%~dp0Templatecopy.doc" >nul

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... explains %~dp0 which is expanding to drive and path of argument 0 which is the batch file path always ending with a backslash.
  • copy /?
  • echo /?
  • goto /?
  • if /?
  • rem /?
  • set /?
Mofi
  • 46,139
  • 17
  • 80
  • 143