Page 1 of 2

FOR command / batch including DIR

Posted: 04 Jan 2017 09:30
by drgt
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

Re: FOR command / batch including DIR

Posted: 04 Jan 2017 10:29
by Compo
Possibly…

Code: Select all

Copy C:\a\b\c\*.* C:\d\e\f
…if you need something else please explain it better.

Re: FOR command / batch including DIR

Posted: 04 Jan 2017 11:42
by Sounak@9434
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

Re: FOR command / batch including DIR

Posted: 04 Jan 2017 13:26
by Squashman
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

Posted: 04 Jan 2017 22:34
by drgt
Compo wrote:Possibly…

Code: Select all

Copy C:\a\b\c\*.* C:\d\e\f
…if you need something else please explain it better.


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

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

Re: FOR command / batch including DIR

Posted: 04 Jan 2017 22:45
by Sounak@9434
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

Re: FOR command / batch including DIR

Posted: 04 Jan 2017 23:24
by drgt
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

Re: FOR command / batch including DIR

Posted: 05 Jan 2017 01:10
by Sounak@9434
drgt wrote:Why not using backslashes?

You mean using backslash as delimiter?

Re: FOR command / batch including DIR

Posted: 05 Jan 2017 01:24
by drgt
I mean you use c:/a/b/c/filelist.txt instead of c:\a\b\c\filelist.txt

Re: FOR command / batch including DIR

Posted: 05 Jan 2017 02:15
by Sounak@9434
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

Posted: 05 Jan 2017 02:59
by drgt
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 AND

Code: Select all

Copy C:\a\b\c\*.* C:\d\e\f
is 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.

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


***That is why the title is "For ... including DIR

Re: FOR command / batch including DIR

Posted: 05 Jan 2017 08:16
by Sounak@9434
drgt wrote: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....


***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

Posted: 05 Jan 2017 23:52
by drgt
Can you be more specific please?

Re: FOR command / batch including DIR

Posted: 06 Jan 2017 00:10
by Sounak@9434
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

Posted: 06 Jan 2017 02:58
by Compo
drgt wrote: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
is 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.

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.