Display folder contents to user and allow them to pick

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Display folder contents to user and allow them to pick

#1 Post by SIMMS7400 » 17 Jul 2019 10:03

Hi Folks -

I have a need to create a script that will display the folder contents from (4) different data backup folders I have. At most, there would only be 44 data files to choose from.

I want the script to essentially cycle through each backup folder and display the content (with data modified) but also allow the user to be able to pick a certain data file. I am thinking something along the lines of CHOICE functionality, but dynamic where it would include a number to the beginning of the line in the command window so then the user could select (i.e. "4").

Here is my script to collect all content, but the output isn't really neat.

Code: Select all

:SELECT_BACKUP

FOR %%D IN ("Daily" "Weekly" "Monthly" "Yearly") DO (
    FORFILES /P "%BACKUPPATH%%%~D" /C "cmd /c ECHO @file @fdate @ftime"
)
pause
Does anyone do anything similar? If so, could you share your idea?

Thank you!

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Display folder contents to user and allow them to pick

#2 Post by SIMMS7400 » 17 Jul 2019 10:33

In doing some research, I came up with this approach which get's me 90% there:

Code: Select all

:SELECT_BACKUP
SET "DOTS=..."
FOR %%A IN ("Daily" "Weekly" "Monthly" "Yearly") DO (
 FOR /F "delims=" %%a IN ('DIR /S /B "%BACKUPPATH%%%A"') DO ( CALL :PROCESS "%%~a"
 )
)

:REPEAT
CLS
FOR /l %%A IN (1,1,%ZIP%) DO (
 IF %%A lss 10 (ECHO(%%A%DOTS%%DOTS:~0,1%!$%%A!) ELSE (ECHO(%%A%DOTS%!$%%A!)
)
SET SELECTION=
ECHO.
SET /p SELECTION="Please choose 1..%ZIP% : "
IF NOT DEFINED $_%SELECTION% GOTO REPEAT

pause

:PROCESS

SET /a "ZIP+=1"
SET "$_%ZIP%=%~1"
SET "$=%~1"
FOR %%A IN ("%$%") DO SET "$%ZIP%=%%~nxA"
GOTO :EOF
But the choice command only allows you set a number value. Is there a way to modify to set to the actual value that is displayed (i.e. Wednesday.zip which is located in the Daily folder).
DOSTIPS.png
DOSTIPS.png (7.8 KiB) Viewed 7541 times
Thanks!

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Display folder contents to user and allow them to pick

#3 Post by penpen » 18 Jul 2019 15:41

If i understand your code and task right, then just add that echo before the pause command (and put an goto :eof behind pause to not falsely entering the :process part of your batch).

Code: Select all

echo(Your choice is: !$_%SELECTION%!
pause
goto :eof
penpen

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Display folder contents to user and allow them to pick

#4 Post by SIMMS7400 » 19 Jul 2019 07:55

penpen wrote:
18 Jul 2019 15:41
If i understand your code and task right, then just add that echo before the pause command (and put an goto :eof behind pause to not falsely entering the :process part of your batch).

Code: Select all

echo(Your choice is: !$_%SELECTION%!
pause
goto :eof
penpen

Hi PenPen -

That worked perfectly, thank you!

I do have one additional question. Is there a way to also display the date modified value for each of the files? Thanks!

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Display folder contents to user and allow them to pick

#5 Post by penpen » 20 Jul 2019 13:46

I'm unsure if the date information of a "for-variable" is the date modified, but i would try something like that:

Code: Select all

for %%a in ("!$_%SELECTION%!") do echo(Your choice is:  "%%~a"  %~ta
penpen

Post Reply