2

Using batch script, I go inside folder and in that folder, I've different files like -

ABc.txt
Pqr.png
.
.
Test-1.2.0.zip

Out of these file, I want to get name of a file which starts with Test and has extension .zip. In this case, Test-1.2.0.zip. As its version could be dynamic.

Could you please help me with the batch script to find Test-1.2.0.zip and return is as output?

3
  • 1
    In PowerShell you can just gci Test*.zip if you are in the correct dir, or gci C:\somewhere Test*.zip for a specific location
    – SimonS
    Jan 25, 2021 at 11:59
  • why do you list "powershell" in your tags? it looks like you want only BAT/CMD stuff ...
    – Lee_Dailey
    Jan 25, 2021 at 14:09
  • oh yeah I also thought this is PowerShell. Batch is the same though dir Test*.zip
    – SimonS
    Jan 25, 2021 at 14:51

2 Answers 2

1

By click right on windows logo in start bar choose "run" from the list and opene it. Then write this word "recent" and click OK. It will be open a recent folder on your laptop try search in it about your file.

1

Try using a For /F loop with:

where .:Strings/Characters+Wildcard+String/Characters

The where command assume "." as literal character, not Regex 1 Digit/Character:

1 Digit/character          <==>  ? 
1 or More Digit/Character  <==>  *
Test-  1 Digit/Character . 1 Digit/Character . 1 Digit/Character.zip        <==> Test-?.?.?.zip
Test-  1 Digit/Character . 1 Digit/Character . 1 or More Digit/Character:   <==> Test-?.?.*.zip
  • Use For /F with %%~nxi (Name eXTension)
@echo off 

for /f useback^tokens^=* %%i in (`where .:"Test-?.?.?.zip"`)do echo\%%~nxi
  • For Powershell:
Get-ChildItem -File '*.zip'| Where-Object {$_.Name -like 'Test-?.?.?.zip'} | Select-Object -ExpandProperty Name


# Or..
ls -af '*.zip'| ? {$_.Name -like 'Test-?.?.?.zip'} |select -Exp Name
  • Results:
Test-1.2.0.zip
Test-1.2.1.zip
Test-1.2.2.zip
Test-1.2.3.zip
Test-1.2.4.zip
Test-1.2.5.zip
Test-1.2.6.zip
Test-1.2.7.zip
Test-1.2.8.zip
Test-1.2.9.zip
for /f useback^tokens^=* %%i in (`where .:"Test-?.?.*.zip"`)do echo\%%~nxi
  • For Powershell:
Get-ChildItem -File '*.zip'| Where-Object {$_.Name -like 'Test-?.?.*.zip'} | Select-Object -ExpandProperty Name


# Or..
ls -af '*.zip'| ? {$_.Name -like 'Test-?.?.*.zip'} |select -Exp Name
  • Results:
Test-1.2.0.zip
Test-1.2.1.zip
Test-1.2.10.zip
Test-1.2.11.zip
Test-1.2.12.zip
Test-1.2.13.zip
Test-1.2.14.zip
Test-1.2.15.zip
Test-1.2.16.zip
Test-1.2.17.zip
Test-1.2.18.zip
Test-1.2.19.zip
Test-1.2.2.zip
Test-1.2.20.zip
Test-1.2.3.zip
Test-1.2.4.zip
Test-1.2.5.zip
Test-1.2.6.zip
Test-1.2.7.zip
Test-1.2.8.zip
Test-1.2.9.zip

  • For Reverse Sort use for ... ^| Sort /r`) do ... in cmd/bat code, for PowerShell add | Sort-Object -Descending
for /f useback^tokens^=* %%i in (`where .:"Test-?.?.*.zip"^|sort /r`)do echo\%%~nxi
  • For Powershell:
Get-ChildItem -File '*.zip'| Where-Object {$_.Name -like 'Test-?.?.*.zip'} | Select-Object -ExpandProperty Name | Sort-Object -Descending


# Or..
ls -af '*.zip'| ? {$_.Name -like 'Test-?.?.*.zip'} |select -Exp Name | Sort -Des
  • Results:
Test-1.2.9.zip
Test-1.2.8.zip
Test-1.2.7.zip
Test-1.2.6.zip
Test-1.2.5.zip
Test-1.2.4.zip
Test-1.2.3.zip
Test-1.2.20.zip
Test-1.2.2.zip
Test-1.2.19.zip
Test-1.2.18.zip
Test-1.2.17.zip
Test-1.2.16.zip
Test-1.2.15.zip
Test-1.2.14.zip
Test-1.2.13.zip
Test-1.2.12.zip
Test-1.2.11.zip
Test-1.2.10.zip
Test-1.2.1.zip
Test-1.2.0.zip


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.