Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Redirection of > to RAM vs Text File?  (Read 55912 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Redirection of > to RAM vs Text File?
« on: July 08, 2022, 05:36:40 PM »
Maybe it cant be done as google search turned up nothing on this, but was curious if there was a method to redirect the output from say DIR /B to RAM vs a text File as currently the batch that I have passes the output to a text file and then the contents of the text file is read in at the next step to pass to variable then that variable used further down the list of instructions. Was thinking if the output could be passed to memory and memory then passed to variable for easy recall in the instructions that that would then eliminate the clean up of temporary text files in the end. And when system is  shut down automatic garage control without having to have instructions to delete temp text files.

But perhaps only way to do this is by use of a RAM Drive or by using something other than batch to take in the string output and pass to variable.

I have no problems sticking with temporary use of text files to pass information between the output and variable, but its a curiosity as to if there is a better method out there. Setting up a RAM Drive still would require cleanup removal of temp text files so while thats in RAM and not written to a Disk or Drive its not as clean as passing to RAM direct.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Redirection of > to RAM vs Text File?
« Reply #1 on: July 09, 2022, 04:42:05 PM »
Hello DaveLembke,
   With  your ex[mortise you would know more that many of us.
   If speed is you concern, use a RAM drive and create the TXT on it.

Here is a GOOFBALL I just now did.
https://www.google.com/search?client=firefox-b-1-d&q=use+ram+drive+to+spped+up+batch+files

Did you already do that? Stack Overflow has a number of  hits. Here is one:
https://stackoverflow.com/questions/13480842/use-of-ram-disk-in-production-environment-to-speed-up-application-performance
Hoe this is of some help.  :D

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Redirection of > to RAM vs Text File?
« Reply #2 on: July 11, 2022, 11:24:21 PM »
Hey Geek-9pm after I drove home from work I was thinking more about it and was like maybe it can be passed to the clipboard and back out to set variables.

Researched today about this and clipboards functionality is a 1 way door from command shell to clipboard and there are no instructions that allow you to pass the clipboard contents to set a variable for use later in the batch. However others have wanted to do this and this article here has what would work for me maybe.  https://stackoverflow.com/questions/17819814/how-can-you-get-the-clipboard-contents-with-a-windows-command

Basically I need folder names of 4 folders that I get from DIR /B *static_folder_name where I am dealing with folders that have prepended alpha-numerics but the tail end of the folder names are static and unique among the rest of the folders in the root of where the project is. The unknown folder name is then known from getting it from DIR /B and passed to variable to be used later in batch. Doing this one at a time to |clip the output and pass the clipboard to a SET /p variable and then get the next folder name and do the same setting the variables and then the batch is populated with the 4 folder names and no writing to file and deletion of temp files.

So I tried out DIR /B *static_folder_name | clip and this passes the output to clipboard but cant do anything with it beyond that unless you use a tool like Paste.exe that someone created to be able to pass the clipboard contents back into command shell.

So I am going to play around with this. There is also a powershell method too that I am going to play around with that doesnt need the 3rd party Paste.exe to function but if working with older OS's that dont have power shell it would be neat to have the Paste.exe as a solution to that to earlier versions of Windows.

The project I have that handles the 4 folders works completely fine with the original method of writing to temp files and then the cleanup, but if I get this clipboard method to work I will implement that just because it seems cleaner than writing files of redirected output and deleting those files when complete.


BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Redirection of > to RAM vs Text File?
« Reply #3 on: July 12, 2022, 01:17:37 PM »
    for /f "" %%A in ('dir /b') do <command>

This will run <command> for every line in the bare directory output, with %%A being the value.

You might want that for going through the bare directory output. and iterating over folders. My approach is usually to have it call another batch file in the command portion. passing %CD%\%%A or something similar should provide the full path to the "sub-batch" file which can do additional processing from it's %1 argument.

Note that something like:

    for /f "" %%A in ('dir /b') do set testvar=%%A

will set the testvar variable to the last line of data in the output (it sets it to each one in turn and ends up on the last one). If a command gives just a single line of output it will set it to that line. A dir /ad /b in a directory with only one subdirectory will set the testvar variable to the name of that directory for example.
I was trying to dereference Null Pointers before it was cool.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Redirection of > to RAM vs Text File?
« Reply #4 on: July 14, 2022, 10:25:23 PM »
Thank You BC for that... that looks far cleaner than the other methods. I didn't know that the output from FOR running the DIR/ B could be passed like that to the command. I had always thought that in order for a FOR statement to operate properly in command run with it that string content -or- values had to be predefined before the FOR statement was called for it to test against. So cool that you can generate what you need passed to %%A and then DO the command with the data that was gotten from DIR / B through the FOR argument.

Definitely going to implement this method and toss away the clipboard and other methods of passing to temp file as this is so much cleaner and no temp text files needed.  8)