batch code not working when put inside a loop
This code works and picks a random file but when I put it inside the outer loop, I get empty "" instead.
rem scrambler
setlocal EnableDelayedExpansion
@echo off
cd j:target
rem for /R %%t in (*.mp3) do (
REM echo ********************
REM echo T folder is %%~dpt
cd j:source
set n=0
for /R %%f in (*.mp3) do (
set /A n+=1
set "file[!n!]=%%f"
)
set /A "rand=(n*%random%)/32768+1"
rem copy "!file[%rand%]!" j:target
echo "!file[%rand%]!"
cd j:target
REM copy "!file[%rand%]!" %%~dpt
REM move "!file[%rand%]!" j:old
rem )
PS. What I am trying to do is: identical source and target folders with mp3 files in them. Then I loop through the target files and for each target file, I overwrite them with a random mp3 file from the source folder. I (re)move the file from source and randomly pick another source file for the next target file, so I exhaust all files without any duplicates. In the end the target has same files and structure as source but they are now scrambled, that is if I can achieve it. I know the array takes some time so I want to optimize it too.
windows command-line batch-file
New contributor
add a comment |
This code works and picks a random file but when I put it inside the outer loop, I get empty "" instead.
rem scrambler
setlocal EnableDelayedExpansion
@echo off
cd j:target
rem for /R %%t in (*.mp3) do (
REM echo ********************
REM echo T folder is %%~dpt
cd j:source
set n=0
for /R %%f in (*.mp3) do (
set /A n+=1
set "file[!n!]=%%f"
)
set /A "rand=(n*%random%)/32768+1"
rem copy "!file[%rand%]!" j:target
echo "!file[%rand%]!"
cd j:target
REM copy "!file[%rand%]!" %%~dpt
REM move "!file[%rand%]!" j:old
rem )
PS. What I am trying to do is: identical source and target folders with mp3 files in them. Then I loop through the target files and for each target file, I overwrite them with a random mp3 file from the source folder. I (re)move the file from source and randomly pick another source file for the next target file, so I exhaust all files without any duplicates. In the end the target has same files and structure as source but they are now scrambled, that is if I can achieve it. I know the array takes some time so I want to optimize it too.
windows command-line batch-file
New contributor
2
Shouldn't those %rand% be !rand! in stead? And can you run it with ECHO ON to see exactly when the error message happens? Your statement 'I get empty ""' instead' isn't very clear about where in the loop(s) that happens.
– Tonny
Jan 6 at 12:37
I tried that, the result is now justrand
as a string. It is working with the code above already, printing the random file name. Thanks for the ECHO ON tip. The error is echo"!file[%rand%]!"
here. If you enable the outer looprem for /R %%t in (*.mp3) do (
, you can see it. I suspect the EnableDelayedExpansion part, I should put it somewhere else I guess.
– vtastek
Jan 6 at 13:12
add a comment |
This code works and picks a random file but when I put it inside the outer loop, I get empty "" instead.
rem scrambler
setlocal EnableDelayedExpansion
@echo off
cd j:target
rem for /R %%t in (*.mp3) do (
REM echo ********************
REM echo T folder is %%~dpt
cd j:source
set n=0
for /R %%f in (*.mp3) do (
set /A n+=1
set "file[!n!]=%%f"
)
set /A "rand=(n*%random%)/32768+1"
rem copy "!file[%rand%]!" j:target
echo "!file[%rand%]!"
cd j:target
REM copy "!file[%rand%]!" %%~dpt
REM move "!file[%rand%]!" j:old
rem )
PS. What I am trying to do is: identical source and target folders with mp3 files in them. Then I loop through the target files and for each target file, I overwrite them with a random mp3 file from the source folder. I (re)move the file from source and randomly pick another source file for the next target file, so I exhaust all files without any duplicates. In the end the target has same files and structure as source but they are now scrambled, that is if I can achieve it. I know the array takes some time so I want to optimize it too.
windows command-line batch-file
New contributor
This code works and picks a random file but when I put it inside the outer loop, I get empty "" instead.
rem scrambler
setlocal EnableDelayedExpansion
@echo off
cd j:target
rem for /R %%t in (*.mp3) do (
REM echo ********************
REM echo T folder is %%~dpt
cd j:source
set n=0
for /R %%f in (*.mp3) do (
set /A n+=1
set "file[!n!]=%%f"
)
set /A "rand=(n*%random%)/32768+1"
rem copy "!file[%rand%]!" j:target
echo "!file[%rand%]!"
cd j:target
REM copy "!file[%rand%]!" %%~dpt
REM move "!file[%rand%]!" j:old
rem )
PS. What I am trying to do is: identical source and target folders with mp3 files in them. Then I loop through the target files and for each target file, I overwrite them with a random mp3 file from the source folder. I (re)move the file from source and randomly pick another source file for the next target file, so I exhaust all files without any duplicates. In the end the target has same files and structure as source but they are now scrambled, that is if I can achieve it. I know the array takes some time so I want to optimize it too.
windows command-line batch-file
windows command-line batch-file
New contributor
New contributor
edited Jan 6 at 18:39
vtastek
New contributor
asked Jan 6 at 12:21
vtastekvtastek
33
33
New contributor
New contributor
2
Shouldn't those %rand% be !rand! in stead? And can you run it with ECHO ON to see exactly when the error message happens? Your statement 'I get empty ""' instead' isn't very clear about where in the loop(s) that happens.
– Tonny
Jan 6 at 12:37
I tried that, the result is now justrand
as a string. It is working with the code above already, printing the random file name. Thanks for the ECHO ON tip. The error is echo"!file[%rand%]!"
here. If you enable the outer looprem for /R %%t in (*.mp3) do (
, you can see it. I suspect the EnableDelayedExpansion part, I should put it somewhere else I guess.
– vtastek
Jan 6 at 13:12
add a comment |
2
Shouldn't those %rand% be !rand! in stead? And can you run it with ECHO ON to see exactly when the error message happens? Your statement 'I get empty ""' instead' isn't very clear about where in the loop(s) that happens.
– Tonny
Jan 6 at 12:37
I tried that, the result is now justrand
as a string. It is working with the code above already, printing the random file name. Thanks for the ECHO ON tip. The error is echo"!file[%rand%]!"
here. If you enable the outer looprem for /R %%t in (*.mp3) do (
, you can see it. I suspect the EnableDelayedExpansion part, I should put it somewhere else I guess.
– vtastek
Jan 6 at 13:12
2
2
Shouldn't those %rand% be !rand! in stead? And can you run it with ECHO ON to see exactly when the error message happens? Your statement 'I get empty ""' instead' isn't very clear about where in the loop(s) that happens.
– Tonny
Jan 6 at 12:37
Shouldn't those %rand% be !rand! in stead? And can you run it with ECHO ON to see exactly when the error message happens? Your statement 'I get empty ""' instead' isn't very clear about where in the loop(s) that happens.
– Tonny
Jan 6 at 12:37
I tried that, the result is now just
rand
as a string. It is working with the code above already, printing the random file name. Thanks for the ECHO ON tip. The error is echo "!file[%rand%]!"
here. If you enable the outer loop rem for /R %%t in (*.mp3) do (
, you can see it. I suspect the EnableDelayedExpansion part, I should put it somewhere else I guess.– vtastek
Jan 6 at 13:12
I tried that, the result is now just
rand
as a string. It is working with the code above already, printing the random file name. Thanks for the ECHO ON tip. The error is echo "!file[%rand%]!"
here. If you enable the outer loop rem for /R %%t in (*.mp3) do (
, you can see it. I suspect the EnableDelayedExpansion part, I should put it somewhere else I guess.– vtastek
Jan 6 at 13:12
add a comment |
1 Answer
1
active
oldest
votes
There are several flaws within your code
- with nested for loops you'd need two levels of delayed expansion (what is possible)
- For every iteration of the outer
for /r
you rebuild/overwrite the very samefile
array from source files. - if you build the array first there is no need to constantly switch between source and target (the folder is stored in the array anyway)
- despite the name scrambler it's unclear what you want to achieve with two copy and one move, please elaborate.
:: Q:Test201916SU_1391138.cmd
rem scrambler
@echo off
setlocal EnableDelayedExpansion
cd j:source
set n=0
for /R %%f in (*.mp3) do (
set /A n+=1
set "file[!n!]=%%f"
)
cd j:target
for /R %%t in (*.mp3) do (
set /A "rand=(n*!random!)/32768+1,m=n,n-=1"
rem following line demonstrates double delayedexpansion with a pseudo call
call set "file=%%file[!rand!]%%"
call set "file[!rand!]=%%file[!m!]%%"
echo copy /B /Y "!file!" "%%t"
copy /B /Y "!file!" "%%t"
)
Thanks for the answer, I will try it and see how it goes. What I am trying to do is: identical source and target folders with mp3 files in them. Then I loop through the target files and for each target file, I overwrite them with a random mp3 file from the source folder. I (re)move the file from source and randomly pick another source file for the next target file, so I exhaust all files without any duplicates. In the end the target has same files and structure as source but they are now scrambled, that is if I can achieve it. I know the array takes some time so I want to optimize it too.
– vtastek
Jan 6 at 18:04
Better edit your question to contain that explanation. Are the number of files in source and target identical? You could then use only the array, replace the file[random] entry with the last onefile[n]
and decrementn
.
– LotPings
Jan 6 at 18:26
Done. Yes they are identical. There are wav files too but they shouldn't interfere. Thanks again for the tip.
– vtastek
Jan 6 at 18:44
Try changed (and untested) answer, may already do what you want.
– LotPings
Jan 6 at 18:45
Thank you very much. It is working, I can even rerun it which is a good feature.
– vtastek
Jan 6 at 18:58
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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
});
}
});
vtastek is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1391138%2fbatch-code-not-working-when-put-inside-a-loop%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
There are several flaws within your code
- with nested for loops you'd need two levels of delayed expansion (what is possible)
- For every iteration of the outer
for /r
you rebuild/overwrite the very samefile
array from source files. - if you build the array first there is no need to constantly switch between source and target (the folder is stored in the array anyway)
- despite the name scrambler it's unclear what you want to achieve with two copy and one move, please elaborate.
:: Q:Test201916SU_1391138.cmd
rem scrambler
@echo off
setlocal EnableDelayedExpansion
cd j:source
set n=0
for /R %%f in (*.mp3) do (
set /A n+=1
set "file[!n!]=%%f"
)
cd j:target
for /R %%t in (*.mp3) do (
set /A "rand=(n*!random!)/32768+1,m=n,n-=1"
rem following line demonstrates double delayedexpansion with a pseudo call
call set "file=%%file[!rand!]%%"
call set "file[!rand!]=%%file[!m!]%%"
echo copy /B /Y "!file!" "%%t"
copy /B /Y "!file!" "%%t"
)
Thanks for the answer, I will try it and see how it goes. What I am trying to do is: identical source and target folders with mp3 files in them. Then I loop through the target files and for each target file, I overwrite them with a random mp3 file from the source folder. I (re)move the file from source and randomly pick another source file for the next target file, so I exhaust all files without any duplicates. In the end the target has same files and structure as source but they are now scrambled, that is if I can achieve it. I know the array takes some time so I want to optimize it too.
– vtastek
Jan 6 at 18:04
Better edit your question to contain that explanation. Are the number of files in source and target identical? You could then use only the array, replace the file[random] entry with the last onefile[n]
and decrementn
.
– LotPings
Jan 6 at 18:26
Done. Yes they are identical. There are wav files too but they shouldn't interfere. Thanks again for the tip.
– vtastek
Jan 6 at 18:44
Try changed (and untested) answer, may already do what you want.
– LotPings
Jan 6 at 18:45
Thank you very much. It is working, I can even rerun it which is a good feature.
– vtastek
Jan 6 at 18:58
add a comment |
There are several flaws within your code
- with nested for loops you'd need two levels of delayed expansion (what is possible)
- For every iteration of the outer
for /r
you rebuild/overwrite the very samefile
array from source files. - if you build the array first there is no need to constantly switch between source and target (the folder is stored in the array anyway)
- despite the name scrambler it's unclear what you want to achieve with two copy and one move, please elaborate.
:: Q:Test201916SU_1391138.cmd
rem scrambler
@echo off
setlocal EnableDelayedExpansion
cd j:source
set n=0
for /R %%f in (*.mp3) do (
set /A n+=1
set "file[!n!]=%%f"
)
cd j:target
for /R %%t in (*.mp3) do (
set /A "rand=(n*!random!)/32768+1,m=n,n-=1"
rem following line demonstrates double delayedexpansion with a pseudo call
call set "file=%%file[!rand!]%%"
call set "file[!rand!]=%%file[!m!]%%"
echo copy /B /Y "!file!" "%%t"
copy /B /Y "!file!" "%%t"
)
Thanks for the answer, I will try it and see how it goes. What I am trying to do is: identical source and target folders with mp3 files in them. Then I loop through the target files and for each target file, I overwrite them with a random mp3 file from the source folder. I (re)move the file from source and randomly pick another source file for the next target file, so I exhaust all files without any duplicates. In the end the target has same files and structure as source but they are now scrambled, that is if I can achieve it. I know the array takes some time so I want to optimize it too.
– vtastek
Jan 6 at 18:04
Better edit your question to contain that explanation. Are the number of files in source and target identical? You could then use only the array, replace the file[random] entry with the last onefile[n]
and decrementn
.
– LotPings
Jan 6 at 18:26
Done. Yes they are identical. There are wav files too but they shouldn't interfere. Thanks again for the tip.
– vtastek
Jan 6 at 18:44
Try changed (and untested) answer, may already do what you want.
– LotPings
Jan 6 at 18:45
Thank you very much. It is working, I can even rerun it which is a good feature.
– vtastek
Jan 6 at 18:58
add a comment |
There are several flaws within your code
- with nested for loops you'd need two levels of delayed expansion (what is possible)
- For every iteration of the outer
for /r
you rebuild/overwrite the very samefile
array from source files. - if you build the array first there is no need to constantly switch between source and target (the folder is stored in the array anyway)
- despite the name scrambler it's unclear what you want to achieve with two copy and one move, please elaborate.
:: Q:Test201916SU_1391138.cmd
rem scrambler
@echo off
setlocal EnableDelayedExpansion
cd j:source
set n=0
for /R %%f in (*.mp3) do (
set /A n+=1
set "file[!n!]=%%f"
)
cd j:target
for /R %%t in (*.mp3) do (
set /A "rand=(n*!random!)/32768+1,m=n,n-=1"
rem following line demonstrates double delayedexpansion with a pseudo call
call set "file=%%file[!rand!]%%"
call set "file[!rand!]=%%file[!m!]%%"
echo copy /B /Y "!file!" "%%t"
copy /B /Y "!file!" "%%t"
)
There are several flaws within your code
- with nested for loops you'd need two levels of delayed expansion (what is possible)
- For every iteration of the outer
for /r
you rebuild/overwrite the very samefile
array from source files. - if you build the array first there is no need to constantly switch between source and target (the folder is stored in the array anyway)
- despite the name scrambler it's unclear what you want to achieve with two copy and one move, please elaborate.
:: Q:Test201916SU_1391138.cmd
rem scrambler
@echo off
setlocal EnableDelayedExpansion
cd j:source
set n=0
for /R %%f in (*.mp3) do (
set /A n+=1
set "file[!n!]=%%f"
)
cd j:target
for /R %%t in (*.mp3) do (
set /A "rand=(n*!random!)/32768+1,m=n,n-=1"
rem following line demonstrates double delayedexpansion with a pseudo call
call set "file=%%file[!rand!]%%"
call set "file[!rand!]=%%file[!m!]%%"
echo copy /B /Y "!file!" "%%t"
copy /B /Y "!file!" "%%t"
)
edited Jan 6 at 18:44
answered Jan 6 at 17:41
LotPingsLotPings
4,7061722
4,7061722
Thanks for the answer, I will try it and see how it goes. What I am trying to do is: identical source and target folders with mp3 files in them. Then I loop through the target files and for each target file, I overwrite them with a random mp3 file from the source folder. I (re)move the file from source and randomly pick another source file for the next target file, so I exhaust all files without any duplicates. In the end the target has same files and structure as source but they are now scrambled, that is if I can achieve it. I know the array takes some time so I want to optimize it too.
– vtastek
Jan 6 at 18:04
Better edit your question to contain that explanation. Are the number of files in source and target identical? You could then use only the array, replace the file[random] entry with the last onefile[n]
and decrementn
.
– LotPings
Jan 6 at 18:26
Done. Yes they are identical. There are wav files too but they shouldn't interfere. Thanks again for the tip.
– vtastek
Jan 6 at 18:44
Try changed (and untested) answer, may already do what you want.
– LotPings
Jan 6 at 18:45
Thank you very much. It is working, I can even rerun it which is a good feature.
– vtastek
Jan 6 at 18:58
add a comment |
Thanks for the answer, I will try it and see how it goes. What I am trying to do is: identical source and target folders with mp3 files in them. Then I loop through the target files and for each target file, I overwrite them with a random mp3 file from the source folder. I (re)move the file from source and randomly pick another source file for the next target file, so I exhaust all files without any duplicates. In the end the target has same files and structure as source but they are now scrambled, that is if I can achieve it. I know the array takes some time so I want to optimize it too.
– vtastek
Jan 6 at 18:04
Better edit your question to contain that explanation. Are the number of files in source and target identical? You could then use only the array, replace the file[random] entry with the last onefile[n]
and decrementn
.
– LotPings
Jan 6 at 18:26
Done. Yes they are identical. There are wav files too but they shouldn't interfere. Thanks again for the tip.
– vtastek
Jan 6 at 18:44
Try changed (and untested) answer, may already do what you want.
– LotPings
Jan 6 at 18:45
Thank you very much. It is working, I can even rerun it which is a good feature.
– vtastek
Jan 6 at 18:58
Thanks for the answer, I will try it and see how it goes. What I am trying to do is: identical source and target folders with mp3 files in them. Then I loop through the target files and for each target file, I overwrite them with a random mp3 file from the source folder. I (re)move the file from source and randomly pick another source file for the next target file, so I exhaust all files without any duplicates. In the end the target has same files and structure as source but they are now scrambled, that is if I can achieve it. I know the array takes some time so I want to optimize it too.
– vtastek
Jan 6 at 18:04
Thanks for the answer, I will try it and see how it goes. What I am trying to do is: identical source and target folders with mp3 files in them. Then I loop through the target files and for each target file, I overwrite them with a random mp3 file from the source folder. I (re)move the file from source and randomly pick another source file for the next target file, so I exhaust all files without any duplicates. In the end the target has same files and structure as source but they are now scrambled, that is if I can achieve it. I know the array takes some time so I want to optimize it too.
– vtastek
Jan 6 at 18:04
Better edit your question to contain that explanation. Are the number of files in source and target identical? You could then use only the array, replace the file[random] entry with the last one
file[n]
and decrement n
.– LotPings
Jan 6 at 18:26
Better edit your question to contain that explanation. Are the number of files in source and target identical? You could then use only the array, replace the file[random] entry with the last one
file[n]
and decrement n
.– LotPings
Jan 6 at 18:26
Done. Yes they are identical. There are wav files too but they shouldn't interfere. Thanks again for the tip.
– vtastek
Jan 6 at 18:44
Done. Yes they are identical. There are wav files too but they shouldn't interfere. Thanks again for the tip.
– vtastek
Jan 6 at 18:44
Try changed (and untested) answer, may already do what you want.
– LotPings
Jan 6 at 18:45
Try changed (and untested) answer, may already do what you want.
– LotPings
Jan 6 at 18:45
Thank you very much. It is working, I can even rerun it which is a good feature.
– vtastek
Jan 6 at 18:58
Thank you very much. It is working, I can even rerun it which is a good feature.
– vtastek
Jan 6 at 18:58
add a comment |
vtastek is a new contributor. Be nice, and check out our Code of Conduct.
vtastek is a new contributor. Be nice, and check out our Code of Conduct.
vtastek is a new contributor. Be nice, and check out our Code of Conduct.
vtastek is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Super User!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1391138%2fbatch-code-not-working-when-put-inside-a-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
Shouldn't those %rand% be !rand! in stead? And can you run it with ECHO ON to see exactly when the error message happens? Your statement 'I get empty ""' instead' isn't very clear about where in the loop(s) that happens.
– Tonny
Jan 6 at 12:37
I tried that, the result is now just
rand
as a string. It is working with the code above already, printing the random file name. Thanks for the ECHO ON tip. The error is echo"!file[%rand%]!"
here. If you enable the outer looprem for /R %%t in (*.mp3) do (
, you can see it. I suspect the EnableDelayedExpansion part, I should put it somewhere else I guess.– vtastek
Jan 6 at 13:12