how to define auto search file path.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
eddiechen
Posts: 7
Joined: 22 Dec 2013 20:50

how to define auto search file path.

#1 Post by eddiechen » 26 Dec 2013 03:45

hi gay :
i find a batch files. I modify some place.now i have a trouble about how to define search file path.
i hope first auto search file: 1.txt in c disk.
and relpace LastLogin to LastLogin=zl in 1.txt.
Who can help me.

Code: Select all

@echo off
set "filename=1.txt"
echo In the search, please wait...
    for %%a in (C) do (
      if exist %%a:\nul (
        for /f "delims=" %%b in ('dir /a-d /s /b "%%a:\*%filename%" 2^>nul') do (
           if /i "%%~nxb" equ "%filename%" (
             cd /d "%%a:\
            pause

               (for /f "delims=" %%o in ("%filename%") do (
                   for /f "delims==" %%d in ("%%o") do (
                   if %%d==LastLogin (
                   echo LastLogin=zl
                   )  else echo;%%o
                   )
                 ))>tme.txt
                move /y tme.txt 1.txt
                :del tme.txt
                start "" "%filename%"


              echo.%%b
             )
           )
         )
        )
    pause

aGerman
Expert
Posts: 4722
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how to define auto search file path.

#2 Post by aGerman » 26 Dec 2013 06:23

hi gay

How did you guess :lol: (just kidding)

Seriously:
I think you will have problems to determine if the root folder of the C drive exists. I'm virtually certain you can't check if the NUL device exists (even if it worked under very old Windows versions).

See what happens

Code: Select all

@echo off
if exist "C:\" (echo "C:\" exists) else echo "C:\" doesn't exist
if exist "C:\nul" (echo "C:\nul" exists) else echo "C:\nul" doesn't exist
pause


Regards
aGerman

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: how to define auto search file path.

#3 Post by foxidrive » 26 Dec 2013 06:31

This is correct for NT class Windows.

aGerman wrote:

Code: Select all

@echo off
if exist "C:\" (echo "C:\" exists) else echo "C:\" doesn't exist
pause


Regards
aGerman


The \NUL trick worked in MSDOS and Win9x.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: how to define auto search file path.

#4 Post by penpen » 26 Dec 2013 07:47

You should not check the existance of a drive by checking its root directory (or any other directories) existance:

Code: Select all

for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do @(
   if exist "%%a:\" echo %drive %%a exists
)
The problem is, that an exception may be thrown for any changeable device, similar to this:http://www.flickr.com/photos/siouxmoux/2088222355/

This depends on the drives driver, but for example the built in WinXP 3,5'' disk drive driver will throw this exception for sure if no disk is inserted
(i've never seen it not throwing this exception on such a situation, so i think it's sure).

So you should better do something like this:

Code: Select all

@echo off
setlocal enableDelayedExpansion
for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do @(
   dir "%%a:\" >nul 2>nul
   if "!errorlevel!" == "0" echo Disk drive %%a is ready.
)
endlocal
goto :eof

penpen

Edit: Fixed some flaws %errorlevel% to !errorlevel! and 0 to "0" and add the echo.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: how to define auto search file path.

#5 Post by foxidrive » 26 Dec 2013 08:11

penpen, it's probably a fair assumption to ignore drives A and B.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: how to define auto search file path.

#6 Post by penpen » 26 Dec 2013 08:43

I have a cdrom drive (+ driver) (pretty old), that throws this exception, too, if no cd rom is inserted:
AZTECH Zeta 6x SPeed LE.

But i haven't seen this behavior on any newer drives, so ignoring drive A and B may be sufficient nowadays.

penpen

aGerman
Expert
Posts: 4722
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how to define auto search file path.

#7 Post by aGerman » 26 Dec 2013 09:00

I thought of something like

Code: Select all

for /f %%i in ('mountvol^|find ":\"') do echo %%i

but I think penpen's solution is much better since I guess CD drives without disk would show up as well (currently I can't test it).

Regards
aGerman

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: how to define auto search file path.

#8 Post by foxidrive » 26 Dec 2013 17:54

aGerman wrote: I guess CD drives without disk would show up as well (currently I can't test it).


FWIW I tested it on a virtual cd drive and the T: drive didn't appear in the list when it was unmounted.

Code: Select all

if exist t:\ echo yes


didn't echo anything.

eddiechen
Posts: 7
Joined: 22 Dec 2013 20:50

Re: how to define auto search file path.

#9 Post by eddiechen » 26 Dec 2013 18:50

:lol:
thanks you for your reply.
this code can find the set of files on disk c .for example file path:c:\user\xx\appdat\locallow\xx.txt

@echo off
set "filename=1.txt"
echo In the search, please wait...
for %%a in (C) do (
if exist %%a:\nul (
for /f "delims=" %%b in ('dir /a-d /s /b "%%a:\*%filename%" 2^>nul') do (
if /i "%%~nxb" equ "%filename%" (
cd /d "%%a:\

My question in the following code
i think If found the specified file name is run this command,but i don't know how to define the path of this file.

(for /f "delims=" %%o in ("%filename%") do (
for /f "delims==" %%d in ("%%o") do (
if %%d==LastLogin (
echo LastLogin=zl
) else echo;%%o
)
))>tme.txt
move /y tme.txt 1.txt

aGerman wrote:
hi gay

How did you guess :lol: (just kidding)

Seriously:
I think you will have problems to determine if the root folder of the C drive exists. I'm virtually certain you can't check if the NUL device exists (even if it worked under very old Windows versions).

See what happens

Code: Select all

@echo off
if exist "C:\" (echo "C:\" exists) else echo "C:\" doesn't exist
if exist "C:\nul" (echo "C:\nul" exists) else echo "C:\nul" doesn't exist
pause


Regards
aGerman

eddiechen
Posts: 7
Joined: 22 Dec 2013 20:50

Re: how to define auto search file path.

#10 Post by eddiechen » 26 Dec 2013 18:54

thanks you for your reply.
this code can find the set of files on disk c .for example file path:c:\user\xx\appdat\locallow\xx.txt

@echo off
set "filename=1.txt"
echo In the search, please wait...
for %%a in (C) do (
if exist %%a:\nul (
for /f "delims=" %%b in ('dir /a-d /s /b "%%a:\*%filename%" 2^>nul') do (
if /i "%%~nxb" equ "%filename%" (
cd /d "%%a:\

My question in the following code
i think If found the specified file name is run this command,but i don't know how to define the Search the file path. thanks.

(for /f "delims=" %%o in ("%filename%") do (
for /f "delims==" %%d in ("%%o") do (
if %%d==LastLogin (
echo LastLogin=zl
) else echo;%%o
)
))>tme.txt
move /y tme.txt 1.txt



foxidrive wrote:
aGerman wrote: I guess CD drives without disk would show up as well (currently I can't test it).


FWIW I tested it on a virtual cd drive and the T: drive didn't appear in the list when it was unmounted.

Code: Select all

if exist t:\ echo yes


didn't echo anything.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: how to define auto search file path.

#11 Post by foxidrive » 26 Dec 2013 21:17

This uses a helper batch file called `repl.bat` - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

It is meant to search c: drive for 1.txt and if found it will replace LastLogin plus any characters following it with LastLogin=zl

Code: Select all

@echo off
for /f "delims=" %%a in ('dir "c:\1.txt" /b /a-d /s ') do (
   type "%%a" |repl "LastLogin.*" "LastLogin=zl" >"%%a.tmp"
move "%%a.tmp" "%%a"
)

Post Reply