Batch For Loop not separating string at specified delimiter





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am using a program called "Easy Context Menu" which allows me to create customized context menus when you right click in File Explorer. I wanted to create my own Context Menu item that allows me to copy a file and it's parenting folder structure into another directory, excluding all of the files in the parented folders.



Example:



target file path: C:thebigfoobar.txt

destination path: D:cow


I want the file 'bar.txt' and it's parenting folders up until 'big' to be copied into the directory 'cow' without the other files in 'big' or 'foo'. The end result should look like:



D:cowbigfoobar.txt


'big' should only contain 'foo' and 'foo' should only contain 'bar.txt'. The program allows me to send the file as a parameter to a file of my choice, in this case a batch file.



I have gotten stuck at the for loop in the ':main" subroutine. It will only print the whole path of the selected file and ignores the delimiter. 'countA' is returned as '1' after the loop and it would have printed the whole path minus drive letter. I do not understand why the for loop is ignoring the delimiter and not separating the path into separate folder names. The reason I am tying to do this is so the user can choose at which point the parented folders should be copied over. This is my first time writing actual code in batch so I still don't completely understand 'setlocal' and a few other things.



I have tried changing the tokens and I am able to choose the individual sections in between the slashes, but I am never able to capture the entire string as separate chunks. I have also changed the delimiter and got the same issue.



@echo off
setlocal
set _target=""
set _dest=""
if not exist %1 goto:error_no_path
set _target=%~pnx1
echo %_target%
goto :dest_selct

:dest_selct
echo select destination folder
pause
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose destination folder.',0,0).self.path""

for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"

setlocal enabledelayedexpansion
echo You chose %folder%
choice /c ync /n /m "Is this correct? ([Y]es, [N]o, [C]ancel)"
if %ERRORLEVEL% EQU 3 goto:eof
if %ERRORLEVEL% EQU 2 goto:dest_selct
if %ERRORLEVEL% EQU 1 (set _dest="%folder%")&&(goto:main)

:error_no_path
ECHO No file path/name
pause
GOTO:eof

:main
echo main
set _countA=0
echo count starts at %_countA%
for /f "tokens=* delims=" %%F in ("%_target%") do (
echo token %_countA% is %%F
echo count is %_countA%
set var!_countA!=%%F
set /a countA+=1
)
echo var1: %var1%
echo count is now %countA%
pause


I should have more than one variable at the end, each being the name of a folder that parents the target file and the count should be however many parent folders there are, plus the file itself. What I am actually getting is one variable and it contains the whole target path still.










share|improve this question


















  • 1





    With setlocal enabledelayedexpansion variables are local for your batch file, so at the end you should also echo !var1!"instead of "global" %var1% Maybe that helps?

    – Stacking For Heap
    Jan 4 at 7:01











  • @StackingForHeap The ! are only required inside a code block, there the %var1% would be evaluated at parse time.

    – LotPings
    Jan 4 at 9:39


















0















I am using a program called "Easy Context Menu" which allows me to create customized context menus when you right click in File Explorer. I wanted to create my own Context Menu item that allows me to copy a file and it's parenting folder structure into another directory, excluding all of the files in the parented folders.



Example:



target file path: C:thebigfoobar.txt

destination path: D:cow


I want the file 'bar.txt' and it's parenting folders up until 'big' to be copied into the directory 'cow' without the other files in 'big' or 'foo'. The end result should look like:



D:cowbigfoobar.txt


'big' should only contain 'foo' and 'foo' should only contain 'bar.txt'. The program allows me to send the file as a parameter to a file of my choice, in this case a batch file.



I have gotten stuck at the for loop in the ':main" subroutine. It will only print the whole path of the selected file and ignores the delimiter. 'countA' is returned as '1' after the loop and it would have printed the whole path minus drive letter. I do not understand why the for loop is ignoring the delimiter and not separating the path into separate folder names. The reason I am tying to do this is so the user can choose at which point the parented folders should be copied over. This is my first time writing actual code in batch so I still don't completely understand 'setlocal' and a few other things.



I have tried changing the tokens and I am able to choose the individual sections in between the slashes, but I am never able to capture the entire string as separate chunks. I have also changed the delimiter and got the same issue.



@echo off
setlocal
set _target=""
set _dest=""
if not exist %1 goto:error_no_path
set _target=%~pnx1
echo %_target%
goto :dest_selct

:dest_selct
echo select destination folder
pause
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose destination folder.',0,0).self.path""

for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"

setlocal enabledelayedexpansion
echo You chose %folder%
choice /c ync /n /m "Is this correct? ([Y]es, [N]o, [C]ancel)"
if %ERRORLEVEL% EQU 3 goto:eof
if %ERRORLEVEL% EQU 2 goto:dest_selct
if %ERRORLEVEL% EQU 1 (set _dest="%folder%")&&(goto:main)

:error_no_path
ECHO No file path/name
pause
GOTO:eof

:main
echo main
set _countA=0
echo count starts at %_countA%
for /f "tokens=* delims=" %%F in ("%_target%") do (
echo token %_countA% is %%F
echo count is %_countA%
set var!_countA!=%%F
set /a countA+=1
)
echo var1: %var1%
echo count is now %countA%
pause


I should have more than one variable at the end, each being the name of a folder that parents the target file and the count should be however many parent folders there are, plus the file itself. What I am actually getting is one variable and it contains the whole target path still.










share|improve this question


















  • 1





    With setlocal enabledelayedexpansion variables are local for your batch file, so at the end you should also echo !var1!"instead of "global" %var1% Maybe that helps?

    – Stacking For Heap
    Jan 4 at 7:01











  • @StackingForHeap The ! are only required inside a code block, there the %var1% would be evaluated at parse time.

    – LotPings
    Jan 4 at 9:39














0












0








0








I am using a program called "Easy Context Menu" which allows me to create customized context menus when you right click in File Explorer. I wanted to create my own Context Menu item that allows me to copy a file and it's parenting folder structure into another directory, excluding all of the files in the parented folders.



Example:



target file path: C:thebigfoobar.txt

destination path: D:cow


I want the file 'bar.txt' and it's parenting folders up until 'big' to be copied into the directory 'cow' without the other files in 'big' or 'foo'. The end result should look like:



D:cowbigfoobar.txt


'big' should only contain 'foo' and 'foo' should only contain 'bar.txt'. The program allows me to send the file as a parameter to a file of my choice, in this case a batch file.



I have gotten stuck at the for loop in the ':main" subroutine. It will only print the whole path of the selected file and ignores the delimiter. 'countA' is returned as '1' after the loop and it would have printed the whole path minus drive letter. I do not understand why the for loop is ignoring the delimiter and not separating the path into separate folder names. The reason I am tying to do this is so the user can choose at which point the parented folders should be copied over. This is my first time writing actual code in batch so I still don't completely understand 'setlocal' and a few other things.



I have tried changing the tokens and I am able to choose the individual sections in between the slashes, but I am never able to capture the entire string as separate chunks. I have also changed the delimiter and got the same issue.



@echo off
setlocal
set _target=""
set _dest=""
if not exist %1 goto:error_no_path
set _target=%~pnx1
echo %_target%
goto :dest_selct

:dest_selct
echo select destination folder
pause
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose destination folder.',0,0).self.path""

for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"

setlocal enabledelayedexpansion
echo You chose %folder%
choice /c ync /n /m "Is this correct? ([Y]es, [N]o, [C]ancel)"
if %ERRORLEVEL% EQU 3 goto:eof
if %ERRORLEVEL% EQU 2 goto:dest_selct
if %ERRORLEVEL% EQU 1 (set _dest="%folder%")&&(goto:main)

:error_no_path
ECHO No file path/name
pause
GOTO:eof

:main
echo main
set _countA=0
echo count starts at %_countA%
for /f "tokens=* delims=" %%F in ("%_target%") do (
echo token %_countA% is %%F
echo count is %_countA%
set var!_countA!=%%F
set /a countA+=1
)
echo var1: %var1%
echo count is now %countA%
pause


I should have more than one variable at the end, each being the name of a folder that parents the target file and the count should be however many parent folders there are, plus the file itself. What I am actually getting is one variable and it contains the whole target path still.










share|improve this question














I am using a program called "Easy Context Menu" which allows me to create customized context menus when you right click in File Explorer. I wanted to create my own Context Menu item that allows me to copy a file and it's parenting folder structure into another directory, excluding all of the files in the parented folders.



Example:



target file path: C:thebigfoobar.txt

destination path: D:cow


I want the file 'bar.txt' and it's parenting folders up until 'big' to be copied into the directory 'cow' without the other files in 'big' or 'foo'. The end result should look like:



D:cowbigfoobar.txt


'big' should only contain 'foo' and 'foo' should only contain 'bar.txt'. The program allows me to send the file as a parameter to a file of my choice, in this case a batch file.



I have gotten stuck at the for loop in the ':main" subroutine. It will only print the whole path of the selected file and ignores the delimiter. 'countA' is returned as '1' after the loop and it would have printed the whole path minus drive letter. I do not understand why the for loop is ignoring the delimiter and not separating the path into separate folder names. The reason I am tying to do this is so the user can choose at which point the parented folders should be copied over. This is my first time writing actual code in batch so I still don't completely understand 'setlocal' and a few other things.



I have tried changing the tokens and I am able to choose the individual sections in between the slashes, but I am never able to capture the entire string as separate chunks. I have also changed the delimiter and got the same issue.



@echo off
setlocal
set _target=""
set _dest=""
if not exist %1 goto:error_no_path
set _target=%~pnx1
echo %_target%
goto :dest_selct

:dest_selct
echo select destination folder
pause
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose destination folder.',0,0).self.path""

for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"

setlocal enabledelayedexpansion
echo You chose %folder%
choice /c ync /n /m "Is this correct? ([Y]es, [N]o, [C]ancel)"
if %ERRORLEVEL% EQU 3 goto:eof
if %ERRORLEVEL% EQU 2 goto:dest_selct
if %ERRORLEVEL% EQU 1 (set _dest="%folder%")&&(goto:main)

:error_no_path
ECHO No file path/name
pause
GOTO:eof

:main
echo main
set _countA=0
echo count starts at %_countA%
for /f "tokens=* delims=" %%F in ("%_target%") do (
echo token %_countA% is %%F
echo count is %_countA%
set var!_countA!=%%F
set /a countA+=1
)
echo var1: %var1%
echo count is now %countA%
pause


I should have more than one variable at the end, each being the name of a folder that parents the target file and the count should be however many parent folders there are, plus the file itself. What I am actually getting is one variable and it contains the whole target path still.







batch-file for-loop token






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 4 at 6:26









Raptor JessusRaptor Jessus

12




12








  • 1





    With setlocal enabledelayedexpansion variables are local for your batch file, so at the end you should also echo !var1!"instead of "global" %var1% Maybe that helps?

    – Stacking For Heap
    Jan 4 at 7:01











  • @StackingForHeap The ! are only required inside a code block, there the %var1% would be evaluated at parse time.

    – LotPings
    Jan 4 at 9:39














  • 1





    With setlocal enabledelayedexpansion variables are local for your batch file, so at the end you should also echo !var1!"instead of "global" %var1% Maybe that helps?

    – Stacking For Heap
    Jan 4 at 7:01











  • @StackingForHeap The ! are only required inside a code block, there the %var1% would be evaluated at parse time.

    – LotPings
    Jan 4 at 9:39








1




1





With setlocal enabledelayedexpansion variables are local for your batch file, so at the end you should also echo !var1!"instead of "global" %var1% Maybe that helps?

– Stacking For Heap
Jan 4 at 7:01





With setlocal enabledelayedexpansion variables are local for your batch file, so at the end you should also echo !var1!"instead of "global" %var1% Maybe that helps?

– Stacking For Heap
Jan 4 at 7:01













@StackingForHeap The ! are only required inside a code block, there the %var1% would be evaluated at parse time.

– LotPings
Jan 4 at 9:39





@StackingForHeap The ! are only required inside a code block, there the %var1% would be evaluated at parse time.

– LotPings
Jan 4 at 9:39












1 Answer
1






active

oldest

votes


















1














The for loop does not delimit as you have tokens=*,
which gets all tokens as one token. Leading delimiters are stripped.
Only var0 has a value, not the echoed var1.



To split _target by path segments, use:



for %%F in ("%_target:=" "%") do echo %%~F


Another issue you have is if you choose n at the choice prompt,
the goto:dest_selct will cause setlocal enabledelayedexpansion
to execute again. Since you are not using endlocal, then the
local environment is recursing without ending. Sometimes endlocal
is implied, so you may not need to use it, though not in this case.



Try this fix:



@echo off
setlocal
set "_target="
set "_dest="
if not exist "%~1" goto:error_no_path
set "_target=%~pnx1"
echo %_target%
goto :dest_selct

:dest_selct
echo select destination folder
pause
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose destination folder.',0,0).self.path""

for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"

echo You chose %folder%
choice /c ync /n /m "Is this correct? ([Y]es, [N]o, [C]ancel)"
if %ERRORLEVEL% EQU 3 goto:eof
if %ERRORLEVEL% EQU 2 goto:dest_selct
if %ERRORLEVEL% EQU 1 (set "_dest=%folder%") && goto:main

:error_no_path
ECHO No file pathname
pause
GOTO:eof

:main
setlocal enabledelayedexpansion
echo main
set "countA=0"
echo count starts at %countA%

rem Remove leading backslash if exist.
if "%_target:~,1%" == "" set "_target=%_target:~1%"

rem Split into path segments.
for %%F in ("%_target:=" "%") do (
echo token !countA! is %%~F
echo count is !countA!
set "var!countA!=%%~F"
set /a countA+=1
)

echo var1: %var1%

rem Echo all variables starting with var and their values.
set var
echo count is now %countA%
endlocal
pause



  • Adjusted double quoting.

  • Changed instances of variable name of _countA to countA to match.


  • set var to show all variables starting with var for debugging.


  • for loop now spilts on path segments.

  • Moved setlocal enabledelayedexpansion out of label loop.

  • Fix 1st argument check if not defined.


Refer to set /? about variable substitution i.e. "%_target:=" "%", which replaces with " ".






share|improve this answer


























  • All issues well pointed out (+1)

    – LotPings
    Jan 4 at 9:43












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54034003%2fbatch-for-loop-not-separating-string-at-specified-delimiter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The for loop does not delimit as you have tokens=*,
which gets all tokens as one token. Leading delimiters are stripped.
Only var0 has a value, not the echoed var1.



To split _target by path segments, use:



for %%F in ("%_target:=" "%") do echo %%~F


Another issue you have is if you choose n at the choice prompt,
the goto:dest_selct will cause setlocal enabledelayedexpansion
to execute again. Since you are not using endlocal, then the
local environment is recursing without ending. Sometimes endlocal
is implied, so you may not need to use it, though not in this case.



Try this fix:



@echo off
setlocal
set "_target="
set "_dest="
if not exist "%~1" goto:error_no_path
set "_target=%~pnx1"
echo %_target%
goto :dest_selct

:dest_selct
echo select destination folder
pause
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose destination folder.',0,0).self.path""

for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"

echo You chose %folder%
choice /c ync /n /m "Is this correct? ([Y]es, [N]o, [C]ancel)"
if %ERRORLEVEL% EQU 3 goto:eof
if %ERRORLEVEL% EQU 2 goto:dest_selct
if %ERRORLEVEL% EQU 1 (set "_dest=%folder%") && goto:main

:error_no_path
ECHO No file pathname
pause
GOTO:eof

:main
setlocal enabledelayedexpansion
echo main
set "countA=0"
echo count starts at %countA%

rem Remove leading backslash if exist.
if "%_target:~,1%" == "" set "_target=%_target:~1%"

rem Split into path segments.
for %%F in ("%_target:=" "%") do (
echo token !countA! is %%~F
echo count is !countA!
set "var!countA!=%%~F"
set /a countA+=1
)

echo var1: %var1%

rem Echo all variables starting with var and their values.
set var
echo count is now %countA%
endlocal
pause



  • Adjusted double quoting.

  • Changed instances of variable name of _countA to countA to match.


  • set var to show all variables starting with var for debugging.


  • for loop now spilts on path segments.

  • Moved setlocal enabledelayedexpansion out of label loop.

  • Fix 1st argument check if not defined.


Refer to set /? about variable substitution i.e. "%_target:=" "%", which replaces with " ".






share|improve this answer


























  • All issues well pointed out (+1)

    – LotPings
    Jan 4 at 9:43
















1














The for loop does not delimit as you have tokens=*,
which gets all tokens as one token. Leading delimiters are stripped.
Only var0 has a value, not the echoed var1.



To split _target by path segments, use:



for %%F in ("%_target:=" "%") do echo %%~F


Another issue you have is if you choose n at the choice prompt,
the goto:dest_selct will cause setlocal enabledelayedexpansion
to execute again. Since you are not using endlocal, then the
local environment is recursing without ending. Sometimes endlocal
is implied, so you may not need to use it, though not in this case.



Try this fix:



@echo off
setlocal
set "_target="
set "_dest="
if not exist "%~1" goto:error_no_path
set "_target=%~pnx1"
echo %_target%
goto :dest_selct

:dest_selct
echo select destination folder
pause
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose destination folder.',0,0).self.path""

for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"

echo You chose %folder%
choice /c ync /n /m "Is this correct? ([Y]es, [N]o, [C]ancel)"
if %ERRORLEVEL% EQU 3 goto:eof
if %ERRORLEVEL% EQU 2 goto:dest_selct
if %ERRORLEVEL% EQU 1 (set "_dest=%folder%") && goto:main

:error_no_path
ECHO No file pathname
pause
GOTO:eof

:main
setlocal enabledelayedexpansion
echo main
set "countA=0"
echo count starts at %countA%

rem Remove leading backslash if exist.
if "%_target:~,1%" == "" set "_target=%_target:~1%"

rem Split into path segments.
for %%F in ("%_target:=" "%") do (
echo token !countA! is %%~F
echo count is !countA!
set "var!countA!=%%~F"
set /a countA+=1
)

echo var1: %var1%

rem Echo all variables starting with var and their values.
set var
echo count is now %countA%
endlocal
pause



  • Adjusted double quoting.

  • Changed instances of variable name of _countA to countA to match.


  • set var to show all variables starting with var for debugging.


  • for loop now spilts on path segments.

  • Moved setlocal enabledelayedexpansion out of label loop.

  • Fix 1st argument check if not defined.


Refer to set /? about variable substitution i.e. "%_target:=" "%", which replaces with " ".






share|improve this answer


























  • All issues well pointed out (+1)

    – LotPings
    Jan 4 at 9:43














1












1








1







The for loop does not delimit as you have tokens=*,
which gets all tokens as one token. Leading delimiters are stripped.
Only var0 has a value, not the echoed var1.



To split _target by path segments, use:



for %%F in ("%_target:=" "%") do echo %%~F


Another issue you have is if you choose n at the choice prompt,
the goto:dest_selct will cause setlocal enabledelayedexpansion
to execute again. Since you are not using endlocal, then the
local environment is recursing without ending. Sometimes endlocal
is implied, so you may not need to use it, though not in this case.



Try this fix:



@echo off
setlocal
set "_target="
set "_dest="
if not exist "%~1" goto:error_no_path
set "_target=%~pnx1"
echo %_target%
goto :dest_selct

:dest_selct
echo select destination folder
pause
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose destination folder.',0,0).self.path""

for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"

echo You chose %folder%
choice /c ync /n /m "Is this correct? ([Y]es, [N]o, [C]ancel)"
if %ERRORLEVEL% EQU 3 goto:eof
if %ERRORLEVEL% EQU 2 goto:dest_selct
if %ERRORLEVEL% EQU 1 (set "_dest=%folder%") && goto:main

:error_no_path
ECHO No file pathname
pause
GOTO:eof

:main
setlocal enabledelayedexpansion
echo main
set "countA=0"
echo count starts at %countA%

rem Remove leading backslash if exist.
if "%_target:~,1%" == "" set "_target=%_target:~1%"

rem Split into path segments.
for %%F in ("%_target:=" "%") do (
echo token !countA! is %%~F
echo count is !countA!
set "var!countA!=%%~F"
set /a countA+=1
)

echo var1: %var1%

rem Echo all variables starting with var and their values.
set var
echo count is now %countA%
endlocal
pause



  • Adjusted double quoting.

  • Changed instances of variable name of _countA to countA to match.


  • set var to show all variables starting with var for debugging.


  • for loop now spilts on path segments.

  • Moved setlocal enabledelayedexpansion out of label loop.

  • Fix 1st argument check if not defined.


Refer to set /? about variable substitution i.e. "%_target:=" "%", which replaces with " ".






share|improve this answer















The for loop does not delimit as you have tokens=*,
which gets all tokens as one token. Leading delimiters are stripped.
Only var0 has a value, not the echoed var1.



To split _target by path segments, use:



for %%F in ("%_target:=" "%") do echo %%~F


Another issue you have is if you choose n at the choice prompt,
the goto:dest_selct will cause setlocal enabledelayedexpansion
to execute again. Since you are not using endlocal, then the
local environment is recursing without ending. Sometimes endlocal
is implied, so you may not need to use it, though not in this case.



Try this fix:



@echo off
setlocal
set "_target="
set "_dest="
if not exist "%~1" goto:error_no_path
set "_target=%~pnx1"
echo %_target%
goto :dest_selct

:dest_selct
echo select destination folder
pause
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose destination folder.',0,0).self.path""

for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"

echo You chose %folder%
choice /c ync /n /m "Is this correct? ([Y]es, [N]o, [C]ancel)"
if %ERRORLEVEL% EQU 3 goto:eof
if %ERRORLEVEL% EQU 2 goto:dest_selct
if %ERRORLEVEL% EQU 1 (set "_dest=%folder%") && goto:main

:error_no_path
ECHO No file pathname
pause
GOTO:eof

:main
setlocal enabledelayedexpansion
echo main
set "countA=0"
echo count starts at %countA%

rem Remove leading backslash if exist.
if "%_target:~,1%" == "" set "_target=%_target:~1%"

rem Split into path segments.
for %%F in ("%_target:=" "%") do (
echo token !countA! is %%~F
echo count is !countA!
set "var!countA!=%%~F"
set /a countA+=1
)

echo var1: %var1%

rem Echo all variables starting with var and their values.
set var
echo count is now %countA%
endlocal
pause



  • Adjusted double quoting.

  • Changed instances of variable name of _countA to countA to match.


  • set var to show all variables starting with var for debugging.


  • for loop now spilts on path segments.

  • Moved setlocal enabledelayedexpansion out of label loop.

  • Fix 1st argument check if not defined.


Refer to set /? about variable substitution i.e. "%_target:=" "%", which replaces with " ".







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 4 at 9:48

























answered Jan 4 at 9:40









michael_heathmichael_heath

3,1122719




3,1122719













  • All issues well pointed out (+1)

    – LotPings
    Jan 4 at 9:43



















  • All issues well pointed out (+1)

    – LotPings
    Jan 4 at 9:43

















All issues well pointed out (+1)

– LotPings
Jan 4 at 9:43





All issues well pointed out (+1)

– LotPings
Jan 4 at 9:43




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54034003%2fbatch-for-loop-not-separating-string-at-specified-delimiter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas