FOR command / batch including DIR
Moderator: DosItHelp
FOR command / batch including DIR
Please help me with the syntax of:
copy every file in dir c:\a\b\c /a-d /on /b to c: \d\e\f
both in command line and batch.
Alternatively, copy every file in c:\a\b\c\filelist.txt from c:\d\e\f to c:\g\h\i
Thank you
copy every file in dir c:\a\b\c /a-d /on /b to c: \d\e\f
both in command line and batch.
Alternatively, copy every file in c:\a\b\c\filelist.txt from c:\d\e\f to c:\g\h\i
Thank you
Re: FOR command / batch including DIR
Possibly……if you need something else please explain it better.
Code: Select all
Copy C:\a\b\c\*.* C:\d\e\f
-
- Posts: 100
- Joined: 16 Dec 2016 22:31
Re: FOR command / batch including DIR
For the second one use this(untested)
Sounak
Code: Select all
for /f %%a in ("c:/a/b/c/filelist.txt") do (
cd "c:/d/e/f"
if exists %%a copy %%a "c:/g/h/i"
)
Sounak
Re: FOR command / batch including DIR
Sounak@9434 wrote:For the second one use this(untested)Code: Select all
for /f %%a in ("c:/a/b/c/filelist.txt") do (
cd "c:/d/e/f"
if exists %%a copy %%a "c:/g/h/i"
)
Sounak
You might want to account for spaces in file names.
Re: FOR command / batch including DIR
Compo wrote:Possibly……if you need something else please explain it better.Code: Select all
Copy C:\a\b\c\*.* C:\d\e\f
The difference between copy every file in dir c:\a\b\c /a-d /on /b to c: \d\e\f AND
Code: Select all
Copy C:\a\b\c\*.* C:\d\e\f
Of course, the dir command can be redirected to create a filelist.txt and then use that to carry out the copying in the order that the file dictates.
I thought there might be a way to skip the creation of the filelist.txt and do it all in one like
Code: Select all
for /f %%a in dir....
-
- Posts: 100
- Joined: 16 Dec 2016 22:31
Re: FOR command / batch including DIR
Squashman wrote:Sounak@9434 wrote:For the second one use this(untested)Code: Select all
for /f %%a in ("c:/a/b/c/filelist.txt") do (
cd "c:/d/e/f"
if exists %%a copy %%a "c:/g/h/i"
)
Sounak
You might want to account for spaces in file names.
Thanks Squashman, new code(untested):-
Code: Select all
for /f "delims=" %%a in ("c:/a/b/c/filelist.txt") do (
cd "c:/d/e/f"
if exists "%%a" copy "%%a" "c:/g/h/i"
)
Sounak
Last edited by Sounak@9434 on 05 Jan 2017 01:26, edited 1 time in total.
Re: FOR command / batch including DIR
Sounak@9434 wrote:Thanks Squashman, new code(untested):-Code: Select all
for /f "delims=" %%a in ("c:/a/b/c/filelist.txt") do (
cd "c:/d/e/f"
if exists %%a copy %%a "c:/g/h/i"
)
Sounak
Why not using backslashes?
Result (not related to above question):
Code: Select all
%a was unexpected at this time
-
- Posts: 100
- Joined: 16 Dec 2016 22:31
Re: FOR command / batch including DIR
drgt wrote:Why not using backslashes?
You mean using backslash as delimiter?
Re: FOR command / batch including DIR
I mean you use c:/a/b/c/filelist.txt instead of c:\a\b\c\filelist.txt
-
- Posts: 100
- Joined: 16 Dec 2016 22:31
Re: FOR command / batch including DIR
drgt wrote:I mean you use c:/a/b/c/filelist.txt instead of c:\a\b\c\filelist.txt
Sorry, I should have posted it from my pc(android is tricky for typing codes) though yeah my fault. Fixed one here:-
Code: Select all
for /f "delims=" %%a in ("c:\a\b\c\filelist.txt") do (
cd "c:\d\e\f"
if exist "%%a" copy "%%a" "c:\g\h\i"
)
Thanks drgt for pointing that out.
Sounak
Re: FOR command / batch including DIR
Sounak@9434 wrote:Thanks drgt for pointing that out.
Sounak
Never mind!
Lets see if someone has the answer to:
drgt wrote:The difference between copy every file in dir c:\a\b\c /a-d /on /b to c: \d\e\f ANDis that the latter will copy the files in "disk order" while the requirement is that they be copied in alphabetical order that the dir command specifies.Code: Select all
Copy C:\a\b\c\*.* C:\d\e\f
Of course, the dir command can be redirected to create a filelist.txt and then use that to carry out the copying in the order that the file dictates.
I thought there might be a way to skip the creation of the filelist.txt and do it all in one likeCode: Select all
for /f %%a in dir....
***That is why the title is "For ... including DIR
-
- Posts: 100
- Joined: 16 Dec 2016 22:31
Re: FOR command / batch including DIR
drgt wrote:I thought there might be a way to skip the creation of the filelist.txt and do it all in one likeCode: Select all
for /f %%a in dir....
***That is why the title is "For ... including DIR
In this case you are talking about the first request. Yeah the code can be achived by directly redirecting the output of dir command using 'singlequote'(maybe with usebackq and "double quote" for file name with spaces).
Sounak
Re: FOR command / batch including DIR
Can you be more specific please?
-
- Posts: 100
- Joined: 16 Dec 2016 22:31
Re: FOR command / batch including DIR
I am just replying based on the command you said earlier 'dir c:\a\b\c /a-d /on /b'
Code: Select all
for /f "delims=" %%a in ('dir c:\a\b\c /a-d /on /b') do (
copy "%%a" "c:\d\e\f"
)
Re: FOR command / batch including DIR
drgt wrote:The difference between copy every file in dir c:\a\b\c /a-d /on /b to c: \d\e\f ANDis that the latter will copy the files in "disk order" while the requirement is that they be copied in alphabetical order that the dir command specifies.Code: Select all
Copy C:\a\b\c\*.* C:\d\e\f
If you are only performing a copy, and one which will happen relatively quickly, why would the order matter? If I was copying the content of a script on here, the end product would look exactly the same if copied it from bottom to top or the opposite.