Get set /p to accept file suffix.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PAB
Posts: 151
Joined: 12 Aug 2019 13:57

Get set /p to accept file suffix.

#1 Post by PAB » 29 Jun 2025 03:28

Good morning,

I am wanting to input a file suffix such as *.xls* [ includes decimal point and asterix ] into a set /p variable which will then get used within the script, but for some reason I can't get it to work.

Here is the relevant part . . .

Code: Select all

              set "User_Input="
              set /p "User_Input=>Enter the 'File Type' [Suffix] including the decimal point and Asterix if applicable and press <Enter>: "
If I hard code *.xls*, it works.

Code: Select all

set "User_Input=*.xls*"
Thanks in advance.

aGerman
Expert
Posts: 4712
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Get set /p to accept file suffix.

#2 Post by aGerman » 29 Jun 2025 04:11

Can't repro.

Code: Select all

@echo off &setlocal
              set "User_Input="
              set /p "User_Input=>Enter the 'File Type' [Suffix] including the decimal point and Asterix if applicable and press <Enter>: "
echo %User_Input%
pause
Screenshot 2025-06-29 121118.png
Screenshot 2025-06-29 121118.png (18.69 KiB) Viewed 122 times
Steffen

miskox
Posts: 656
Joined: 28 Jun 2010 03:46

Re: Get set /p to accept file suffix.

#3 Post by miskox » 29 Jun 2025 11:25

Works for me too.

Windows 10, 64-bit.

Saso

miskox
Posts: 656
Joined: 28 Jun 2010 03:46

Re: Get set /p to accept file suffix.

#4 Post by miskox » 29 Jun 2025 11:25

miskox wrote:
29 Jun 2025 11:25
Works for me too.

Windows 10, 64-bit.

Why don't you show us your result?

Saso

PAB
Posts: 151
Joined: 12 Aug 2019 13:57

Re: Get set /p to accept file suffix.

#5 Post by PAB » 30 Jun 2025 01:38

Thanks everyone for the replies.

Yes, it does work if there is an Asterix and a Decimal Point at the beginning, but if you just put in .xls for example it doesn't !

I might try and look later to see if I can get it to automatically put in the *. in the set /p so the user doesn't need to.

Thank again and kind regards.

miskox
Posts: 656
Joined: 28 Jun 2010 03:46

Re: Get set /p to accept file suffix.

#6 Post by miskox » 30 Jun 2025 02:08

Works as expected.

Code: Select all

c:\temp>a
>Enter the 'File Type' [Suffix] including the decimal point and Asterix if applicable and press <Enter>: *.xls*
*.xls*
>Enter the 'File Type' [Suffix] including the decimal point and Asterix if applicable and press <Enter>: .xls*
.xls*
>Enter the 'File Type' [Suffix] including the decimal point and Asterix if applicable and press <Enter>: .xls
.xls
c:\temp>
In your code you should check if the first character is an asterisk or not and addit if required. Variable User_Input will contain whatever you enter.

Saso

PAB
Posts: 151
Joined: 12 Aug 2019 13:57

Re: Get set /p to accept file suffix.

#7 Post by PAB » 30 Jun 2025 04:16

Thanks again.

Here is a cut down version of my code where .xls does not work but *.xls for example works great.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

              set "Suffix="
echo. &       set /p "Suffix=>Enter the [Suffix File Type] starting with the [Asterix] followed by the [Decimal Point] and press <Enter>: "

echo. & echo  Select the [Source] folder:
              Call :Browse_4_Folder
  PowerShell  Write-Host ^
         `n'  The [Source] folder chosen is: ' -NoNewline; Write-Host -ForegroundColor Cyan '%Location%'

PowerShell ^
     $CMD =Get-ChildItem %Location% -Filter %Suffix% -Recurse ^| Sort-Object -Property {$_.LastWriteTime} -Descending; ^
     $Tot =($CMD ^| Measure-Object).Count.ToString('#,##0'); ^
     $List=($CMD ^| Format-Table -AutoSize ^
     @{L='Full Name'           ;E={;if([string]::IsNullOrWhiteSpace($_.FullName))                                         {'-'} else {$_.FullName}}}, ^
     @{L='Last Write Date/Time';E={;if([string]::IsNullOrWhiteSpace($_.LastWriteTime))                                    {'-'} else {$_.LastWriteTime}};A='Right'}, ^
     @{L='Size ##'             ;E={;if($_.Length -lt 1073741824) {;[Math]::Round($_.Length / 1MB, 2, 00).ToString('###.00 MB')} else {;[Math]::Round($_.Length / 1GB, 2, 00).ToString('###.00 GB')}};A='Right'} ^| ^
Out-String -Width 1000).Trim("""`r`n"""); ^
     if ($List.Length) {Write-Host """`n `n___ %Suffix% Files [$Tot] - Sorted by [LastWriteDateTime -Descending] ___`n `n$List"""} else ^
                       {Write-Host """`n `n___ NO %Suffix% Files Available ___"""; exit 1} >> %Temp%\A.txt
     for /f "delims=" %%i in (%Temp%\A.txt) do echo. %%i
     del %Temp%\A.txt %Temp%\_.cmd %Temp%\_.vbs >nul 2>&1

echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & Exit

:Browse_4_Folder

set Location=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
(
    echo set Shell=WScript.CreateObject("Shell.Application"^) 
    echo set f=Shell.BrowseForFolder(0,"%~1",0,"%~2"^)
    echo if TypeName(f^)="Nothing" Then  
    echo WScript.echo "set Location=Dialog Cancelled" 
    echo WScript.Quit(1^)
    echo end if 
    echo set fs=f.Items(^):set fi=fs.Item(^) 
    echo p=fi.Path:WScript.echo "set Location=" ^& p
)>%vbs%
  CScript //NoLogo %vbs% > %cmd%
  for /f "delims=" %%a in (%cmd%) do %%a
  for %%f in (%vbs% %cmd%) do if exist %%f del /f /q %%f
  for %%g in ("vbs cmd") do if defined %%g set %%g=
Exit /b
Thanks in advance.

aGerman
Expert
Posts: 4712
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Get set /p to accept file suffix.

#8 Post by aGerman » 30 Jun 2025 12:01

I doubt that this is a cmd issue. It's just the way how the -Filter parameter of the PowerShell Get-ChildItem cmdlet works. It needs the asterisk as a wildcard for the file name. So, what are you trying to do? Manually prepend the asterisk whenever the user didn't enter it?

Steffen

PAB
Posts: 151
Joined: 12 Aug 2019 13:57

Re: Get set /p to accept file suffix.

#9 Post by PAB » 30 Jun 2025 14:19

Thanks Steffen,

Yes, pre-pending the Asterisk and Decimal Point would be quite good although adding it in the set /p doesn't seem to work for me !

Thanks in advance.

aGerman
Expert
Posts: 4712
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Get set /p to accept file suffix.

#10 Post by aGerman » 30 Jun 2025 15:05

Screenshot 2025-06-30 224917.png
Screenshot 2025-06-30 224917.png (44.01 KiB) Viewed 87 times
Screenshot 2025-06-30 225044.png
Screenshot 2025-06-30 225044.png (33.32 KiB) Viewed 87 times
As you can see, SET /P just assignes whatever the user enters. It cannot do anything else.

I f you insert these two lines after your SET /P

Code: Select all

set "Suffix=%Suffix:*.=%"
set "Suffix=*.%Suffix%"
it doesn't matter if the user omits the asterisk or even both asterisk and dot.
Screenshot 2025-06-30 225735.png
Screenshot 2025-06-30 225735.png (43.41 KiB) Viewed 87 times
Screenshot 2025-06-30 225845.png
Screenshot 2025-06-30 225845.png (44.72 KiB) Viewed 87 times
Screenshot 2025-06-30 225933.png
Screenshot 2025-06-30 225933.png (47.12 KiB) Viewed 87 times
Steffen

PAB
Posts: 151
Joined: 12 Aug 2019 13:57

Re: Get set /p to accept file suffix.

#11 Post by PAB » 01 Jul 2025 00:34

Thank you SO much Steffen,

That's perfect. I wouldn't have got that in a million years.

Thanks again.

aGerman
Expert
Posts: 4712
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Get set /p to accept file suffix.

#12 Post by aGerman » 01 Jul 2025 12:46

The strategy is simple.
1) If a dot is found in %Suffix%, remove everything that comes before including the dot itself. So "*.xls", ".xls" and "xls" all become "xls" in the first SET statement. (For more information run SET /? and read about environment variable substitution.)
2) Because Get-ChildItem requires it, prepend both the asterisk and the dot so that the result is always "*.xls".

Steffen

Post Reply