将目录中的不同文件夹和文件名批量分配给变量
Assign different folder and file names from directory to variable in batch
我正在尝试将给定目录中的不同文件夹名称分配给不同的变量。
我可以在 unix 中做同样的事情,但它看起来很难从批处理中实现。下面是unix代码。在文件 changed.txt 中,我的行数如下
branches/abc/xyz/components/ADAdapter/release/post_commit_hook.zip
branches/abc/xys/components/ADAdapter/release/post_commit_hook.zip
branches/abc/xyz/components/ADAdapter/release/post_commit_hook.zip
branches/abc/xyz/components/ADAdapter/release/post_commit_hook.zip
for x in `cat changed.txt`
do
a=`echo $x |egrep -i 'release'|egrep -i 'zip'| sed 's/.*release\///'|
sed 's/^[ \t]*//;s/[ \t]*$//' `
b="$b,$a"
c=`echo $x | cut -d "/" -f1`
d=`echo $x | cut -d "/" -f5`
e="$e,$c"
f="$f,$d"
done
PACKAGE=`echo $b | sed 's/[,]//'`
BRANCH=`echo $e | sed 's/[,]//'`
UPDATEFOLDER=`echo $f | sed 's/[,]//'`
echo PACKAGE is $PACKAGE
echo BRANCH is $BRANCH
echo UPDATEFOLDER is $UPDATEFOLDER
我期待这个输出
PACKAGE is post_commit_hook.zip,post_commit_hook.zip,post_commit_hook.zip,post_commit_hook.zip
BRANCH is branches,branches,branches,branches
UPDATEFOLDER is ADAdapter,ADAdapter,ADAdapter,ADAdapter
谁能帮我用 Batch 做同样的事情?
这是一个基于我的评论的示例:
@Echo Off
SetLocal EnableDelayedExpansion
Rem This Undefines the variables
For %%A In (PKG BRN UPD) Do Set "%%A="
Rem This will propagate the variables
For /F "Tokens=1,5,7 Delims=/" %%A In (
'FindStr /I "\/release/.*\.zip$" "changed.txt"'
) Do (
If Defined BRN (Set "BRN=!BRN!,%%A") Else Set "BRN=%%A"
If Defined UPD (Set "UPD=!UPD!,%%B") Else Set "UPD=%%B"
If Defined PKG (Set "PKG=!PKG!,%%C") Else Set "PKG=%%C"
)
Rem This will show the variables with their content
For %%A In (PKG BRN UPD) Do Set %%A 2>Nul
Pause
[编辑/]
作为 acronicks 评论的结果,如果文件使用 LF
行结尾或不以标准 CRLF
结尾终止,这里的版本应该足够了。它还会更改变量名称以匹配问题中的名称。
@Echo Off
SetLocal EnableDelayedExpansion
Rem This Undefines the variables
For %%A In (PACKAGE BRANCH UPDATEFOLDER) Do Set "%%A="
Rem This will propagate the variables
For /F "Tokens=1,5,7 Delims=/" %%A In (
'More "changed.txt"^|FindStr /I "\/release/.*\.zip$"'
) Do (
If Defined BRANCH (Set "BRANCH=!BRANCH!,%%A") Else Set "BRANCH=%%A"
If Defined UPDATEFOLDER (Set "UPDATEFOLDER=!UPDATEFOLDER!,%%B") Else Set "UPDATEFOLDER=%%B"
If Defined PACKAGE (Set "PACKAGE=!PACKAGE!,%%C") Else Set "PACKAGE=%%C"
)
Rem This will show the variables with their content
Echo PACKAGE is %PACKAGE%
Echo BRANCH is %BRANCH%
Echo UPDATEFOLDER is %UPDATEFOLDER%
Pause
[Edit2 /]
这里有一个小修改,允许先将命令保存到变量中,由于评论.
@Echo Off
SetLocal EnableDelayedExpansion
Rem This Undefines the variables
For %%A In (PACKAGE BRANCH UPDATEFOLDER) Do Set "%%A="
Rem This will propagate the variables
Set "a=More "changed.txt"|FindStr /I "\/release/.*\.zip$""
For /F "Tokens=1,5,7 Delims=/" %%A In ('"%a%"') Do (
If Defined BRANCH (Set "BRANCH=!BRANCH!,%%A") Else Set "BRANCH=%%A"
If Defined UPDATEFOLDER (Set "UPDATEFOLDER=!UPDATEFOLDER!,%%B") Else Set "UPDATEFOLDER=%%B"
If Defined PACKAGE (Set "PACKAGE=!PACKAGE!,%%C") Else Set "PACKAGE=%%C"
)
Rem This will show the variables with their content
Echo PACKAGE is %PACKAGE%
Echo BRANCH is %BRANCH%
Echo UPDATEFOLDER is %UPDATEFOLDER%
Pause
我正在尝试将给定目录中的不同文件夹名称分配给不同的变量。
我可以在 unix 中做同样的事情,但它看起来很难从批处理中实现。下面是unix代码。在文件 changed.txt 中,我的行数如下
branches/abc/xyz/components/ADAdapter/release/post_commit_hook.zip branches/abc/xys/components/ADAdapter/release/post_commit_hook.zip branches/abc/xyz/components/ADAdapter/release/post_commit_hook.zip branches/abc/xyz/components/ADAdapter/release/post_commit_hook.zip
for x in `cat changed.txt`
do
a=`echo $x |egrep -i 'release'|egrep -i 'zip'| sed 's/.*release\///'|
sed 's/^[ \t]*//;s/[ \t]*$//' `
b="$b,$a"
c=`echo $x | cut -d "/" -f1`
d=`echo $x | cut -d "/" -f5`
e="$e,$c"
f="$f,$d"
done
PACKAGE=`echo $b | sed 's/[,]//'`
BRANCH=`echo $e | sed 's/[,]//'`
UPDATEFOLDER=`echo $f | sed 's/[,]//'`
echo PACKAGE is $PACKAGE
echo BRANCH is $BRANCH
echo UPDATEFOLDER is $UPDATEFOLDER
我期待这个输出
PACKAGE is post_commit_hook.zip,post_commit_hook.zip,post_commit_hook.zip,post_commit_hook.zip BRANCH is branches,branches,branches,branches UPDATEFOLDER is ADAdapter,ADAdapter,ADAdapter,ADAdapter
谁能帮我用 Batch 做同样的事情?
这是一个基于我的评论的示例:
@Echo Off
SetLocal EnableDelayedExpansion
Rem This Undefines the variables
For %%A In (PKG BRN UPD) Do Set "%%A="
Rem This will propagate the variables
For /F "Tokens=1,5,7 Delims=/" %%A In (
'FindStr /I "\/release/.*\.zip$" "changed.txt"'
) Do (
If Defined BRN (Set "BRN=!BRN!,%%A") Else Set "BRN=%%A"
If Defined UPD (Set "UPD=!UPD!,%%B") Else Set "UPD=%%B"
If Defined PKG (Set "PKG=!PKG!,%%C") Else Set "PKG=%%C"
)
Rem This will show the variables with their content
For %%A In (PKG BRN UPD) Do Set %%A 2>Nul
Pause
[编辑/]
作为 acronicks 评论的结果,如果文件使用 LF
行结尾或不以标准 CRLF
结尾终止,这里的版本应该足够了。它还会更改变量名称以匹配问题中的名称。
@Echo Off
SetLocal EnableDelayedExpansion
Rem This Undefines the variables
For %%A In (PACKAGE BRANCH UPDATEFOLDER) Do Set "%%A="
Rem This will propagate the variables
For /F "Tokens=1,5,7 Delims=/" %%A In (
'More "changed.txt"^|FindStr /I "\/release/.*\.zip$"'
) Do (
If Defined BRANCH (Set "BRANCH=!BRANCH!,%%A") Else Set "BRANCH=%%A"
If Defined UPDATEFOLDER (Set "UPDATEFOLDER=!UPDATEFOLDER!,%%B") Else Set "UPDATEFOLDER=%%B"
If Defined PACKAGE (Set "PACKAGE=!PACKAGE!,%%C") Else Set "PACKAGE=%%C"
)
Rem This will show the variables with their content
Echo PACKAGE is %PACKAGE%
Echo BRANCH is %BRANCH%
Echo UPDATEFOLDER is %UPDATEFOLDER%
Pause
[Edit2 /]
这里有一个小修改,允许先将命令保存到变量中,由于评论.
@Echo Off
SetLocal EnableDelayedExpansion
Rem This Undefines the variables
For %%A In (PACKAGE BRANCH UPDATEFOLDER) Do Set "%%A="
Rem This will propagate the variables
Set "a=More "changed.txt"|FindStr /I "\/release/.*\.zip$""
For /F "Tokens=1,5,7 Delims=/" %%A In ('"%a%"') Do (
If Defined BRANCH (Set "BRANCH=!BRANCH!,%%A") Else Set "BRANCH=%%A"
If Defined UPDATEFOLDER (Set "UPDATEFOLDER=!UPDATEFOLDER!,%%B") Else Set "UPDATEFOLDER=%%B"
If Defined PACKAGE (Set "PACKAGE=!PACKAGE!,%%C") Else Set "PACKAGE=%%C"
)
Rem This will show the variables with their content
Echo PACKAGE is %PACKAGE%
Echo BRANCH is %BRANCH%
Echo UPDATEFOLDER is %UPDATEFOLDER%
Pause