How to ensure full path in Windows batch FOR loop












2















I've created a generic batch (or Windows Command) file that lets me loop through the contents of a directory and call a command for each item.



IF a%1==a ( set _DIR="%CD%") ELSE ( set _DIR="%~1")
IF a%2==a ( set _COMMAND=rem) ELSE ( set _COMMAND=%2)
IF a%3==a ( set _FILTER=*.*) ELSE ( set _FILTER=%3)
set _OPTS=%4

FOR /F "delims=" %%f IN ('dir %_DIR%%_FILTER% %_OPTS% /b') DO (
%_COMMAND% "%%f"
)


But, I'm trying to determine how to ensure that I call %_COMMAND% on the correct file.



I've tried pre-pending the directory variable onto the front, like %_COMMAND% %_DIR%"%%f", but this leaves a quotation mark in the parameter I pass. For example, if I call my batch file exec_dir.bat, and call it with the following echo_test.bat, I see that all of the files have a quotation mark when echo_test.bat runs.



echo %~dpn1.mp4


That batch script produces:



> exec_dir.bat "C:UsersUserDesktopTest Folder" echo_test.bat *.txt
C:UsersUserDesktopTest Folder"Test File.txt
C:UsersUserDesktopTest Folder"Test2.txt


My thought is that it has something to do with the as an escape character, but I can't seem to work around it.










share|improve this question























  • i am not sure if any of these ideas will help but you know %1 %~f1 makes it full. Also, dir /b doesn't seem to give the full path, but dir /b /s does, though i guess you don't want /s. another thought is you can cut bits off of an environment variable e.g. echo abc , then echo %a:~1,2% not sure if that's useful to you

    – barlop
    Jan 31 '11 at 7:34


















2















I've created a generic batch (or Windows Command) file that lets me loop through the contents of a directory and call a command for each item.



IF a%1==a ( set _DIR="%CD%") ELSE ( set _DIR="%~1")
IF a%2==a ( set _COMMAND=rem) ELSE ( set _COMMAND=%2)
IF a%3==a ( set _FILTER=*.*) ELSE ( set _FILTER=%3)
set _OPTS=%4

FOR /F "delims=" %%f IN ('dir %_DIR%%_FILTER% %_OPTS% /b') DO (
%_COMMAND% "%%f"
)


But, I'm trying to determine how to ensure that I call %_COMMAND% on the correct file.



I've tried pre-pending the directory variable onto the front, like %_COMMAND% %_DIR%"%%f", but this leaves a quotation mark in the parameter I pass. For example, if I call my batch file exec_dir.bat, and call it with the following echo_test.bat, I see that all of the files have a quotation mark when echo_test.bat runs.



echo %~dpn1.mp4


That batch script produces:



> exec_dir.bat "C:UsersUserDesktopTest Folder" echo_test.bat *.txt
C:UsersUserDesktopTest Folder"Test File.txt
C:UsersUserDesktopTest Folder"Test2.txt


My thought is that it has something to do with the as an escape character, but I can't seem to work around it.










share|improve this question























  • i am not sure if any of these ideas will help but you know %1 %~f1 makes it full. Also, dir /b doesn't seem to give the full path, but dir /b /s does, though i guess you don't want /s. another thought is you can cut bits off of an environment variable e.g. echo abc , then echo %a:~1,2% not sure if that's useful to you

    – barlop
    Jan 31 '11 at 7:34
















2












2








2








I've created a generic batch (or Windows Command) file that lets me loop through the contents of a directory and call a command for each item.



IF a%1==a ( set _DIR="%CD%") ELSE ( set _DIR="%~1")
IF a%2==a ( set _COMMAND=rem) ELSE ( set _COMMAND=%2)
IF a%3==a ( set _FILTER=*.*) ELSE ( set _FILTER=%3)
set _OPTS=%4

FOR /F "delims=" %%f IN ('dir %_DIR%%_FILTER% %_OPTS% /b') DO (
%_COMMAND% "%%f"
)


But, I'm trying to determine how to ensure that I call %_COMMAND% on the correct file.



I've tried pre-pending the directory variable onto the front, like %_COMMAND% %_DIR%"%%f", but this leaves a quotation mark in the parameter I pass. For example, if I call my batch file exec_dir.bat, and call it with the following echo_test.bat, I see that all of the files have a quotation mark when echo_test.bat runs.



echo %~dpn1.mp4


That batch script produces:



> exec_dir.bat "C:UsersUserDesktopTest Folder" echo_test.bat *.txt
C:UsersUserDesktopTest Folder"Test File.txt
C:UsersUserDesktopTest Folder"Test2.txt


My thought is that it has something to do with the as an escape character, but I can't seem to work around it.










share|improve this question














I've created a generic batch (or Windows Command) file that lets me loop through the contents of a directory and call a command for each item.



IF a%1==a ( set _DIR="%CD%") ELSE ( set _DIR="%~1")
IF a%2==a ( set _COMMAND=rem) ELSE ( set _COMMAND=%2)
IF a%3==a ( set _FILTER=*.*) ELSE ( set _FILTER=%3)
set _OPTS=%4

FOR /F "delims=" %%f IN ('dir %_DIR%%_FILTER% %_OPTS% /b') DO (
%_COMMAND% "%%f"
)


But, I'm trying to determine how to ensure that I call %_COMMAND% on the correct file.



I've tried pre-pending the directory variable onto the front, like %_COMMAND% %_DIR%"%%f", but this leaves a quotation mark in the parameter I pass. For example, if I call my batch file exec_dir.bat, and call it with the following echo_test.bat, I see that all of the files have a quotation mark when echo_test.bat runs.



echo %~dpn1.mp4


That batch script produces:



> exec_dir.bat "C:UsersUserDesktopTest Folder" echo_test.bat *.txt
C:UsersUserDesktopTest Folder"Test File.txt
C:UsersUserDesktopTest Folder"Test2.txt


My thought is that it has something to do with the as an escape character, but I can't seem to work around it.







windows batch






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 16 '10 at 22:57









palswimpalswim

1,72672951




1,72672951













  • i am not sure if any of these ideas will help but you know %1 %~f1 makes it full. Also, dir /b doesn't seem to give the full path, but dir /b /s does, though i guess you don't want /s. another thought is you can cut bits off of an environment variable e.g. echo abc , then echo %a:~1,2% not sure if that's useful to you

    – barlop
    Jan 31 '11 at 7:34





















  • i am not sure if any of these ideas will help but you know %1 %~f1 makes it full. Also, dir /b doesn't seem to give the full path, but dir /b /s does, though i guess you don't want /s. another thought is you can cut bits off of an environment variable e.g. echo abc , then echo %a:~1,2% not sure if that's useful to you

    – barlop
    Jan 31 '11 at 7:34



















i am not sure if any of these ideas will help but you know %1 %~f1 makes it full. Also, dir /b doesn't seem to give the full path, but dir /b /s does, though i guess you don't want /s. another thought is you can cut bits off of an environment variable e.g. echo abc , then echo %a:~1,2% not sure if that's useful to you

– barlop
Jan 31 '11 at 7:34







i am not sure if any of these ideas will help but you know %1 %~f1 makes it full. Also, dir /b doesn't seem to give the full path, but dir /b /s does, though i guess you don't want /s. another thought is you can cut bits off of an environment variable e.g. echo abc , then echo %a:~1,2% not sure if that's useful to you

– barlop
Jan 31 '11 at 7:34












1 Answer
1






active

oldest

votes


















0















setlocal
if "%~1"=="" (set dir=.) else (set dir="%~1")
if "%~2"=="" (set command=echo) else (set command=%~2)
if "%~3"=="" (set mask=*) else (set mask=%~3)
for /r %dir% %%f in (%mask%) do %command% %%~dpnxf


Or simply:




C:>for /r . %f in (*) do @echo %~dpnxf





share|improve this answer



















  • 1





    I didn't use the FOR /R %_DIR% %%f IN (%_FILTER%) DO syntax because I wanted support for recursion.

    – palswim
    Dec 16 '10 at 23:33











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f222900%2fhow-to-ensure-full-path-in-windows-batch-for-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









0















setlocal
if "%~1"=="" (set dir=.) else (set dir="%~1")
if "%~2"=="" (set command=echo) else (set command=%~2)
if "%~3"=="" (set mask=*) else (set mask=%~3)
for /r %dir% %%f in (%mask%) do %command% %%~dpnxf


Or simply:




C:>for /r . %f in (*) do @echo %~dpnxf





share|improve this answer



















  • 1





    I didn't use the FOR /R %_DIR% %%f IN (%_FILTER%) DO syntax because I wanted support for recursion.

    – palswim
    Dec 16 '10 at 23:33
















0















setlocal
if "%~1"=="" (set dir=.) else (set dir="%~1")
if "%~2"=="" (set command=echo) else (set command=%~2)
if "%~3"=="" (set mask=*) else (set mask=%~3)
for /r %dir% %%f in (%mask%) do %command% %%~dpnxf


Or simply:




C:>for /r . %f in (*) do @echo %~dpnxf





share|improve this answer



















  • 1





    I didn't use the FOR /R %_DIR% %%f IN (%_FILTER%) DO syntax because I wanted support for recursion.

    – palswim
    Dec 16 '10 at 23:33














0












0








0








setlocal
if "%~1"=="" (set dir=.) else (set dir="%~1")
if "%~2"=="" (set command=echo) else (set command=%~2)
if "%~3"=="" (set mask=*) else (set mask=%~3)
for /r %dir% %%f in (%mask%) do %command% %%~dpnxf


Or simply:




C:>for /r . %f in (*) do @echo %~dpnxf





share|improve this answer














setlocal
if "%~1"=="" (set dir=.) else (set dir="%~1")
if "%~2"=="" (set command=echo) else (set command=%~2)
if "%~3"=="" (set mask=*) else (set mask=%~3)
for /r %dir% %%f in (%mask%) do %command% %%~dpnxf


Or simply:




C:>for /r . %f in (*) do @echo %~dpnxf






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 16 '10 at 23:07









grawitygrawity

234k36495550




234k36495550








  • 1





    I didn't use the FOR /R %_DIR% %%f IN (%_FILTER%) DO syntax because I wanted support for recursion.

    – palswim
    Dec 16 '10 at 23:33














  • 1





    I didn't use the FOR /R %_DIR% %%f IN (%_FILTER%) DO syntax because I wanted support for recursion.

    – palswim
    Dec 16 '10 at 23:33








1




1





I didn't use the FOR /R %_DIR% %%f IN (%_FILTER%) DO syntax because I wanted support for recursion.

– palswim
Dec 16 '10 at 23:33





I didn't use the FOR /R %_DIR% %%f IN (%_FILTER%) DO syntax because I wanted support for recursion.

– palswim
Dec 16 '10 at 23:33


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f222900%2fhow-to-ensure-full-path-in-windows-batch-for-loop%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

Probability when a professor distributes a quiz and homework assignment to a class of n students.

Aardman Animations

Are they similar matrix