Using BATCH to find a file on your hard drive
by BiggAndyy on July 14th, 2011, in Windows AdministrationSounds easy, find a file on your hard drive to be used in your BATCH script. Usually you don’t need to. Microsoft keeps fairly well the directory structure from one version to the next. Usually. What about files like PRNPORT.VBS and all those files in printer administration?
In Windows XP they can be buried down 4 levels, but maybe only two. In VIST/7 maybe just 2, and in other versions if you have to deal with them, who knows if they are even there. Let’s face it, not every IT enabled business has up to date standards, machines, and uniform OS’s throughout.
Sometimes you have to write an awful lot of code to do a simple thing, like when a network printer changes it’s IP address, the printers are not on a print server, and all the mappings on the local machines are pointing to the IP address. Simple thing to write a script to change the IP address.
Not so much. Remember, I don’t have access to the AD tree, even though I am an administrator. Since I am considered an outside “contractor” by the powers that be (even though I am paid from the same pool as the general IT staff), I have much less access. So let’s try some guerrilla administration and solve this problem, writing a script that doesn’t care where PRNPORT.VBS lives, it will find it and use it.
Here is the code I cobbled together and I think it warrants passing along:
@echo off
@CLS
::
:: Initializing variables used
::
SETLOCAL ENABLEDELAYEDEXPANSION
SETLOCAL ENABLEEXTENSIONS
SET curdr=
SET myvar=
SET targetfile=
SET diritup=
::
:: Ask for file to find.
::
SET /P targetfile=”File to look for: ”
::
:: Initialize the target file,
:: the @ is important to keep the display uncluttered
::
@SET diritup=”dir /s /B %targetfile% 2>NUL”
::
:: Find the target file
:: It can take a while on a hard drive with a lot
:: of data. If you have a way to search faster
:: let me know.
ECHO Finding %targetfile% (This may take a few moments)…
ECHO Note this will return only the LAST file found if there
ECHO are multiple copies of the file.
set curdr=%cd%
cd\
for /f “tokens=*” %%a in (
‘%diritup%’
) do (
set myvar=%%a
)
cd\
cd %curdr%
IF “%MyVar%”==”" GOTO :FILENOTFOUND
ECHO.
ECHO Found the file found at
ECHO %myvar%
ECHO.
ENDLOCAL
Exit /b 0
:FILENOTFOUND
ECHO File Not Here.
ENDLOCAL
Exit /b 0
Wow. That’s a lot of typing to just do this: CSCRIPT PRNPORT.VBS -T -R %OLDPORT% -H %NEWIP%
Yeah, that’s what happens when you can’t depend on where PRNPORT.VBS lives. The script does a couple of things, first it remembers where you started the script, goes to the root of the drive, executes a DIR /B to return the file with it’s path (IMPORTANT) and finally switches back to the original directory to do whatever you want the rest of the code to do.
One trick built in, if there are multiple copies (say you type *.JPG) for the search term (this is a generic portion of the script I wrote), the only file displayed will be the last one found. To see all of the files insert ECHO %%a in the FOR loop just after SET MYVAR=%%a
You are asking, is this really necessary? Yeah, in order to pass a directory result to a variable WITHOUT using a temporary file, this is one way. You could also pipe a FINDSTR on the DIR command but I have found a FOR loop to be a bit faster and a smaller hit on the performance of the machine.
Copy it and give it a try.
139 views