Solved: how to set variable in batchfile

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

leescott

Thread Starter
Joined
Nov 25, 2011
Messages
7
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.

Code:
@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

Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
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.
 

Squashman

Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
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

Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
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.
Code:
pushd E:\JP-XIN
for /f "tokens=*" %%K in ('dir /b /s /a-d *.pdf *.txt ^|findstr /G:B2.txt') do (
 

leescott

Thread Starter
Joined
Nov 25, 2011
Messages
7
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

Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
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

Thread Starter
Joined
Nov 25, 2011
Messages
7
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

Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
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

Thread Starter
Joined
Nov 25, 2011
Messages
7
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!
 

Squashman

Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Please post your final code so that others can see how the problem was solved.
 

leescott

Thread Starter
Joined
Nov 25, 2011
Messages
7
Oh! I forget it.Thanks for your reminder.
@echo off
for /f "tokens=* delims=" %%a in (b2.txt) do (
set VAR=%%a
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%k in ('dir /s/b/a-d E:\JP-XIN\*.pdf E:\JP-XIN\*.txt ^| findstr /i/c:"!VAR!" 2^>nul') do (
for /f "tokens=*" %%l in ('dir /s/b/a:d-h E:\ ^| findstr /e/c:"!VAR!" 2^>nul') do (
if not "%%l"=="" move "%%k" "%%l\" >nul 2>nul
)))
pause
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Top