2

Using windows CMD/BAT, how would I be able to copy a file (an executable / .exe file in this case) to a new location if I don't no the ABSOLUTE location of the source file? I'm rather new to CMD and BAT files.

The specified file itself is dependant on the input of the user; therefore the path of said file is not specified in the code itself.

Ex. Copying MSpaint.exe (Located in C:\Windows\System32, although for the context of the question, we don't know that) to a desktop folder called PAINT.

I've tried using

copy "*\mspaint.exe" "C:\Users\User\Desktop\PAINT"

But the error statement says

The filename, directory name, or volume label syntax is incorrect.
    0 files(s) copied.

I've tried using the command without the wildcard path, to no further avail.

copy "mspaint.exe" "C:\Users\User\Desktop\PAINT"

Any help would be much appreciated!

Caspr
  • 57
  • 6
  • `Dir /s c:\filename.ext` searches the hard drive. `For /f %%A in ('Dir /s c:\filename.ext') Do Echo %%A` operates on a command output. –  Nov 12 '16 at 21:21
  • Open a command prompt window and run there `for /?`, `call /?`, `set /?` and finally also just `set` to get help on the 3 commands and which environment variables are defined by default by Windows. Command __DIR__ as suggested by Noodles is not really needed except the file to copy is in a hidden or system directory or is itself a hidden or system file which `for /R ...` always ignore. For example the desktop folder of current user can be referenced with `"%USERPROFILE%\Desktop"`. – Mofi Nov 12 '16 at 21:29
  • If the executable source file is in your PATH, you can use `FOR /F` to find it. This may help. http://stackoverflow.com/questions/31575817/locate-file-and-copy-its-path-batch-script/31576863#31576863 If it is not in the PATH, then you will need to resort to `DIR /S` as suggested by Noodles. However, that does not seem to be reliable unless you make assumptions about where programs are on the machine. – lit Nov 12 '16 at 21:34
  • @Mofi The purpose of this is to be used in conjunction with a where /q "filename" in a cmd file. If successful (Errorlevel 0) a desktop folder called "filename" is created via mkdir and the /copy command will create the .exe shortcut into the file. The "WHERE" command follows the user input, and is successful in locating MSPAINT in this example, however the specified path of whatever filename is entered is unknown. Is it possible to copy the file.exe if the path is unspecified? The question has been edited for clarity. – Caspr Nov 12 '16 at 21:45

2 Answers2

1

A rarely used special for variable expansion is poor mans where.exe ;-) So provided the looked for file is somewhere in the searchpath:

Edit changed to put exe in a var which is expanded to the fullpath when found.

Set "EXE=mspaint.exe" & set MyPath=.;%Path%
for %%A in (%EXE%) do Set EXE=%%~$MyPath:A
If defined EXE copy "%EXE%" "C:\Users\User\Desktop\PAINT"
Cite from for /?
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string
  • This works perfectly. Is there any way to change the program name in the FOR loop to a variable? [FOR %%A in (%Variable%) do ()]? I noticed in the 'for /?' that it mentions wildcards may be used. – Caspr Nov 13 '16 at 03:30
  • @Caspr changed the behaviour accordingly in the above batch snippet –  Nov 13 '16 at 14:21
0

Here is a simple example batch code:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "FileToFind="
set /P "FileToFind=Please enter file name: "
for /F %%I in ('dir "C:\!FileToFind!" /A-D /B /S 2^>nul') do (
    set "FilePath=%%~dpI"
    goto FileFound
)
echo Could not find !FileToFind! on drive C:
goto EndBatch

:FileFound
echo Found !FileToFind! in %FilePath%

:EndBatch
echo.
endlocal
pause

This batch code can find also a hidden or system file respectively a file in a hidden or system directory on drive C:.

An alternate solution ignoring hidden and system directories/files is:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "FileToFind="
set /P "FileToFind=Please enter file name: "
for /R "C:\" %%I in ("!FileToFind!*") do (
    set "FilePath=%%~dpI"
    goto FileFound
)
echo Could not find !FileToFind! on drive C:
goto EndBatch

:FileFound
echo Found !FileToFind! in %FilePath%

:EndBatch
echo.
endlocal
pause

This solution has additionally the disadvantage that on entering test.txt it would also find test.txt.bak.

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.

  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • pause /?
  • set /?
  • setlocal /?
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Just attempted to try this. I tested for both "mspaint.exe"/"mspaint" and "test.txt"/"test" (where test is a blank .txt file I just created on my desktop to test this) and received no result. Turning @echo off reveals that the code simply pauses after the GOTO file found at the end of the FOR loop. The command prompt simply remains stuck, unable to accept any input or running any code. Any clue as to why this would be? Thanks! – Caspr Nov 12 '16 at 22:41
  • Well, this code was just an example, nothing more. It does not check if the file name was entered just partly, i.e. without file extension, or was entered with double quotes which need to be removed, or was entered with a path which needs to be removed, or can't be a file name at all as contains characters not being valid for a file name, ... In other words the batch code EXAMPLE does not verify at all what the user entered before running the file search. You did not ask how to make a fool proof batch file where the user can enter a file name and check that input on possible mistakes. – Mofi Nov 13 '16 at 09:32