-2

I have the following MS Windows batch file code:

@echo off
cd\
dir/s *.docx *.xlsx *.ppt *.docx
SET /p input= %data% "
copy "%data%" C:\abc\
pause

This command shows all 4 types of extension list all over drives, but I want to take input from user and then copy to the desired location.

What am I missing?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Batcher
  • 5
  • 4
  • There are only 3 file types one is doubled. I guess the error stems from using `%data%` instead of `%input%` If the var data isn't previously assigned a value it is empty. –  Jan 20 '17 at 13:55
  • 2
    Run it after changing `@echo off` to `@echo ON`. Apply http://ss64.com/nt/ as reference. Unclear what is `%data%` and you do not _use_ variable `%input%` (post a [mcve]). – JosefZ Jan 20 '17 at 15:39
  • `set /? |find /i "set /p"` – Stephan Jan 20 '17 at 17:08

1 Answers1

0

Your main mistake is assigning the string entered by the user of the batch file to environment variable input but referencing on command copy the environment variable data which is not defined at all and therefore %data% is replaced twice before execution of the command line by nothing.

What about using this batch code?

@echo off
rem The next 2 commands are commented out as not needed for this task.
rem cd \
rem dir /s *.doc *.docx *.xlsx *.ppt

setlocal EnableExtensions EnableDelayedExpansion

:UserPrompt
echo/
echo Enter the file name with complete path or drag and drop
echo the file to copy from Windows Explorer over this window.
echo/
set "FileName=""
set /p "FileName=Enter file name: "
echo/

rem Remove all double quotes from entered string.
set "FileName=!FileName:"=!"

rem Has the user entered any file name at all?
if "!FileName!" == "" goto UserPrompt

rem Does the file really exist?
if exist "!FileName!" goto CopyFile

echo Error: The specified file does not exist.
echo/
goto UserPrompt

:CopyFile
copy "!FileName!" C:\abc\
echo/
endlocal
pause

Read the answer on Why is string comparison after prompting user for a string/option not working as expected? for an explanation on all the extra code used here.

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.

  • copy /?
  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?
Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143