0

I am working on a document import utility that imports a text document from a specific location. The filename of the document is unknown and I need to find out how to determine the name of the file and make it a temporary value in my script so that it knows what the file name is so that it can ultimately open it in notepad automatically. I tried using *.txt to see if notepad would just open the first file matching this filter within the working directory but it doesn't work. How do I get and then set this unknown filename variable once the document has been copied to the working directory? Here is the code I have for the function so far:

@ECHO OFF

:CHECK
IF EXIST "*.txt" GOTO :AUTOEDIT
IF NOT EXIST "*.txt" GOTO :RUN

:AUTOEDIT
%SYSTEMROOT%\Notepad.exe "*.txt"
GOTO :END

:RUN
%SYSTEMROOT%\Notepad.exe
GOTO :END

END
2
  • 1
    I suppose you could check the folder repeatedly, say every ten seconds looking for a change in folder contents... Can you give us a better idea as to what you are trying to accomplish? The big picture? It's kind of hard to understand what you're really trying to do here.
    – Larryc
    Oct 5, 2019 at 2:07
  • I could try that, but I wouldn't know how @Larryc. By the time :AUTOEDIT is run the mystery file will already be present. Essentially, I need to get a specific file's name after it's imported to the folder this script is in, once imported I need to get the file's name then use that as a variable to run in conjunction with notepad. The document in question will have a timestamp based name, so it will be different whenever it has been imported to be edited. Oct 5, 2019 at 2:16

1 Answer 1

2

Create this batch file and save it in the folder where the misterious txt file is beeing copied to.

@echo off

:CHECK
for %%a in (*.txt) do (
set TxtFile=%%a
Goto :AUTOEDIT %TxtFile%
)

echo Checking for txt files every 10 seconds...
timeout /t 10 > Nul
cls
Goto :CHECK

:AUTOEDIT 
cls
echo.
echo Hello, I found a file called "%TxtFile%", Press any key to open it...
pause > Nul
%systemroot%\notepad.exe %Txtfile%

enter image description here

1
  • Thank you for your answer, it works exceptionally! Oct 20, 2019 at 20:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .