The following batch file can be used for this task:
@echo off
if not exist "C:\Device\Number\" goto :EOF
setlocal EnableExtensions DisableDelayedExpansion
cls
echo Device folders in folder: "C:\Device\Number"
echo/
dir "C:\Device\Number\*" /AD /ON /W | %SystemRoot%\System32\findstr.exe /B /L /C:[
echo/
:PromptDevice
set "DeviceFolder="
set /P "DeviceFolder=Please enter device: "
rem Has the user not input any string?
if not defined DeviceFolder goto PromptDevice
rem Remove all double quotes from string value.
set "DeviceFolder=%DeviceFolder:"=%"
rem Has the user input just one or more double quotes?
if not defined DeviceFolder goto PromptDevice
rem If the user has input the device with [ at beginnning and with ] at end
rem as output by command DIR, remove those square brackets from device string.
if "%DeviceFolder:~0,1%" == "[" set "DeviceFolder=%DeviceFolder:~1%"
if not defined DeviceFolder goto PromptDevice
if "%DeviceFolder:~-1%" == "]" set "DeviceFolder=%DeviceFolder:~0,-1%"
if not defined DeviceFolder goto PromptDevice
rem Determine name of subfolder with date in name.
set "TargetFolder="
for /D %%I in ("C:\Device\Number\%DeviceFolder%\xyz_??-??-??") do set "TargetFolder=%%I\Path\To\Folder"
if not defined TargetFolder (
echo/
echo Subfolder xyz_??-??-?? not found in: "C:\Device\Number\%DeviceFolder%"
echo/
goto PromptDevice
)
if not exist "%TargetFolder%\" (
echo/
echo Folder "%TargetFolder%" not found.
echo/
goto PromptDevice
)
echo/
echo Copying log.pdf to "%TargetFolder%" ...
copy /Y /B "%~dp0log.pdf" "%TargetFolder%\"
echo/
endlocal
pause
After an initial check if folder C:\Device\Number
exists at all command DIR is used to output all subfolders in this folder order by name in wide format with using FINDSTR
to filter out all lines output by DIR not starting with [
, i.e. header and footer lines. DIR outputs also [.]
and [..]
which the users have to ignore.
Then the user is prompted for the name of the device number folder. See How to stop Windows command interpreter from quitting batch file execution on an incorrect user input? It explains the reasons for the additional IF conditions used to prevent an unexpected exit of batch file processing by Windows command processor because of a syntax error which could occur otherwise on user makes a mistake on entering the device folder name.
The batch file uses command FOR with option /D
to get name of subfolder with date if the folder with entered the device folder name exists at all and this device folder contains a non-hidden folder matching the wildcard pattern xyz_??-??-??
at all. Otherwise the user is informed about missing folder xyz_??-??-??
in entered device folder and is prompted once again to enter the device folder name as most likely the user input the folder name not 100% correct.
After one more check if the target folder really exists, the user is informed about file copying and the file is copied by batch script, hopefully also successfully. The batch file expects the file to copy in directory containing the batch file referenced with %~dp0
which always expands to a batch string ending with directory separator \
.
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
... drive and path of argument 0, i.e. full batch file path.
cls /?
copy /?
dir /?
echo /?
endlocal /?
findstr /?
goto /?
if /?
pause /?
rem /?
set /?
setlocal /?
See also Microsoft article about Using command redirection operators for an explanation of redirection operator |
.