0

Could someone assist with a batch move script.

I'd like the script to prompt for a File Source path, and a File Destination path, and a Filename.

Then from the above 3 perform a move of the file. I have a script that would search if a file is present and display in notepad the results. Hoping to add to it for the above.

@echo off
:start

set /p filesource="Enter file source path:  "
set /p filedestination="Enter file destination path:  "
set /p filename="Enter file name to look for:  "

if "%filesource%"=="" goto :error
if "%filedestination%"=="" goto :error
if "%filename%"=="" goto :error

echo Moving %filename% From "%filesource%\%filename%" to %filedestination%.  
>> %f%
move "%filesource%\%filename%" %filedestination
echo. >> %f%
notepad %f%
goto :end

:error
echo You did not enter correct information.
pause
goto :start
:end
Steve Walsh
  • 3
  • 1
  • 3
  • 4
    you missed a `%` at the end of your `move` line. What should the line `>>%f%` do? Also it's not clear to me what you want to achieve. Maybe you can provide an example of the expected output? – Stephan Dec 10 '18 at 19:07

1 Answers1

0

The main mistake is on command line:

move "%filesource%\%filename%" %filedestination

The second % is missing on referencing environment variable filedestination.

The destination path should be also enclosed in double quotes in case of containing a space or one of these characters &()[]{}^=;!'+,`~<|>.

And it should be made sure that destination path really ends with a backslash to move the file into this directory because of otherwise it would be possible to move the file to a directory with a different file name.

So this command line should be:

move "%filesource%\%filename%" "%filedestination%\"

But honestly the batch file needs much more code to be fail safe. For example:

@echo off
goto Main

:UserPrompt
set "UserInput="
set /P "UserInput=Enter file %~2: "
rem Has the user entered no string?
if not defined UserInput goto ErrorInput
rem Remove all double quotes.
set "UserInput=%UserInput:"=%"
rem Has the user entered a string consisting only of double quotes?
if not defined UserInput goto ErrorInput

rem Check if file or directory exist at all.
if %1 == FileName (
    rem The file name should not contain a directory separator.
    if not "%UserInput:\=%" == "%UserInput%" goto ErrorInput
    if not "%UserInput:/=%" == "%UserInput%" goto ErrorInput
    if not exist "%FileSource%%UserInput%" (
        echo/
        echo There is no file "%FileSource%%UserInput%"
        echo/
        pause
        echo/
        goto UserPrompt
    )
    rem Prevent moving the batch file.
    for %%I in ("%FileSource%%UserInput%") do if "%%~fI" == "%~f0" (
        echo/
        echo This batch file cannot be moved.
        echo/
        pause
        echo/
        goto UserPrompt
    )
    goto ReturnValue
)

rem Get full path in case of user entered a relative path.
for %%I in ("%UserInput%") do set "UserInput=%%~fI"
if not "%UserInput:~-1%" == "\" set "UserInput=%UserInput%\"
if not exist "%UserInput%" (
    echo/
    echo There is no directory "%UserInput%"
    echo/
    pause
    echo/
    goto UserPrompt
)

:ReturnValue
set "%1=%UserInput%"
goto :EOF

:ErrorInput
echo/
echo You did not enter correct information.
echo/
pause
echo/
goto UserPrompt


:Main
setlocal EnableExtensions DisableDelayedExpansion
call :UserPrompt FileSource "source path"
call :UserPrompt FileTarget "destination path"
call :UserPrompt FileName "name to look for"

set "LogFile=%TEMP%\%~n0.tmp"
echo Moving "%FileName%" from "%FileSource%" to "%FileTarget%".>"%LogFile%"
move /Y "%FileSource%%FileName%" "%FileTarget%" 2>>"%LogFile%"
echo/>>"%LogFile%"
Notepad.exe "%LogFile%"
del "%LogFile%" 2>nul
endlocal

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 /?
  • del /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • move /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143