1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: how to set variable in batchfile

Discussion in 'DOS/Other' started by leescott, Nov 25, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. leescott

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

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

    leescott Thread Starter

    Joined:
    Nov 25, 2011
    Messages:
    7
    Thanks for your help! I'm waiting your code with pleasure .
     
  4. Squashman

    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?
     
  5. Squashman

    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 (
     
  6. leescott

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

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

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

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

    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!
     
  11. leescott

    leescott Thread Starter

    Joined:
    Nov 25, 2011
    Messages:
    7
    I 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! [​IMG]
     
  12. Squashman

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

    leescott Thread Starter

    Joined:
    Nov 25, 2011
    Messages:
    7
    Oh! I forget it.Thanks for your reminder.
     
  14. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Still think you could have done this with two for loops.
     
  15. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1028448

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice