Windows CMD FINDSTR STRING AND COPY FILE
I have a folder with subfolders include txt and pdf files. There is a Pdf file for each txt file which has nearly same name.
For example; for each ABC_R10.txt --> there is a ABC).pdf file.
In Windows 10, with a batch file,
I want to search specific string in a .txt file with FINDSTR command, and copy files, which contain my string, into current folder. I achieved proper code until this point.
CLS
@ECHO OFF
ECHO FIND BUKUM
findstr /m /s /i /p /c:"BUKUM" *.txt > logfile.xls
for /f "delims=" %%a in ('findstr /m /s /i /p /c:"BUKUM" *.txt') do ^
copy "%%a" "%cd%"
if errorlevel 1 echo nothing found.
PAUSE
CLS
EXIT
But I want to find file name of exact match but get pdf file with similar name, not txt file.
I have to get ABC of ABC_R10.txt and add ).pdf string and get ABC).pdf
Substring of _R occurs each .txt file.
How can I achieve it?
batch-file cmd
add a comment |
I have a folder with subfolders include txt and pdf files. There is a Pdf file for each txt file which has nearly same name.
For example; for each ABC_R10.txt --> there is a ABC).pdf file.
In Windows 10, with a batch file,
I want to search specific string in a .txt file with FINDSTR command, and copy files, which contain my string, into current folder. I achieved proper code until this point.
CLS
@ECHO OFF
ECHO FIND BUKUM
findstr /m /s /i /p /c:"BUKUM" *.txt > logfile.xls
for /f "delims=" %%a in ('findstr /m /s /i /p /c:"BUKUM" *.txt') do ^
copy "%%a" "%cd%"
if errorlevel 1 echo nothing found.
PAUSE
CLS
EXIT
But I want to find file name of exact match but get pdf file with similar name, not txt file.
I have to get ABC of ABC_R10.txt and add ).pdf string and get ABC).pdf
Substring of _R occurs each .txt file.
How can I achieve it?
batch-file cmd
First you should show your code. If iterating the .txt file with a for variable use the modifier%%~nA
for example to return the name without extension and append your.pdf
extension instead.
– LotPings
Nov 23 '18 at 19:58
I edited post with code included. I'm trying your suggestion right now.
– v.3
Nov 23 '18 at 20:16
Changing the scope of your question isn't fair when there is already an answer that does solve your original question. Reason: The answer is now invalid, which will lead to downvotes to the answer (which doesn't deserve it, because it answered your original question) If you have to change the scope of your question, it's better to ask a follow-up question (and add a link to this one for reference). (PS: as long as there is no answer yet, changing your question is not a problem)
– Stephan
Nov 24 '18 at 9:53
You are right. It is my mistake because of I posted from phone without certainity of question. I'm going to follow your suggestion next time.
– v.3
Nov 24 '18 at 9:55
add a comment |
I have a folder with subfolders include txt and pdf files. There is a Pdf file for each txt file which has nearly same name.
For example; for each ABC_R10.txt --> there is a ABC).pdf file.
In Windows 10, with a batch file,
I want to search specific string in a .txt file with FINDSTR command, and copy files, which contain my string, into current folder. I achieved proper code until this point.
CLS
@ECHO OFF
ECHO FIND BUKUM
findstr /m /s /i /p /c:"BUKUM" *.txt > logfile.xls
for /f "delims=" %%a in ('findstr /m /s /i /p /c:"BUKUM" *.txt') do ^
copy "%%a" "%cd%"
if errorlevel 1 echo nothing found.
PAUSE
CLS
EXIT
But I want to find file name of exact match but get pdf file with similar name, not txt file.
I have to get ABC of ABC_R10.txt and add ).pdf string and get ABC).pdf
Substring of _R occurs each .txt file.
How can I achieve it?
batch-file cmd
I have a folder with subfolders include txt and pdf files. There is a Pdf file for each txt file which has nearly same name.
For example; for each ABC_R10.txt --> there is a ABC).pdf file.
In Windows 10, with a batch file,
I want to search specific string in a .txt file with FINDSTR command, and copy files, which contain my string, into current folder. I achieved proper code until this point.
CLS
@ECHO OFF
ECHO FIND BUKUM
findstr /m /s /i /p /c:"BUKUM" *.txt > logfile.xls
for /f "delims=" %%a in ('findstr /m /s /i /p /c:"BUKUM" *.txt') do ^
copy "%%a" "%cd%"
if errorlevel 1 echo nothing found.
PAUSE
CLS
EXIT
But I want to find file name of exact match but get pdf file with similar name, not txt file.
I have to get ABC of ABC_R10.txt and add ).pdf string and get ABC).pdf
Substring of _R occurs each .txt file.
How can I achieve it?
batch-file cmd
batch-file cmd
edited Nov 24 '18 at 8:16
v.3
asked Nov 23 '18 at 19:33
v.3v.3
32
32
First you should show your code. If iterating the .txt file with a for variable use the modifier%%~nA
for example to return the name without extension and append your.pdf
extension instead.
– LotPings
Nov 23 '18 at 19:58
I edited post with code included. I'm trying your suggestion right now.
– v.3
Nov 23 '18 at 20:16
Changing the scope of your question isn't fair when there is already an answer that does solve your original question. Reason: The answer is now invalid, which will lead to downvotes to the answer (which doesn't deserve it, because it answered your original question) If you have to change the scope of your question, it's better to ask a follow-up question (and add a link to this one for reference). (PS: as long as there is no answer yet, changing your question is not a problem)
– Stephan
Nov 24 '18 at 9:53
You are right. It is my mistake because of I posted from phone without certainity of question. I'm going to follow your suggestion next time.
– v.3
Nov 24 '18 at 9:55
add a comment |
First you should show your code. If iterating the .txt file with a for variable use the modifier%%~nA
for example to return the name without extension and append your.pdf
extension instead.
– LotPings
Nov 23 '18 at 19:58
I edited post with code included. I'm trying your suggestion right now.
– v.3
Nov 23 '18 at 20:16
Changing the scope of your question isn't fair when there is already an answer that does solve your original question. Reason: The answer is now invalid, which will lead to downvotes to the answer (which doesn't deserve it, because it answered your original question) If you have to change the scope of your question, it's better to ask a follow-up question (and add a link to this one for reference). (PS: as long as there is no answer yet, changing your question is not a problem)
– Stephan
Nov 24 '18 at 9:53
You are right. It is my mistake because of I posted from phone without certainity of question. I'm going to follow your suggestion next time.
– v.3
Nov 24 '18 at 9:55
First you should show your code. If iterating the .txt file with a for variable use the modifier
%%~nA
for example to return the name without extension and append your .pdf
extension instead.– LotPings
Nov 23 '18 at 19:58
First you should show your code. If iterating the .txt file with a for variable use the modifier
%%~nA
for example to return the name without extension and append your .pdf
extension instead.– LotPings
Nov 23 '18 at 19:58
I edited post with code included. I'm trying your suggestion right now.
– v.3
Nov 23 '18 at 20:16
I edited post with code included. I'm trying your suggestion right now.
– v.3
Nov 23 '18 at 20:16
Changing the scope of your question isn't fair when there is already an answer that does solve your original question. Reason: The answer is now invalid, which will lead to downvotes to the answer (which doesn't deserve it, because it answered your original question) If you have to change the scope of your question, it's better to ask a follow-up question (and add a link to this one for reference). (PS: as long as there is no answer yet, changing your question is not a problem)
– Stephan
Nov 24 '18 at 9:53
Changing the scope of your question isn't fair when there is already an answer that does solve your original question. Reason: The answer is now invalid, which will lead to downvotes to the answer (which doesn't deserve it, because it answered your original question) If you have to change the scope of your question, it's better to ask a follow-up question (and add a link to this one for reference). (PS: as long as there is no answer yet, changing your question is not a problem)
– Stephan
Nov 24 '18 at 9:53
You are right. It is my mistake because of I posted from phone without certainity of question. I'm going to follow your suggestion next time.
– v.3
Nov 24 '18 at 9:55
You are right. It is my mistake because of I posted from phone without certainity of question. I'm going to follow your suggestion next time.
– v.3
Nov 24 '18 at 9:55
add a comment |
2 Answers
2
active
oldest
votes
Based solely on your now edited question:
@For /F "Delims=_" %%A In ('FindStr /SPMIC:"BUKUM" *.txt') Do @Copy /Y "%%A).pdf">Nul
add a comment |
EDIT: there is nothing to say to @Compo's modification,
just when using the )
unquoted inside a code block it has to be escpaped ^)
@ECHO OFF
CLS
ECHO FIND BUKUM
for /f "delims=_" %%A in (
'findstr /msip /c:"BUKUM" *.txt'
) do if exist "%%A).pdf" (
copy "%%A).pdf" "%cd%"
Echo copied %%A^).pdf to %cd%
) else echo not found "%%A).pdf"
PAUSE
Dear LotPings, I had an edit for my post. File names are not same, but similar. So I should take part of file until _R occurs, and add ).pdf section. I tested your code for an example of same name files. It cannot copy any file. What is problem?
– v.3
Nov 24 '18 at 9:14
add a comment |
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
});
}
});
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%2fstackoverflow.com%2fquestions%2f53452100%2fwindows-cmd-findstr-string-and-copy-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Based solely on your now edited question:
@For /F "Delims=_" %%A In ('FindStr /SPMIC:"BUKUM" *.txt') Do @Copy /Y "%%A).pdf">Nul
add a comment |
Based solely on your now edited question:
@For /F "Delims=_" %%A In ('FindStr /SPMIC:"BUKUM" *.txt') Do @Copy /Y "%%A).pdf">Nul
add a comment |
Based solely on your now edited question:
@For /F "Delims=_" %%A In ('FindStr /SPMIC:"BUKUM" *.txt') Do @Copy /Y "%%A).pdf">Nul
Based solely on your now edited question:
@For /F "Delims=_" %%A In ('FindStr /SPMIC:"BUKUM" *.txt') Do @Copy /Y "%%A).pdf">Nul
answered Nov 24 '18 at 14:00
CompoCompo
15.7k3926
15.7k3926
add a comment |
add a comment |
EDIT: there is nothing to say to @Compo's modification,
just when using the )
unquoted inside a code block it has to be escpaped ^)
@ECHO OFF
CLS
ECHO FIND BUKUM
for /f "delims=_" %%A in (
'findstr /msip /c:"BUKUM" *.txt'
) do if exist "%%A).pdf" (
copy "%%A).pdf" "%cd%"
Echo copied %%A^).pdf to %cd%
) else echo not found "%%A).pdf"
PAUSE
Dear LotPings, I had an edit for my post. File names are not same, but similar. So I should take part of file until _R occurs, and add ).pdf section. I tested your code for an example of same name files. It cannot copy any file. What is problem?
– v.3
Nov 24 '18 at 9:14
add a comment |
EDIT: there is nothing to say to @Compo's modification,
just when using the )
unquoted inside a code block it has to be escpaped ^)
@ECHO OFF
CLS
ECHO FIND BUKUM
for /f "delims=_" %%A in (
'findstr /msip /c:"BUKUM" *.txt'
) do if exist "%%A).pdf" (
copy "%%A).pdf" "%cd%"
Echo copied %%A^).pdf to %cd%
) else echo not found "%%A).pdf"
PAUSE
Dear LotPings, I had an edit for my post. File names are not same, but similar. So I should take part of file until _R occurs, and add ).pdf section. I tested your code for an example of same name files. It cannot copy any file. What is problem?
– v.3
Nov 24 '18 at 9:14
add a comment |
EDIT: there is nothing to say to @Compo's modification,
just when using the )
unquoted inside a code block it has to be escpaped ^)
@ECHO OFF
CLS
ECHO FIND BUKUM
for /f "delims=_" %%A in (
'findstr /msip /c:"BUKUM" *.txt'
) do if exist "%%A).pdf" (
copy "%%A).pdf" "%cd%"
Echo copied %%A^).pdf to %cd%
) else echo not found "%%A).pdf"
PAUSE
EDIT: there is nothing to say to @Compo's modification,
just when using the )
unquoted inside a code block it has to be escpaped ^)
@ECHO OFF
CLS
ECHO FIND BUKUM
for /f "delims=_" %%A in (
'findstr /msip /c:"BUKUM" *.txt'
) do if exist "%%A).pdf" (
copy "%%A).pdf" "%cd%"
Echo copied %%A^).pdf to %cd%
) else echo not found "%%A).pdf"
PAUSE
edited Nov 24 '18 at 15:11
answered Nov 23 '18 at 20:32
LotPingsLotPings
18.4k61532
18.4k61532
Dear LotPings, I had an edit for my post. File names are not same, but similar. So I should take part of file until _R occurs, and add ).pdf section. I tested your code for an example of same name files. It cannot copy any file. What is problem?
– v.3
Nov 24 '18 at 9:14
add a comment |
Dear LotPings, I had an edit for my post. File names are not same, but similar. So I should take part of file until _R occurs, and add ).pdf section. I tested your code for an example of same name files. It cannot copy any file. What is problem?
– v.3
Nov 24 '18 at 9:14
Dear LotPings, I had an edit for my post. File names are not same, but similar. So I should take part of file until _R occurs, and add ).pdf section. I tested your code for an example of same name files. It cannot copy any file. What is problem?
– v.3
Nov 24 '18 at 9:14
Dear LotPings, I had an edit for my post. File names are not same, but similar. So I should take part of file until _R occurs, and add ).pdf section. I tested your code for an example of same name files. It cannot copy any file. What is problem?
– v.3
Nov 24 '18 at 9:14
add a comment |
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.
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%2fstackoverflow.com%2fquestions%2f53452100%2fwindows-cmd-findstr-string-and-copy-file%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
First you should show your code. If iterating the .txt file with a for variable use the modifier
%%~nA
for example to return the name without extension and append your.pdf
extension instead.– LotPings
Nov 23 '18 at 19:58
I edited post with code included. I'm trying your suggestion right now.
– v.3
Nov 23 '18 at 20:16
Changing the scope of your question isn't fair when there is already an answer that does solve your original question. Reason: The answer is now invalid, which will lead to downvotes to the answer (which doesn't deserve it, because it answered your original question) If you have to change the scope of your question, it's better to ask a follow-up question (and add a link to this one for reference). (PS: as long as there is no answer yet, changing your question is not a problem)
– Stephan
Nov 24 '18 at 9:53
You are right. It is my mistake because of I posted from phone without certainity of question. I'm going to follow your suggestion next time.
– v.3
Nov 24 '18 at 9:55