You are not logged in.
Pages: 1
Hi,
I am trying to create a batch file to dynamically list the sub-directories in a folder, assigning a unique number to each of them and prompting the user to input the number of one of the sub-directories. I then need to copy the users input directory to another location. For example:
Folder structure:
R:\Project
R:\Information
R:\Documents
The cmd file will list each of these directories with a unique number
1 - Project
2 - Information
3 - Documents
Then the batch file would prompt for user input:
"Please enter the number of the directory to be copied:"
The user should then be able to input the number of the directory they want to be copied, and it will copy that directory to "C:\Copy".
At the moment I am achieving this by 'hard coding' the directories, however I want to have a self-maintaining batch file as there will be lots of sub-directories regularly added to the main directory.
Appreciate any help on this.
Thank you!
Offline
Do you only care about the directories inside of the root directory, or do you need to be able to pick a subdirectory that's inside of one of the directories (if there was a folder called English inside of the Documents folder, would that be listed as an option)?
Offline
Because if there's only one level of directories, you can use this:
@echo off
setlocal enabledelayedexpansion
cls
:: In your example, you'd set source_folder to R:\ and target_folder to C:\copy
set "source_folder=%~dp0\source_folder"
set "target_folder=%~dp0\target_folder"
:: Move into the folder that contains the subfolders
pushd %source_folder%
:: Iterate over every directory in the list and add them to an array. I'd prefer
:: to use choice instead of set, but making a good menu gets hard after 9 items.
set folder_counter=1
for /f "delims=" %%A in ('dir /b /a:d') do (
set "folder[!folder_counter!]=%%~A"
echo !folder_counter! - %%~A
set /a folder_counter+=1
)
echo(
:folder_picker
set /p "selection=Enter the number of one of the above options: "
if not defined folder[%selection%] goto :folder_picker
:: For whatever reason, copy only copies files. Just make a new folder instead.
mkdir "%target_folder%\!folder[%selection%]!"
Copy "%source_folder%\!folder[%selection%]!" "%target_folder%\!folder[%selection%]!"
Offline
That is exactly what I was looking for! Thanks so much!!
Offline
Pages: 1