Batch文件 – 在函数内搜索和替换函数参数时遇到问题。

huangapple go评论55阅读模式
英文:

Batch file - having trouble with searching and replacing function argument within a function

问题

以下是翻译好的部分:

我有以下的批处理文件:

@echo off
setlocal enabledelayedexpansion
set "ChosenFolder="
for /r /d %%f in (.) do call :function "%%f"
goto end

:function
echo %%~1:  %~1
set "ChosenFolder=!%~1:*\web\=!"
echo ChosenFolder:  %ChosenFolder%

:end

批处理文件位于 `c:\docs\webs\my website name\web\`。它应该遍历每个子文件夹,并生成每个文件夹的相对路径,将每个路径从`\web\`开始的部分剥离掉。总共有两个子文件夹,`apples` 和 `apples\oranges`,所以输出应该是这样的:

%~1:  c:\docs\webs\my website name\web\.
ChosenFolder:  \.
%~1:  c:\docs\webs\my website name\web\apples\.
ChosenFolder:  apples\.
%~1:  c:\docs\webs\my website name\web\apples\oranges\.
ChosenFolder:  apples\oranges\.

但是,实际输出是这样的:

%~1:  c:\docs\webs\my website name\web\.
ChosenFolder:  c:\docs\webs\my website name\web\.:*\web\=!
%~1:  c:\docs\webs\my website name\web\apples\.
ChosenFolder:  c:\docs\webs\my website name\web\apples\.:*\web\=!
%~1:  c:\docs\webs\my website name\web\apples\oranges\.:*\web\=!
ChosenFolder:  c:\docs\webs\my website name\web\apples\oranges\.:*\web\=!

我做错了什么?
英文:

I have the following batch file:

@echo off
setlocal enabledelayedexpansion
set "ChosenFolder="
for /r /d %%f in (.) do call :function "%%f"
goto end

:function
echo %%~1:  %~1
set "ChosenFolder=!%~1:*\web\=!"
echo ChosenFolder:  %ChosenFolder%

:end

The batch file is located at c:\docs\webs\my website name\web\. It is supposed to run through every subfolder and generate relative paths of each folder, stripping each path of everything up through \web\. There are two total subfolders, apples and apples\oranges, so the output should look like this:

%~1:  c:\docs\webs\my website name\web\.
ChosenFolder:  \.
%~1:  c:\docs\webs\my website name\web\apples\.
ChosenFolder:  apples\.
%~1:  c:\docs\webs\my website name\web\apples\oranges\.
ChosenFolder:  apples\oranges\.

But instead, this is what I get:

%~1:  c:\docs\webs\my website name\web\.
ChosenFolder:  c:\docs\webs\my website name\web\.:*\web\=!
%~1:  c:\docs\webs\my website name\web\apples\.
ChosenFolder:  c:\docs\webs\my website name\web\apples\.:*\web\=!
%~1:  c:\docs\webs\my website name\web\apples\oranges\.:*\web\=!
ChosenFolder:  c:\docs\webs\my website name\web\apples\oranges\.:*\web\=!

What am I doing wrong?

答案1

得分: 1

Here is the translated code portion:

@ECHO OFF
SETLOCAL
set "ChosenFolder="
for %%e in (
"c:\docs\webs\my website name\web\."
"c:\docs\webs\my website name\web\apples\."
"c:\docs\webs\my website name\web\apples\oranges\."
) do call :function "%%~e"
goto end

:function
echo %%~1:  %~1
set "ChosenFolder=%~1"
SET "chosenfolder=%chosenfolder:*\web\=%"
echo ChosenFolder:  %ChosenFolder%

:end

Please note that the code has been translated, and I have removed the extraneous information as per your request.

英文:
@ECHO OFF
SETLOCAL
set "ChosenFolder="
for %%e in (
"c:\docs\webs\my website name\web\."
"c:\docs\webs\my website name\web\apples\."
"c:\docs\webs\my website name\web\apples\oranges\."
) do call :function "%%~e"
goto end

:function
echo %%~1:  %~1
set "ChosenFolder=%~1"
SET "chosenfolder=%chosenfolder:*\web\=%"
echo ChosenFolder:  %ChosenFolder%

:end

Note: I used the directorynames you nominated in a simple for loop for testing, consequently the metavariable %%e is quoted and could be passed 'bare' or as I have shown, dequoted and quoted.

I prefer to avoid ADFNPSTXZ (in either case) as metavariables (loop-control variables) 
ADFNPSTXZ are also metavariable-modifiers which can lead to difficult-to-find bugs 
(See `for/f` from the prompt for documentation)

The problem with your code is that substringing only works on ordinary variables, not on metavariables.

Note also that delayedexpansion is not required unless you choose to use the single line

set "ChosenFolder=%~1"&SET "chosenfolder=!chosenfolder:*\web\=!"

huangapple
  • 本文由 发表于 2023年4月17日 01:10:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定