Attempt To Use Batch Files While Keep Structure
I wanted to use robocopy
, but it is not possible due to my text file has filenames attached.
I ended up have to use copy
instead.
I wanted to copy files like this...
Source: S:folderABCDEproduct_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
Destination: C:wamp64FGproduct_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
Here is how my text file (ActivePDF.txt) listed..
product_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
product_pdfDriveDriveCommodesNRS185007-11117N.pdf
product_pdfDriveDriveCommodes11125Series.pdf
product_pdfDriveDriveSuctionCupGrabBar_RTL13082.pdf
product_pdfDriveDriveChromeKnurledGrabBar.pdf
My attempted batch file is looked like this
@echo on
enableextensions
set main_folder=S:folderABCDE
set my_folder=C:wamp64FG
set log_file="%main_folder%CopyLog.txt"
for /f "delims=" %%a in (ActivePDF.txt) do if exist "%main_folder%%%a" (
md "%my_folder%%%a" 2>nul
copy /v /y "%main_folder%%%a" "%my_folder%%%a"
)
It did copied, but it ended up created folder with ".pdf" and actual PDF files are inside that ".pdf" folders. (Therefore, each PDF contained in the own folder...)
I think I'm getting there... but I do wonder if there is any cleaner way to do it.
windows batch file-transfer robocopy xcopy
|
show 1 more comment
I wanted to use robocopy
, but it is not possible due to my text file has filenames attached.
I ended up have to use copy
instead.
I wanted to copy files like this...
Source: S:folderABCDEproduct_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
Destination: C:wamp64FGproduct_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
Here is how my text file (ActivePDF.txt) listed..
product_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
product_pdfDriveDriveCommodesNRS185007-11117N.pdf
product_pdfDriveDriveCommodes11125Series.pdf
product_pdfDriveDriveSuctionCupGrabBar_RTL13082.pdf
product_pdfDriveDriveChromeKnurledGrabBar.pdf
My attempted batch file is looked like this
@echo on
enableextensions
set main_folder=S:folderABCDE
set my_folder=C:wamp64FG
set log_file="%main_folder%CopyLog.txt"
for /f "delims=" %%a in (ActivePDF.txt) do if exist "%main_folder%%%a" (
md "%my_folder%%%a" 2>nul
copy /v /y "%main_folder%%%a" "%my_folder%%%a"
)
It did copied, but it ended up created folder with ".pdf" and actual PDF files are inside that ".pdf" folders. (Therefore, each PDF contained in the own folder...)
I think I'm getting there... but I do wonder if there is any cleaner way to do it.
windows batch file-transfer robocopy xcopy
It'smd "%my_folder%%%a" 2>nul
that's creating the subdirectories and causing your problem: omit this line and all your files will be copied to%my_folder%
. To emphasise the point, you can omit%%a
from the copy target, iecopy /v /y "%main_folder%%%a" "%my_folder%"
.
– AFH
Feb 6 at 22:02
If you need to make sure that the directory path exists in the target, replace themd
line bymd "%my_folder%%%a.." 2>nul
(this is a trick to avoid parsing the%%a
variable).
– AFH
Feb 6 at 22:13
It ended up copy "my_folder" without sub directories that I'm intended to put into. I do needed those PDF files into sub directories as stated in text files.
– PanK
Feb 11 at 21:26
If you want the source directory structure recreated in"%my_folder%"
, then you can use the same trick with themd
on thecopy
command:copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
.
– AFH
Feb 11 at 22:46
Yep, this copy command is the answer!copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
So, all I truly need is the..
part to go up by one folder!
– PanK
Feb 12 at 15:27
|
show 1 more comment
I wanted to use robocopy
, but it is not possible due to my text file has filenames attached.
I ended up have to use copy
instead.
I wanted to copy files like this...
Source: S:folderABCDEproduct_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
Destination: C:wamp64FGproduct_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
Here is how my text file (ActivePDF.txt) listed..
product_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
product_pdfDriveDriveCommodesNRS185007-11117N.pdf
product_pdfDriveDriveCommodes11125Series.pdf
product_pdfDriveDriveSuctionCupGrabBar_RTL13082.pdf
product_pdfDriveDriveChromeKnurledGrabBar.pdf
My attempted batch file is looked like this
@echo on
enableextensions
set main_folder=S:folderABCDE
set my_folder=C:wamp64FG
set log_file="%main_folder%CopyLog.txt"
for /f "delims=" %%a in (ActivePDF.txt) do if exist "%main_folder%%%a" (
md "%my_folder%%%a" 2>nul
copy /v /y "%main_folder%%%a" "%my_folder%%%a"
)
It did copied, but it ended up created folder with ".pdf" and actual PDF files are inside that ".pdf" folders. (Therefore, each PDF contained in the own folder...)
I think I'm getting there... but I do wonder if there is any cleaner way to do it.
windows batch file-transfer robocopy xcopy
I wanted to use robocopy
, but it is not possible due to my text file has filenames attached.
I ended up have to use copy
instead.
I wanted to copy files like this...
Source: S:folderABCDEproduct_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
Destination: C:wamp64FGproduct_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
Here is how my text file (ActivePDF.txt) listed..
product_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
product_pdfDriveDriveCommodesNRS185007-11117N.pdf
product_pdfDriveDriveCommodes11125Series.pdf
product_pdfDriveDriveSuctionCupGrabBar_RTL13082.pdf
product_pdfDriveDriveChromeKnurledGrabBar.pdf
My attempted batch file is looked like this
@echo on
enableextensions
set main_folder=S:folderABCDE
set my_folder=C:wamp64FG
set log_file="%main_folder%CopyLog.txt"
for /f "delims=" %%a in (ActivePDF.txt) do if exist "%main_folder%%%a" (
md "%my_folder%%%a" 2>nul
copy /v /y "%main_folder%%%a" "%my_folder%%%a"
)
It did copied, but it ended up created folder with ".pdf" and actual PDF files are inside that ".pdf" folders. (Therefore, each PDF contained in the own folder...)
I think I'm getting there... but I do wonder if there is any cleaner way to do it.
windows batch file-transfer robocopy xcopy
windows batch file-transfer robocopy xcopy
asked Feb 6 at 21:36
PanKPanK
32
32
It'smd "%my_folder%%%a" 2>nul
that's creating the subdirectories and causing your problem: omit this line and all your files will be copied to%my_folder%
. To emphasise the point, you can omit%%a
from the copy target, iecopy /v /y "%main_folder%%%a" "%my_folder%"
.
– AFH
Feb 6 at 22:02
If you need to make sure that the directory path exists in the target, replace themd
line bymd "%my_folder%%%a.." 2>nul
(this is a trick to avoid parsing the%%a
variable).
– AFH
Feb 6 at 22:13
It ended up copy "my_folder" without sub directories that I'm intended to put into. I do needed those PDF files into sub directories as stated in text files.
– PanK
Feb 11 at 21:26
If you want the source directory structure recreated in"%my_folder%"
, then you can use the same trick with themd
on thecopy
command:copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
.
– AFH
Feb 11 at 22:46
Yep, this copy command is the answer!copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
So, all I truly need is the..
part to go up by one folder!
– PanK
Feb 12 at 15:27
|
show 1 more comment
It'smd "%my_folder%%%a" 2>nul
that's creating the subdirectories and causing your problem: omit this line and all your files will be copied to%my_folder%
. To emphasise the point, you can omit%%a
from the copy target, iecopy /v /y "%main_folder%%%a" "%my_folder%"
.
– AFH
Feb 6 at 22:02
If you need to make sure that the directory path exists in the target, replace themd
line bymd "%my_folder%%%a.." 2>nul
(this is a trick to avoid parsing the%%a
variable).
– AFH
Feb 6 at 22:13
It ended up copy "my_folder" without sub directories that I'm intended to put into. I do needed those PDF files into sub directories as stated in text files.
– PanK
Feb 11 at 21:26
If you want the source directory structure recreated in"%my_folder%"
, then you can use the same trick with themd
on thecopy
command:copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
.
– AFH
Feb 11 at 22:46
Yep, this copy command is the answer!copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
So, all I truly need is the..
part to go up by one folder!
– PanK
Feb 12 at 15:27
It's
md "%my_folder%%%a" 2>nul
that's creating the subdirectories and causing your problem: omit this line and all your files will be copied to %my_folder%
. To emphasise the point, you can omit %%a
from the copy target, ie copy /v /y "%main_folder%%%a" "%my_folder%"
.– AFH
Feb 6 at 22:02
It's
md "%my_folder%%%a" 2>nul
that's creating the subdirectories and causing your problem: omit this line and all your files will be copied to %my_folder%
. To emphasise the point, you can omit %%a
from the copy target, ie copy /v /y "%main_folder%%%a" "%my_folder%"
.– AFH
Feb 6 at 22:02
If you need to make sure that the directory path exists in the target, replace the
md
line by md "%my_folder%%%a.." 2>nul
(this is a trick to avoid parsing the %%a
variable).– AFH
Feb 6 at 22:13
If you need to make sure that the directory path exists in the target, replace the
md
line by md "%my_folder%%%a.." 2>nul
(this is a trick to avoid parsing the %%a
variable).– AFH
Feb 6 at 22:13
It ended up copy "my_folder" without sub directories that I'm intended to put into. I do needed those PDF files into sub directories as stated in text files.
– PanK
Feb 11 at 21:26
It ended up copy "my_folder" without sub directories that I'm intended to put into. I do needed those PDF files into sub directories as stated in text files.
– PanK
Feb 11 at 21:26
If you want the source directory structure recreated in
"%my_folder%"
, then you can use the same trick with the md
on the copy
command: copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
.– AFH
Feb 11 at 22:46
If you want the source directory structure recreated in
"%my_folder%"
, then you can use the same trick with the md
on the copy
command: copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
.– AFH
Feb 11 at 22:46
Yep, this copy command is the answer!
copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
So, all I truly need is the ..
part to go up by one folder!– PanK
Feb 12 at 15:27
Yep, this copy command is the answer!
copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
So, all I truly need is the ..
part to go up by one folder!– PanK
Feb 12 at 15:27
|
show 1 more comment
1 Answer
1
active
oldest
votes
It's the commands in the for
loop that are causing the problem: %%a
contains both the subdirectory path and the PDF file name, so md "%my_folder%%%a" 2>nul
will create in the target folder a directory path which includes the PDF name as the last subdirectory.
When the target path is a directory the copy
command copies the source file(s) into it, with the result you observed.
While it is possible to parse %%a
to extract the directory path and file names, there is a trick that can be used to reference the directory path without parsing: if %%a
contains a file name with directory path, then %%a..
will reference the directory path. Even though %%a
is a file path, the parsing of %%a..
will identify the containing directory without checking whether the last element of %%a
is a file or directory.
So the commands in the for
loop become:-
md "%my_folder%%%a.." 2>nul
copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
The md
is needed to create the target subdirectory if it does not exist.
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%2f1402874%2fattempt-to-use-batch-files-while-keep-structure%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's the commands in the for
loop that are causing the problem: %%a
contains both the subdirectory path and the PDF file name, so md "%my_folder%%%a" 2>nul
will create in the target folder a directory path which includes the PDF name as the last subdirectory.
When the target path is a directory the copy
command copies the source file(s) into it, with the result you observed.
While it is possible to parse %%a
to extract the directory path and file names, there is a trick that can be used to reference the directory path without parsing: if %%a
contains a file name with directory path, then %%a..
will reference the directory path. Even though %%a
is a file path, the parsing of %%a..
will identify the containing directory without checking whether the last element of %%a
is a file or directory.
So the commands in the for
loop become:-
md "%my_folder%%%a.." 2>nul
copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
The md
is needed to create the target subdirectory if it does not exist.
add a comment |
It's the commands in the for
loop that are causing the problem: %%a
contains both the subdirectory path and the PDF file name, so md "%my_folder%%%a" 2>nul
will create in the target folder a directory path which includes the PDF name as the last subdirectory.
When the target path is a directory the copy
command copies the source file(s) into it, with the result you observed.
While it is possible to parse %%a
to extract the directory path and file names, there is a trick that can be used to reference the directory path without parsing: if %%a
contains a file name with directory path, then %%a..
will reference the directory path. Even though %%a
is a file path, the parsing of %%a..
will identify the containing directory without checking whether the last element of %%a
is a file or directory.
So the commands in the for
loop become:-
md "%my_folder%%%a.." 2>nul
copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
The md
is needed to create the target subdirectory if it does not exist.
add a comment |
It's the commands in the for
loop that are causing the problem: %%a
contains both the subdirectory path and the PDF file name, so md "%my_folder%%%a" 2>nul
will create in the target folder a directory path which includes the PDF name as the last subdirectory.
When the target path is a directory the copy
command copies the source file(s) into it, with the result you observed.
While it is possible to parse %%a
to extract the directory path and file names, there is a trick that can be used to reference the directory path without parsing: if %%a
contains a file name with directory path, then %%a..
will reference the directory path. Even though %%a
is a file path, the parsing of %%a..
will identify the containing directory without checking whether the last element of %%a
is a file or directory.
So the commands in the for
loop become:-
md "%my_folder%%%a.." 2>nul
copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
The md
is needed to create the target subdirectory if it does not exist.
It's the commands in the for
loop that are causing the problem: %%a
contains both the subdirectory path and the PDF file name, so md "%my_folder%%%a" 2>nul
will create in the target folder a directory path which includes the PDF name as the last subdirectory.
When the target path is a directory the copy
command copies the source file(s) into it, with the result you observed.
While it is possible to parse %%a
to extract the directory path and file names, there is a trick that can be used to reference the directory path without parsing: if %%a
contains a file name with directory path, then %%a..
will reference the directory path. Even though %%a
is a file path, the parsing of %%a..
will identify the containing directory without checking whether the last element of %%a
is a file or directory.
So the commands in the for
loop become:-
md "%my_folder%%%a.." 2>nul
copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
The md
is needed to create the target subdirectory if it does not exist.
answered Feb 12 at 17:39
AFHAFH
14.4k31938
14.4k31938
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%2f1402874%2fattempt-to-use-batch-files-while-keep-structure%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
It's
md "%my_folder%%%a" 2>nul
that's creating the subdirectories and causing your problem: omit this line and all your files will be copied to%my_folder%
. To emphasise the point, you can omit%%a
from the copy target, iecopy /v /y "%main_folder%%%a" "%my_folder%"
.– AFH
Feb 6 at 22:02
If you need to make sure that the directory path exists in the target, replace the
md
line bymd "%my_folder%%%a.." 2>nul
(this is a trick to avoid parsing the%%a
variable).– AFH
Feb 6 at 22:13
It ended up copy "my_folder" without sub directories that I'm intended to put into. I do needed those PDF files into sub directories as stated in text files.
– PanK
Feb 11 at 21:26
If you want the source directory structure recreated in
"%my_folder%"
, then you can use the same trick with themd
on thecopy
command:copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
.– AFH
Feb 11 at 22:46
Yep, this copy command is the answer!
copy /v /y "%main_folder%%%a" "%my_folder%%%a.."
So, all I truly need is the..
part to go up by one folder!– PanK
Feb 12 at 15:27