Sorting files into folders, according a portion of the file name
I am looking to sort approximately 3,000 pdf files into their own folders. All 3,000 files are in one folder and follow the same format.
COURSE NAME - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME
I have made all the folders, but now want to move the appropriate PDF document into the corresponding folder. I found this handy answer to get me started: How to sort files into folders, according to file names - Windows CMD.
This is what I'm currently trying to modify:
:: C:UsersUserDesktopTraining Test.cmd
@Echo off
PushD "C:UsersUserDesktopTraining Test"
For %%A in (*.pdf) do For /F "tokens=2,3delims=-." %%B in ("%%A") Do (
If Not exist "%%B" MD "%%B"
Move "%%A" "%%B"
)
PopD
What I'm finding is that it's creating a new folder with a space in front of the employee name. I have a folder named "EMPLOYEEFIRST EMPLOYEELAST", but its making a new folder of " EMPLOYEEFIRST EMPLOYEELAST" (note the space at the beginning).
I have also noticed that it creates a new folder for those employees with a hyphenated first or last name. EMPLOYEE-FIRST EMPLOYEE-LAST just turns into a new folder of " EMPLOYEE".
I'm very new to all of this. Is there a way of resolving the issues with the space in the folder name and hyphenated names?
batch
add a comment |
I am looking to sort approximately 3,000 pdf files into their own folders. All 3,000 files are in one folder and follow the same format.
COURSE NAME - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME
I have made all the folders, but now want to move the appropriate PDF document into the corresponding folder. I found this handy answer to get me started: How to sort files into folders, according to file names - Windows CMD.
This is what I'm currently trying to modify:
:: C:UsersUserDesktopTraining Test.cmd
@Echo off
PushD "C:UsersUserDesktopTraining Test"
For %%A in (*.pdf) do For /F "tokens=2,3delims=-." %%B in ("%%A") Do (
If Not exist "%%B" MD "%%B"
Move "%%A" "%%B"
)
PopD
What I'm finding is that it's creating a new folder with a space in front of the employee name. I have a folder named "EMPLOYEEFIRST EMPLOYEELAST", but its making a new folder of " EMPLOYEEFIRST EMPLOYEELAST" (note the space at the beginning).
I have also noticed that it creates a new folder for those employees with a hyphenated first or last name. EMPLOYEE-FIRST EMPLOYEE-LAST just turns into a new folder of " EMPLOYEE".
I'm very new to all of this. Is there a way of resolving the issues with the space in the folder name and hyphenated names?
batch
You need to remove this space. Try to use additionalfor /F "tokens=1,* delims= " %%C in (%%B) do (and use %%D there)
– Akina
Mar 1 at 7:23
I think you are missing a blank beforetokens. Instead of"tokens=2,3delims=-."you should have"tokens=2,3 delims=-. "(note the blank at the end).
– harrymc
Mar 1 at 7:24
If the spaces are consistent then use the space delimiter instead of dashes. Alternatively,md %%Bandmove "%%A" %%Bwithout quotes on%%Bto strip off any preceding or trailing spaces.
– shawn
Mar 1 at 16:24
add a comment |
I am looking to sort approximately 3,000 pdf files into their own folders. All 3,000 files are in one folder and follow the same format.
COURSE NAME - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME
I have made all the folders, but now want to move the appropriate PDF document into the corresponding folder. I found this handy answer to get me started: How to sort files into folders, according to file names - Windows CMD.
This is what I'm currently trying to modify:
:: C:UsersUserDesktopTraining Test.cmd
@Echo off
PushD "C:UsersUserDesktopTraining Test"
For %%A in (*.pdf) do For /F "tokens=2,3delims=-." %%B in ("%%A") Do (
If Not exist "%%B" MD "%%B"
Move "%%A" "%%B"
)
PopD
What I'm finding is that it's creating a new folder with a space in front of the employee name. I have a folder named "EMPLOYEEFIRST EMPLOYEELAST", but its making a new folder of " EMPLOYEEFIRST EMPLOYEELAST" (note the space at the beginning).
I have also noticed that it creates a new folder for those employees with a hyphenated first or last name. EMPLOYEE-FIRST EMPLOYEE-LAST just turns into a new folder of " EMPLOYEE".
I'm very new to all of this. Is there a way of resolving the issues with the space in the folder name and hyphenated names?
batch
I am looking to sort approximately 3,000 pdf files into their own folders. All 3,000 files are in one folder and follow the same format.
COURSE NAME - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME
I have made all the folders, but now want to move the appropriate PDF document into the corresponding folder. I found this handy answer to get me started: How to sort files into folders, according to file names - Windows CMD.
This is what I'm currently trying to modify:
:: C:UsersUserDesktopTraining Test.cmd
@Echo off
PushD "C:UsersUserDesktopTraining Test"
For %%A in (*.pdf) do For /F "tokens=2,3delims=-." %%B in ("%%A") Do (
If Not exist "%%B" MD "%%B"
Move "%%A" "%%B"
)
PopD
What I'm finding is that it's creating a new folder with a space in front of the employee name. I have a folder named "EMPLOYEEFIRST EMPLOYEELAST", but its making a new folder of " EMPLOYEEFIRST EMPLOYEELAST" (note the space at the beginning).
I have also noticed that it creates a new folder for those employees with a hyphenated first or last name. EMPLOYEE-FIRST EMPLOYEE-LAST just turns into a new folder of " EMPLOYEE".
I'm very new to all of this. Is there a way of resolving the issues with the space in the folder name and hyphenated names?
batch
batch
edited Mar 1 at 10:50
LotPings
5,2351823
5,2351823
asked Mar 1 at 6:50
Mickey Delo CanesMickey Delo Canes
12
12
You need to remove this space. Try to use additionalfor /F "tokens=1,* delims= " %%C in (%%B) do (and use %%D there)
– Akina
Mar 1 at 7:23
I think you are missing a blank beforetokens. Instead of"tokens=2,3delims=-."you should have"tokens=2,3 delims=-. "(note the blank at the end).
– harrymc
Mar 1 at 7:24
If the spaces are consistent then use the space delimiter instead of dashes. Alternatively,md %%Bandmove "%%A" %%Bwithout quotes on%%Bto strip off any preceding or trailing spaces.
– shawn
Mar 1 at 16:24
add a comment |
You need to remove this space. Try to use additionalfor /F "tokens=1,* delims= " %%C in (%%B) do (and use %%D there)
– Akina
Mar 1 at 7:23
I think you are missing a blank beforetokens. Instead of"tokens=2,3delims=-."you should have"tokens=2,3 delims=-. "(note the blank at the end).
– harrymc
Mar 1 at 7:24
If the spaces are consistent then use the space delimiter instead of dashes. Alternatively,md %%Bandmove "%%A" %%Bwithout quotes on%%Bto strip off any preceding or trailing spaces.
– shawn
Mar 1 at 16:24
You need to remove this space. Try to use additional
for /F "tokens=1,* delims= " %%C in (%%B) do (and use %%D there)– Akina
Mar 1 at 7:23
You need to remove this space. Try to use additional
for /F "tokens=1,* delims= " %%C in (%%B) do (and use %%D there)– Akina
Mar 1 at 7:23
I think you are missing a blank before
tokens. Instead of "tokens=2,3delims=-." you should have "tokens=2,3 delims=-. " (note the blank at the end).– harrymc
Mar 1 at 7:24
I think you are missing a blank before
tokens. Instead of "tokens=2,3delims=-." you should have "tokens=2,3 delims=-. " (note the blank at the end).– harrymc
Mar 1 at 7:24
If the spaces are consistent then use the space delimiter instead of dashes. Alternatively,
md %%B and move "%%A" %%B without quotes on %%B to strip off any preceding or trailing spaces.– shawn
Mar 1 at 16:24
If the spaces are consistent then use the space delimiter instead of dashes. Alternatively,
md %%B and move "%%A" %%B without quotes on %%B to strip off any preceding or trailing spaces.– shawn
Mar 1 at 16:24
add a comment |
1 Answer
1
active
oldest
votes
It looks like your delimiter between course name and first/last name is the sequence space-space.
Unfortunately for /f can't treat this as one delimiter but would treat them all as single delimiters and more worse see's adjacent delimiters as only one.
You can use string substitution to replace - with one character like : and then split on that.
String substitution requires normal (no for meta) variables and inside a code block needs delayed expansion.
Sample tree on my ram drive a: before:
> tree a: /F
A:
COURSE NAME - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
COURSE NAME - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
NAME COURSE - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
NAME COURSE - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
running this batch file:
:: Q:Test201931SU_1410384.cmd
@Echo off & SetLocal EnableDelayedExpansion
PushD "C:UsersUserDesktopTraining Test"
For /f "delims=" %%A in ('dir /B "* - *.pdf" 2^>Nul') do (
Set "BaseName=%%~nA"
Set "BaseName=!BaseName: - =:!"
For /F "tokens=1,2 delims=:" %%B in ("!BaseName!") Do (
If Not exist "%%B" MD "%%B"
Move "%%A" "%%B" 1>Nul
)
)
PopD
And tree after:
> tree a: /F
A:
├───COURSE NAME
│ COURSE NAME - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
│ COURSE NAME - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
│
└───NAME COURSE
NAME COURSE - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
NAME COURSE - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
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
});
}
});
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%2f1410384%2fsorting-files-into-folders-according-a-portion-of-the-file-name%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
It looks like your delimiter between course name and first/last name is the sequence space-space.
Unfortunately for /f can't treat this as one delimiter but would treat them all as single delimiters and more worse see's adjacent delimiters as only one.
You can use string substitution to replace - with one character like : and then split on that.
String substitution requires normal (no for meta) variables and inside a code block needs delayed expansion.
Sample tree on my ram drive a: before:
> tree a: /F
A:
COURSE NAME - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
COURSE NAME - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
NAME COURSE - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
NAME COURSE - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
running this batch file:
:: Q:Test201931SU_1410384.cmd
@Echo off & SetLocal EnableDelayedExpansion
PushD "C:UsersUserDesktopTraining Test"
For /f "delims=" %%A in ('dir /B "* - *.pdf" 2^>Nul') do (
Set "BaseName=%%~nA"
Set "BaseName=!BaseName: - =:!"
For /F "tokens=1,2 delims=:" %%B in ("!BaseName!") Do (
If Not exist "%%B" MD "%%B"
Move "%%A" "%%B" 1>Nul
)
)
PopD
And tree after:
> tree a: /F
A:
├───COURSE NAME
│ COURSE NAME - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
│ COURSE NAME - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
│
└───NAME COURSE
NAME COURSE - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
NAME COURSE - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
add a comment |
It looks like your delimiter between course name and first/last name is the sequence space-space.
Unfortunately for /f can't treat this as one delimiter but would treat them all as single delimiters and more worse see's adjacent delimiters as only one.
You can use string substitution to replace - with one character like : and then split on that.
String substitution requires normal (no for meta) variables and inside a code block needs delayed expansion.
Sample tree on my ram drive a: before:
> tree a: /F
A:
COURSE NAME - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
COURSE NAME - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
NAME COURSE - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
NAME COURSE - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
running this batch file:
:: Q:Test201931SU_1410384.cmd
@Echo off & SetLocal EnableDelayedExpansion
PushD "C:UsersUserDesktopTraining Test"
For /f "delims=" %%A in ('dir /B "* - *.pdf" 2^>Nul') do (
Set "BaseName=%%~nA"
Set "BaseName=!BaseName: - =:!"
For /F "tokens=1,2 delims=:" %%B in ("!BaseName!") Do (
If Not exist "%%B" MD "%%B"
Move "%%A" "%%B" 1>Nul
)
)
PopD
And tree after:
> tree a: /F
A:
├───COURSE NAME
│ COURSE NAME - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
│ COURSE NAME - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
│
└───NAME COURSE
NAME COURSE - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
NAME COURSE - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
add a comment |
It looks like your delimiter between course name and first/last name is the sequence space-space.
Unfortunately for /f can't treat this as one delimiter but would treat them all as single delimiters and more worse see's adjacent delimiters as only one.
You can use string substitution to replace - with one character like : and then split on that.
String substitution requires normal (no for meta) variables and inside a code block needs delayed expansion.
Sample tree on my ram drive a: before:
> tree a: /F
A:
COURSE NAME - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
COURSE NAME - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
NAME COURSE - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
NAME COURSE - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
running this batch file:
:: Q:Test201931SU_1410384.cmd
@Echo off & SetLocal EnableDelayedExpansion
PushD "C:UsersUserDesktopTraining Test"
For /f "delims=" %%A in ('dir /B "* - *.pdf" 2^>Nul') do (
Set "BaseName=%%~nA"
Set "BaseName=!BaseName: - =:!"
For /F "tokens=1,2 delims=:" %%B in ("!BaseName!") Do (
If Not exist "%%B" MD "%%B"
Move "%%A" "%%B" 1>Nul
)
)
PopD
And tree after:
> tree a: /F
A:
├───COURSE NAME
│ COURSE NAME - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
│ COURSE NAME - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
│
└───NAME COURSE
NAME COURSE - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
NAME COURSE - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
It looks like your delimiter between course name and first/last name is the sequence space-space.
Unfortunately for /f can't treat this as one delimiter but would treat them all as single delimiters and more worse see's adjacent delimiters as only one.
You can use string substitution to replace - with one character like : and then split on that.
String substitution requires normal (no for meta) variables and inside a code block needs delayed expansion.
Sample tree on my ram drive a: before:
> tree a: /F
A:
COURSE NAME - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
COURSE NAME - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
NAME COURSE - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
NAME COURSE - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
running this batch file:
:: Q:Test201931SU_1410384.cmd
@Echo off & SetLocal EnableDelayedExpansion
PushD "C:UsersUserDesktopTraining Test"
For /f "delims=" %%A in ('dir /B "* - *.pdf" 2^>Nul') do (
Set "BaseName=%%~nA"
Set "BaseName=!BaseName: - =:!"
For /F "tokens=1,2 delims=:" %%B in ("!BaseName!") Do (
If Not exist "%%B" MD "%%B"
Move "%%A" "%%B" 1>Nul
)
)
PopD
And tree after:
> tree a: /F
A:
├───COURSE NAME
│ COURSE NAME - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
│ COURSE NAME - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
│
└───NAME COURSE
NAME COURSE - EMPLOYEE-FIRST EMPLOYEE-LAST.pdf
NAME COURSE - EMPLOYEEFIRSTNAME EMPLOYEELASTNAME.pdf
answered Mar 1 at 11:46
LotPingsLotPings
5,2351823
5,2351823
add a comment |
add a comment |
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.
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%2f1410384%2fsorting-files-into-folders-according-a-portion-of-the-file-name%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
You need to remove this space. Try to use additional
for /F "tokens=1,* delims= " %%C in (%%B) do (and use %%D there)– Akina
Mar 1 at 7:23
I think you are missing a blank before
tokens. Instead of"tokens=2,3delims=-."you should have"tokens=2,3 delims=-. "(note the blank at the end).– harrymc
Mar 1 at 7:24
If the spaces are consistent then use the space delimiter instead of dashes. Alternatively,
md %%Bandmove "%%A" %%Bwithout quotes on%%Bto strip off any preceding or trailing spaces.– shawn
Mar 1 at 16:24