Setting variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Setting variables

#1 Post by drgt » 07 Mar 2017 02:25

In the following batch

Code: Select all

for /f "delims=" %%a in ('dir D:\Data\Test /a-d /on /b') do (
   copy "D:\Data\Test\%%a" "C:\Test"


I would like to set the source and target in the beginning of the file.
e.g
source=D:\Data\Test
target=C:\Test

and then modify the code accordingly.

Could you please show me?

-----------------------------------

For example, this does NOT work:

Code: Select all

set source=D:\Data\Test
set target=C:\Test

for /f "delims=" %%a in ('dir %source% /a-d /on /b') do (
   copy "%source%\%%a" "%target%"

set source=
set target=

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Setting variables

#2 Post by Compo » 07 Mar 2017 03:16

The line

Code: Select all

   copy "%source%\%%a" "%target%"

should read

Code: Select all

   copy "%source%\%%a" "%target%")

drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Setting variables

#3 Post by drgt » 07 Mar 2017 03:21

THANK YOU !!!

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Setting variables

#4 Post by Squashman » 07 Mar 2017 08:48

This isn't a problem with setting variables. You are basically not using parentheses correctly. Looking back at the code you were given in previous questions you were shown how to use parentheses correctly with the FOR command.

Regardless of that you could just put this all on one line and not bother with the parentheses.

Code: Select all

for /f "delims=" %%a in ('dir %source% /a-d /on /b') do copy "%source%\%%a" "%target%"

drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Setting variables

#5 Post by drgt » 09 Mar 2017 01:19

Thank you for pointing that out!

Post Reply