# Solved: how to set variable in batchfile



## leescott (Nov 26, 2011)

I want to move files according to folder names.

1.Some folder names have been written in b2.txt.In my b2.txt,every line contains one or two or three words ,connected by space or "-".Like this:

transfer print
anti-foamer
insect
fibre reinforced plastic

2.My files is in "E:\JP-XIN\".

3.In E disk,there must existed one(only one) folder name containing one line in b2.txt.

My question is how to set exactly every line in b2.txt as variable.

In follow code,"%%k" is file name gotten by searching,"%%l" is path to the folder gotten by searching.The code worked not accurately.


```
@echo off
        setlocal ENABLEDELAYEDEXPANSION
        for /f "delims=" %%a in (b2.txt) do (
              set VAR=%%a
                 for /f "delims=" %%k in ('dir /s/b/a-d E:\JP-XIN\*.pdf E:\JP-XIN\*.txt  ^| findstr /i /c:"!VAR!"') do (
                    for /f "delims=" %%l in ('dir /s/b/a:d-h E:\ ^| findstr /i /c:"!VAR!"') do (
                       if not "%%l"=="" move "%%k" "%%~fsl"
        )))
        pause
```


----------



## Squashman (Apr 4, 2003)

I think I understand what you are trying to do. I will try and take a look at this when I get home from the Holiday weekend. Can't code from my phone. Well at least not until they make a VM for Android to run Windows on it.


----------



## leescott (Nov 26, 2011)

Thanks for your help! I'm waiting your code with pleasure .


----------



## Squashman (Apr 4, 2003)

After reading your explanation again and looking at your code I am not quite sure what you are trying to do. Could you explain a little better what your input looks like and what your output is suppose to look like.

Are you saying you have folder names listed in your B2.txt file and those are the only folders you want to search for PDF and text files inside?


----------



## Squashman (Apr 4, 2003)

Not really understanding what you are doing with that 3rd For Loop and If statement. You will end up finding any directories listed in your B2 text file and it will actually try to move the files to the same location when it gets to the JP-XIN directory.

You can get rid of the First For Loop all together and change the 2nd for loop to this.
I would change it to this.

```
pushd E:\JP-XIN
for /f "tokens=*" %%K in ('dir /b /s /a-d *.pdf *.txt ^|findstr /G:B2.txt') do (
```


----------



## leescott (Nov 26, 2011)

That code echoed a wrong result!Such as,b2.txt contains "water-proof",E disk contains folder "waterproof,water-proof,corrosion proof",but, files containing "water-proof or waterproof" were moved into "corrosion proof".
So,I think setting variable must have some questions.
I want to get all files in JP-XIN moved to a new folder (directory),files and folders all containing search phrase in b2.txt(exactly).
Thanks for your work!


----------



## Squashman (Apr 4, 2003)

I am sensing a language barrier here which is why I will usually ask for a more detailed explanation of what your input looks like and what you want your output to look like.


----------



## leescott (Nov 26, 2011)

Thanks for your reply!
I wish "!VAR!"=one line in b2.txt.
I have tryed such code,it didn't work rightly.
@echo off
for /f "delims=" %%k in ('dir /s/b/a-d E:\JP-XIN\*.pdf E:\JP-XIN\*.txt ^| findstr /r /g:b2.txt') do (
for /f "delims=" %%l in ('dir /s/b/a:d-h E:\ ^| findstr /r /g:b2.txt') do (
if not "%%l"=="" move "%%k" "%%~fsl"
))
pause


----------



## Squashman (Apr 4, 2003)

Not sure I can help you with your problem because I am not understanding what you are trying to do. I asked you twice in this thread to show me what your input looks like and what you want your output to look like. Both times I did not get a response to that request. I am sorry, but I don't think I can help you solve your problem.


----------



## leescott (Nov 26, 2011)

I'm sorry for my poor expression and understanding of English!I have thought that my expression already clear enough, you already understood.
I want to move files from "E:\JP-XIN" folder to other folder.Now ,I find aimed folder may have subfolder.
b2.txt :
water-proof
water proof
waterproof
aimed folder is "E:\*\water-proof" or "E:\*\waterproof" or "E:\*\water proof".
filename like this(txt OR pdf)
CN101333412-organic silicon rubber water-proof paint special for sheet metal roofing and method for preparing same
US3987258-=waterproof sound appts in sealed cabinet

The batchfile frutrated me!Perhaps giving up is the best means for me.
Thanks!May you have a nice *holiday!*


----------



## leescott (Nov 26, 2011)

http://forums.techguy.org/members/59575-squashman.htmlI find my code have some wrong and my txt designed imperfectly.Finally ,I get answer.Thanks Squashman and all other man to help me!Best wishes!


----------



## Squashman (Apr 4, 2003)

Please post your final code so that others can see how the problem was solved.


----------



## leescott (Nov 26, 2011)

Oh! I forget it.Thanks for your reminder.


> @echo off
> for /f "tokens=* delims=" %%a in (b2.txt) do (
> set VAR=%%a
> setlocal ENABLEDELAYEDEXPANSION
> ...


----------



## Squashman (Apr 4, 2003)

Still think you could have done this with two for loops.


----------

