Page 1 of 1

how to define auto search file path.

Posted: 26 Dec 2013 03:45
by eddiechen
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

Re: how to define auto search file path.

Posted: 26 Dec 2013 06:23
by aGerman
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

Re: how to define auto search file path.

Posted: 26 Dec 2013 06:31
by foxidrive
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.

Re: how to define auto search file path.

Posted: 26 Dec 2013 07:47
by penpen
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.

Re: how to define auto search file path.

Posted: 26 Dec 2013 08:11
by foxidrive
penpen, it's probably a fair assumption to ignore drives A and B.

Re: how to define auto search file path.

Posted: 26 Dec 2013 08:43
by penpen
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

Re: how to define auto search file path.

Posted: 26 Dec 2013 09:00
by aGerman
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

Re: how to define auto search file path.

Posted: 26 Dec 2013 17:54
by foxidrive
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.

Re: how to define auto search file path.

Posted: 26 Dec 2013 18:50
by eddiechen
: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

Re: how to define auto search file path.

Posted: 26 Dec 2013 18:54
by eddiechen
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.

Re: how to define auto search file path.

Posted: 26 Dec 2013 21:17
by foxidrive
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"
)