Нужна помощь с пакетным сценарием Windows, который должен установить значение переменной var на основе части имени файла
Это не домашнее задание - у кого будет домашнее задание по пакетному сценарию? Мне нужно что-то автоматизировать. В настоящее время существует жестко запрограммированный пакетный скрипт, предназначенный для ежедневного запуска систем, и он должен работать динамично. Все, что нужно для ввода, - это номер сборки, который можно определить по имени файла, расположенного в... скажем C:\DumpLocation\
, Я не очень хорош в пакетных сценариях и ищу пакетный ниндзя. Если бы это зависело от меня, я бы сам написал это на Python, но я не могу ожидать, что другие установят его только для этого. PowerShell также доступен не на каждом компьютере с Windows, поэтому пакетный скрипт является наименьшим общим знаменателем.
Это должно помочь: http://www.techsupportforum.com/microsoft-support/windows-xp-support/54848-set-variable-based-output-seach-string-batch.html
Вот что я хочу, чтобы скрипт делал:
dirToLookAt = 'C:\DumpLocation\'
# In that location there should be a single file named
# Custom_SomethingBuild34567Client_12345.zip
# I want to extract the build number into a variable to this effect:
buildNumber = '34567'
# strings which surround the build number are fixed.
# If there is more than one zip file with a build number in it,
# I need to print a warning and pick the largest one.
# I can do the rest.
Следующее должно также помочь:
http://www.computing.net/answers/programming/batch-string-substitution/12097.html
Пожалуйста, дайте мне знать, если у вас есть вопросы.
1 ответ
При условии, что Custom_SomethingBuild
а также Client_12345.zip
являются константами, это должно сделать свое дело:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM Get a count of the files in the directory
set /a FileCount=0
for /f "tokens=* delims= " %%a in ('dir/s/b/a-d C:\DumpLocation') do (
set /a FileCount+=1
)
REM If the file count is greater than or equal to 2, warn the user
IF %FileCount% GTR 1 ECHO The total number of files in the directory is: %FileCount%
REM If the file count is less than or equal to 0, pause and exit
IF %FileCount% LEQ 0 PAUSE & EXIT
:: For each build number, use that number if it is the largest number
:: In the loop, we'll strip out the assumed constants, leaving us with a build number.
SET Build=
SET /a BuildNum=0
FOR /F %%A IN ('DIR /P /B C:\DumpLocation') DO (
SET Build=%%A
SET Build=!Build:Custom_SomethingBuild=!
SET Build=!Build:Client_12345.zip=!
IF !Build! GTR !BuildNum! SET /a BuildNum=!Build!
)
ECHO The greatest build number in the directory is: %BuildNum%
PAUSE