0

I want to copy a file from flash drive E:\ to a local machine. But the folder structure on the local machine is something like this:

C:\Device\Number\One\xyz_01-01-19\Path\To\Folder
C:\Device\Number\Two\xyz_01-02-19\Path\To\Folder
C:\Device\Number\Three\xyz_01-03-19\Path\To\Folder
C:\Device\Number\Four\xyz_01-04-19\Path\To\Folder

There are hundreds of device folders like One, Two, etc. each unique to the device, but all the subfolders follow the same naming convention for each unit. xyz_ is the fixed beginning part of the name for the folder with date in format mm-dd-yy appended in folder name.

There is no rule to select automatic by batch script the device folder into which the file should be copied into a subfolder with known path inside variable date related subfolder of the user selected device folder. So the user of the batch file has to enter the device folder name. The file should be copied only to one of the device folders determined by the user.

Where I am running into an issue is that the path to the folder has a date in it that is different from one device folder to next device folder. Everything else is exactly the same, except for the date, and I have no idea how to get the folder name with date in folder name.

My script is similar to the following:

REM Asking for the "Device" number
set /P device="Device Number?"
REM Copying files to the server
copy E:\log.pdf C:\Device\Number\%device%\xyz_01-01-19\Path\To\Folder
Mofi
  • 46,139
  • 17
  • 80
  • 143
gbkabitz
  • 1
  • 2
  • So you are trying to resolve a path like `C:\Device\Number\*\xyz_??-??-??\Path\To\Folder`? If so, use nested [`for /D` loosp](https://ss64.com/nt/for_d.html), which allow to do a task for each of the matching folders (like echoing in this example): `for /D %%J in ("C:\Device\Number\*") do for /D %%I in ("%%~J\xyz_??-??-??") do echo/%%~I\Path\To\Folder`; from there you can get further, like inserting a condition for the name of the device folder, like `if "%%~nxJ" == "%device%" echo Device folder: %%~J`... – aschipfl Sep 11 '19 at 11:34

2 Answers2

0

If I'm understanding your question + comments correctly, this is the first part (and problem part) of your file structure:

C:.
└───Device
    └───Number
        ├───Four
        │   ├───xyz_1-1-19
        │   ├───xyz_1-2-19
        │   ├───xyz_1-3-19
        │   └───xyz_x-x-xx
        ├───One
        │   ├───xyz_1-1-19
        │   ├───xyz_1-2-19
        │   ├───xyz_1-3-19
        │   └───xyz_x-x-xx
        ├───Three
        │   ├───xyz_1-1-19
        │   ├───xyz_1-2-19
        │   ├───xyz_1-3-19
        │   └───xyz_x-x-xx
        └───Two
            ├───xyz_1-1-19
            ├───xyz_1-2-19
            ├───xyz_1-3-19
            └───xyz_x-x-xx

Unless you always know the criteria needed to be used to choose which xyz_x-x-xx folder to use, you must rely on a human deciding for you. In which case, you need to add user input to your script. Here's how you should approach this (in pseudo code):

n = 0
for every directory in "C:\Device\Number\%device%\" {
    n++
    folder[n]=path_to_folder
    echo n) directory_name
}
echo Choice:
choice = user_input

copy E:\log.pdf C:\Device\Number\%device%\folder[choice]\Path\To\Folder

Here's some reading to get you started:

BDM
  • 3,760
  • 3
  • 19
  • 27
0

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 |.

Mofi
  • 46,139
  • 17
  • 80
  • 143