Move files into a folder based on only part of their name Announcing the arrival of Valued...

.bashrc alias for a command with fixed second parameter

Keep at all times, the minus sign above aligned with minus sign below

Should man-made satellites feature an intelligent inverted "cow catcher"?

Did any compiler fully use 80-bit floating point?

Any stored/leased 737s that could substitute for grounded MAXs?

Was the pager message from Nick Fury to Captain Marvel unnecessary?

How does TikZ render an arc?

How to make triangles with rounded sides and corners? (squircle with 3 sides)

What does 丫 mean? 丫是什么意思?

Statistical analysis applied to methods coming out of Machine Learning

How to ask rejected full-time candidates to apply to teach individual courses?

What are some likely causes to domain member PC losing contact to domain controller?

How do I say "this must not happen"?

Is a copyright notice with a non-existent name be invalid?

Twin's vs. Twins'

Fit odd number of triplets in a measure?

Understanding piped commands in GNU/Linux

Why are two-digit numbers in Jonathan Swift's "Gulliver's Travels" (1726) written in "German style"?

How to resize main filesystem

How do Java 8 default methods hеlp with lambdas?

How to infer difference of population proportion between two groups when proportion is small?

Do i imagine the linear (straight line) homotopy in a correct way?

Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?

Are there any irrational/transcendental numbers for which the distribution of decimal digits is not uniform?



Move files into a folder based on only part of their name



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)How do I open a random file in a folder, and set that only files with the specified filename extension(s) should be opened?Batch file to delete folder and its contentsFast NT batch script for determining path lengths in a folderPart filename to folderHow can I move files into a folder based on their name?batch file to move files from sub dirs to root dir with overwritting files with name conflictsBatch to move files from root folder into sub-foldersMove files into a folder based on their nameNeed .bat that will make folder based on filenameBatch script to move files from parent folder into subfolders in lots of specified quantity





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







0















I have a bunch of files that I am trying to organize into folders based on the first 11 characters (Ex. X-01234-567) and some of the files have description after these numbers that help to identify what they are. I have been able to make folders that only have these 11 characters and then move all files that start with the corresponding 11 characters into those folders. The issue I'm having is that there already are a bunch of folders that have more than just those 11 characters (Ex. X-09902-024 Adapter, 0.38 NPT) etc. I want the batch file to look for folders that already have the same first 11 characters as the part, and then move the part into that folder if they match, otherwise if there isn't a folder, then to create one. Attached are screenshots of the different ways I've tried it. I'm assuming it's something really simple, but I can't figure out the syntax.



I didn't know I couldn't add in images yet, so here are the copies of the two different batch files that I tried. Thanks in advance for your help.



@echo off
setlocal

set "basename=."
for /F "tokens=1* delims=." %%a in ('dir /B /A-D ^| sort /R') do (
set "filename=%%a"
setlocal EnableDelayedExpansion
for /F "delims=" %%c in ("!basename!") do if "!filename:%%c=!" equ "!filename!" (
set "basename=!filename:~0,11!"
if not exist "!filename:~0,11!" md "!basename!"
)
move "!filename!.%%b" "!basename!"
for /F "delims=" %%c in ("!basename!") do (
endlocal
set "basename=%%c
)
)


The second one I tried is here:



@echo off
for /f "delims=" %%F in (
'Dir /b *.dwg *.jpg *.pdf *.slddrw *.sldprt *.sldasm *.STEP^|findstr "^[X]-[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]" '
) do call :subr "%%F"
exit /b

:subr
set "file=%~n1"
set "fold=%file:~0,11%"
if not exist "%fold%*" md "%fold%"
move %1 "%fold%"


Image showing script working if folder was created by it



The image I attached is showing how the files will go into the folder if it was created by the script, but there was a folder with the same name except with more characters, and it left it empty. Please let me know if you need me to clarify anything!










share|improve this question







New contributor




Braden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • General notes: (1) Why do you believe that you need endlocal and two setlocal statements?  It’s typical just to do a setlocal at the beginning and leave it at that.  (2) I’m not sure it makes sense to do endlocal in a loop.  (3) Your last statement (third-to-last line) has an unmatched quote.  (4) It’s a good idea to comment your code.  Doing so will force you to take another look at the code and maybe find the problem yourself.  But, even if that doesn’t happen, it’s helpful to the people who will be reading your code. … (Cont’d)

    – Scott
    2 hours ago











  • (Cont’d) …  (5) Please try to explain more clearly what you want to happen and what is happening that’s different.  In particular, I don’t understand what you mean by “part”. … … … … Please do not respond in comments; edit your question to make it clearer and more complete.

    – Scott
    2 hours ago




















0















I have a bunch of files that I am trying to organize into folders based on the first 11 characters (Ex. X-01234-567) and some of the files have description after these numbers that help to identify what they are. I have been able to make folders that only have these 11 characters and then move all files that start with the corresponding 11 characters into those folders. The issue I'm having is that there already are a bunch of folders that have more than just those 11 characters (Ex. X-09902-024 Adapter, 0.38 NPT) etc. I want the batch file to look for folders that already have the same first 11 characters as the part, and then move the part into that folder if they match, otherwise if there isn't a folder, then to create one. Attached are screenshots of the different ways I've tried it. I'm assuming it's something really simple, but I can't figure out the syntax.



I didn't know I couldn't add in images yet, so here are the copies of the two different batch files that I tried. Thanks in advance for your help.



@echo off
setlocal

set "basename=."
for /F "tokens=1* delims=." %%a in ('dir /B /A-D ^| sort /R') do (
set "filename=%%a"
setlocal EnableDelayedExpansion
for /F "delims=" %%c in ("!basename!") do if "!filename:%%c=!" equ "!filename!" (
set "basename=!filename:~0,11!"
if not exist "!filename:~0,11!" md "!basename!"
)
move "!filename!.%%b" "!basename!"
for /F "delims=" %%c in ("!basename!") do (
endlocal
set "basename=%%c
)
)


The second one I tried is here:



@echo off
for /f "delims=" %%F in (
'Dir /b *.dwg *.jpg *.pdf *.slddrw *.sldprt *.sldasm *.STEP^|findstr "^[X]-[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]" '
) do call :subr "%%F"
exit /b

:subr
set "file=%~n1"
set "fold=%file:~0,11%"
if not exist "%fold%*" md "%fold%"
move %1 "%fold%"


Image showing script working if folder was created by it



The image I attached is showing how the files will go into the folder if it was created by the script, but there was a folder with the same name except with more characters, and it left it empty. Please let me know if you need me to clarify anything!










share|improve this question







New contributor




Braden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • General notes: (1) Why do you believe that you need endlocal and two setlocal statements?  It’s typical just to do a setlocal at the beginning and leave it at that.  (2) I’m not sure it makes sense to do endlocal in a loop.  (3) Your last statement (third-to-last line) has an unmatched quote.  (4) It’s a good idea to comment your code.  Doing so will force you to take another look at the code and maybe find the problem yourself.  But, even if that doesn’t happen, it’s helpful to the people who will be reading your code. … (Cont’d)

    – Scott
    2 hours ago











  • (Cont’d) …  (5) Please try to explain more clearly what you want to happen and what is happening that’s different.  In particular, I don’t understand what you mean by “part”. … … … … Please do not respond in comments; edit your question to make it clearer and more complete.

    – Scott
    2 hours ago
















0












0








0








I have a bunch of files that I am trying to organize into folders based on the first 11 characters (Ex. X-01234-567) and some of the files have description after these numbers that help to identify what they are. I have been able to make folders that only have these 11 characters and then move all files that start with the corresponding 11 characters into those folders. The issue I'm having is that there already are a bunch of folders that have more than just those 11 characters (Ex. X-09902-024 Adapter, 0.38 NPT) etc. I want the batch file to look for folders that already have the same first 11 characters as the part, and then move the part into that folder if they match, otherwise if there isn't a folder, then to create one. Attached are screenshots of the different ways I've tried it. I'm assuming it's something really simple, but I can't figure out the syntax.



I didn't know I couldn't add in images yet, so here are the copies of the two different batch files that I tried. Thanks in advance for your help.



@echo off
setlocal

set "basename=."
for /F "tokens=1* delims=." %%a in ('dir /B /A-D ^| sort /R') do (
set "filename=%%a"
setlocal EnableDelayedExpansion
for /F "delims=" %%c in ("!basename!") do if "!filename:%%c=!" equ "!filename!" (
set "basename=!filename:~0,11!"
if not exist "!filename:~0,11!" md "!basename!"
)
move "!filename!.%%b" "!basename!"
for /F "delims=" %%c in ("!basename!") do (
endlocal
set "basename=%%c
)
)


The second one I tried is here:



@echo off
for /f "delims=" %%F in (
'Dir /b *.dwg *.jpg *.pdf *.slddrw *.sldprt *.sldasm *.STEP^|findstr "^[X]-[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]" '
) do call :subr "%%F"
exit /b

:subr
set "file=%~n1"
set "fold=%file:~0,11%"
if not exist "%fold%*" md "%fold%"
move %1 "%fold%"


Image showing script working if folder was created by it



The image I attached is showing how the files will go into the folder if it was created by the script, but there was a folder with the same name except with more characters, and it left it empty. Please let me know if you need me to clarify anything!










share|improve this question







New contributor




Braden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I have a bunch of files that I am trying to organize into folders based on the first 11 characters (Ex. X-01234-567) and some of the files have description after these numbers that help to identify what they are. I have been able to make folders that only have these 11 characters and then move all files that start with the corresponding 11 characters into those folders. The issue I'm having is that there already are a bunch of folders that have more than just those 11 characters (Ex. X-09902-024 Adapter, 0.38 NPT) etc. I want the batch file to look for folders that already have the same first 11 characters as the part, and then move the part into that folder if they match, otherwise if there isn't a folder, then to create one. Attached are screenshots of the different ways I've tried it. I'm assuming it's something really simple, but I can't figure out the syntax.



I didn't know I couldn't add in images yet, so here are the copies of the two different batch files that I tried. Thanks in advance for your help.



@echo off
setlocal

set "basename=."
for /F "tokens=1* delims=." %%a in ('dir /B /A-D ^| sort /R') do (
set "filename=%%a"
setlocal EnableDelayedExpansion
for /F "delims=" %%c in ("!basename!") do if "!filename:%%c=!" equ "!filename!" (
set "basename=!filename:~0,11!"
if not exist "!filename:~0,11!" md "!basename!"
)
move "!filename!.%%b" "!basename!"
for /F "delims=" %%c in ("!basename!") do (
endlocal
set "basename=%%c
)
)


The second one I tried is here:



@echo off
for /f "delims=" %%F in (
'Dir /b *.dwg *.jpg *.pdf *.slddrw *.sldprt *.sldasm *.STEP^|findstr "^[X]-[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]" '
) do call :subr "%%F"
exit /b

:subr
set "file=%~n1"
set "fold=%file:~0,11%"
if not exist "%fold%*" md "%fold%"
move %1 "%fold%"


Image showing script working if folder was created by it



The image I attached is showing how the files will go into the folder if it was created by the script, but there was a folder with the same name except with more characters, and it left it empty. Please let me know if you need me to clarify anything!







windows command-line batch script batch-file






share|improve this question







New contributor




Braden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Braden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Braden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









BradenBraden

1




1




New contributor




Braden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Braden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Braden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • General notes: (1) Why do you believe that you need endlocal and two setlocal statements?  It’s typical just to do a setlocal at the beginning and leave it at that.  (2) I’m not sure it makes sense to do endlocal in a loop.  (3) Your last statement (third-to-last line) has an unmatched quote.  (4) It’s a good idea to comment your code.  Doing so will force you to take another look at the code and maybe find the problem yourself.  But, even if that doesn’t happen, it’s helpful to the people who will be reading your code. … (Cont’d)

    – Scott
    2 hours ago











  • (Cont’d) …  (5) Please try to explain more clearly what you want to happen and what is happening that’s different.  In particular, I don’t understand what you mean by “part”. … … … … Please do not respond in comments; edit your question to make it clearer and more complete.

    – Scott
    2 hours ago





















  • General notes: (1) Why do you believe that you need endlocal and two setlocal statements?  It’s typical just to do a setlocal at the beginning and leave it at that.  (2) I’m not sure it makes sense to do endlocal in a loop.  (3) Your last statement (third-to-last line) has an unmatched quote.  (4) It’s a good idea to comment your code.  Doing so will force you to take another look at the code and maybe find the problem yourself.  But, even if that doesn’t happen, it’s helpful to the people who will be reading your code. … (Cont’d)

    – Scott
    2 hours ago











  • (Cont’d) …  (5) Please try to explain more clearly what you want to happen and what is happening that’s different.  In particular, I don’t understand what you mean by “part”. … … … … Please do not respond in comments; edit your question to make it clearer and more complete.

    – Scott
    2 hours ago



















General notes: (1) Why do you believe that you need endlocal and two setlocal statements?  It’s typical just to do a setlocal at the beginning and leave it at that.  (2) I’m not sure it makes sense to do endlocal in a loop.  (3) Your last statement (third-to-last line) has an unmatched quote.  (4) It’s a good idea to comment your code.  Doing so will force you to take another look at the code and maybe find the problem yourself.  But, even if that doesn’t happen, it’s helpful to the people who will be reading your code. … (Cont’d)

– Scott
2 hours ago





General notes: (1) Why do you believe that you need endlocal and two setlocal statements?  It’s typical just to do a setlocal at the beginning and leave it at that.  (2) I’m not sure it makes sense to do endlocal in a loop.  (3) Your last statement (third-to-last line) has an unmatched quote.  (4) It’s a good idea to comment your code.  Doing so will force you to take another look at the code and maybe find the problem yourself.  But, even if that doesn’t happen, it’s helpful to the people who will be reading your code. … (Cont’d)

– Scott
2 hours ago













(Cont’d) …  (5) Please try to explain more clearly what you want to happen and what is happening that’s different.  In particular, I don’t understand what you mean by “part”. … … … … Please do not respond in comments; edit your question to make it clearer and more complete.

– Scott
2 hours ago







(Cont’d) …  (5) Please try to explain more clearly what you want to happen and what is happening that’s different.  In particular, I don’t understand what you mean by “part”. … … … … Please do not respond in comments; edit your question to make it clearer and more complete.

– Scott
2 hours ago












0






active

oldest

votes












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


}
});






Braden is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1428072%2fmove-files-into-a-folder-based-on-only-part-of-their-name%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








Braden is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Braden is a new contributor. Be nice, and check out our Code of Conduct.













Braden is a new contributor. Be nice, and check out our Code of Conduct.












Braden 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1428072%2fmove-files-into-a-folder-based-on-only-part-of-their-name%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

Couldn't open a raw socket. Error: Permission denied (13) (nmap)Is it possible to run networking commands...

VNC viewer RFB protocol error: bad desktop size 0x0I Cannot Type the Key 'd' (lowercase) in VNC Viewer...

Why not use the yoke to control yaw, as well as pitch and roll? Announcing the arrival of...