-2

Basically, I am working on a large project, with some new concepts to challenge myself. I was planning on mapping out my computers directories and folders from what folder my document was in through HTML. Well, as it turns out (not surprisingly), I need some other files to do the mapping so that the HTML file can visualize it. I am stuck. Basically, I decided I would create a batch script that looks like this:

    echo off
    cd %cd%
    for /d %%a in (*) do dir /ad /on /s /b "%%a" >> get_dirs.txt

This is a simple but cool script that I discovered at https://grumpybear.wordpress.com/2011/05/22/batch-files-to-list-all-files-and-directories-in-a-folder/ Which describes the components of the script very nicely. The output text file looks something like this:

    C:\Users\Jason\Desktop\G3\DCIM\CardboardCamera
    C:\Users\Jason\Desktop\G3\DCIM\Facebook
    C:\Users\Jason\Desktop\G3\DCIM\Flickr
    C:\Users\Jason\Desktop\G3\DCIM\.thumbnails\otg
    C:\Users\Jason\Desktop\G3\Download\Adobe Acrobat
    C:\Users\Jason\Desktop\G3\Download\Adobe Reader
    C:\Users\Jason\Desktop\G3\Download\TrimbleMapBundles
    C:\Users\Jason\Desktop\G3\Pictures\Facebook
    C:\Users\Jason\Desktop\G3\Pictures\Instagram
    C:\Users\Jason\Desktop\G3\Pictures\Messenger
    C:\Users\Jason\Desktop\G3\Pictures\Old Phone

This is when the script is placed on the desktop. This is pretty much as far as I got when I came upon a serious problem. I needed to get these files to be displayed in an html document as

    <a href="file:///C:/Users/etc.">C:/Users/etc.</a>

So I was going to edit the echo output, silly me didn't realize that it wasn't using echo for output but instead the dir command. So I tried to edit the output with echo by sending the output as input to a variable. Which I'm sure there is solutions to on Batch - Command output to variable and Save command output to variable, but the answers on those pages are a little vague and somewhat confusing to someone with a little less experience. Plus, I'm not sure it matters, because the directories all come out as one full output, and not line by line (editable line by line). I'm not sure if it is possible to make one line after another in the output file into separate variables, which would be great. It would be helpful if I could make one line of the text document a variable, then the next, and so on. which I could the format my html links using the variables in separate links, by making a different output with something caused by:

    echo <a href="file:///%dir1%">%dir1%</a>
    echo <a href="file:///%dir2%">%dir2%</a>
    echo <a href="file:///%dir3%">%dir3%</a>
    echo <a href="file:///%dir4%">%dir4%</a>
    echo <a href="file:///%dir5%">%dir5%</a>

The script could calculate the amount of variables needed by reading how many lines are in the .txt file as seen here:How to count no of lines in text file and store the value into a variable using batch script?. If anyone can share any insight (I know this is a lot to ask for and a lot of information) I would greatly appreciate it. Mainly just getting the variables line by line is the main part. I think I might be able to get some of the other stuff. Thank You.

--Edit: So, I was kind of thinking, is there a way for the batch script to read the .txt file line by line? It seems there is but no one has really broken down the method. It all seems a little vague to me.

--Edit: I did some more thinking and looking around and came up with this code:

    @echo off
    set /a file=file
    cd %cd%
    for /d %%b in (*) do dir /ad /on /s /b "%%b" >> get_dirs.txt
    for /F "tokens=*" %%A in (get_dirs.txt) do set /a text=%%A
    echo <a href="%file%:///%text%">%text%</a>
    pause

This script, has something wrong with it, or as batch script output says, "Missing Operator," a bunch of times, then closes the window. I am not sure what is wrong with it but I do think I am getting somewhere. I think it may have to do with the angle brackets when echoing my html code, thinking it's trying to output or redirect the echo. Anyone know how I can solve this problem (assuming that it is the actual problem)?

Community
  • 1
  • 1

1 Answers1

0

<and > are commands to redirect in- or output. To disable this behaviour you have to escape ("disarm") those characters with a caret ^:

 echo ^<a href="%file%:///%text%"^>%text%^</a^>

Also your second for reassigns each line to the variable (one after the other), resulting in your varaible containing the last line only. Extend the for to contain your echo line:

 for /F "tokens=*" %%A in (get_dirs.txt) do (
     echo ^<a href="%file%:///%%A"^>%%A^</a^>
)

PS: are you sure set /a file=file does what you want? According to your desired output, you should just use set file=file

Stephan
  • 53,940
  • 10
  • 58
  • 91