|
Answer» hey contrex thaks for ur contribution. can u just tell me hw to store the file path into a variable after displaying.looking into ur code i dont know where the path is getting stored before displaying.
plz help meIt is not getting stored anywhere. What you see is the output of DIR.
I explain the relevant batch file code
Code: [Select]REM ask user for filespec to use with dir /b /s set /p FILE="What is the file NAME (include extension)?"
REM For each letter of the alphabet for %%D 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 (
REM if a volume of that letter exists, perform DIR (file spec) /b /s on that volume REM drive letter is obtained by expanding loop variable %%D
if exist "%%D:\" DIR %%D:\"%FILE%" /b /s )
If you want the filename paths to appear in a variable you could redirect the output to a file and read the file
Code: [Select]@ECHO off
REM you need this to use variables in a loop setlocal enabledelayedexpansion
REM clear temp file first if exist %temp%\filesearch.txt del %temp%\filesearch.txt
REM ask user for filespec to use with dir /b /s REM possible inputs REM readme.txt REM *.doc REM file name with spaces.txt set /p FILE="What is the file name (include extension)? "
echo. echo searching... echo.
REM For each letter of the alphabet for %%D 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 (
REM if a volume of that letter exists, perform DIR (file spec) /b /s on that volume REM drive letter is obtained by expanding loop variable %%D
if exist "%%D:\" (
REM inform user of drive being SEARCHED echo searching drive %%D:\
REM perform DIR and redirect output to file DIR %%D:\"%FILE%" /b /s >> %temp%\filesearch.txt echo. )
)
echo. echo search finished. echo search results: echo.
REM (1) count number of matching files found REM use set /a to set counter to 0 REM shows how to count in batch set /a count=0
REM for each line in output file for /f "delims==" %%L in (%temp%\filesearch.txt) do ( set /a count+=1 )
REM (2) show results to user REM for each line in output file for /f "delims==" %%L in (%temp%\filesearch.txt) do (
REM make variable equal to found file path set filepath=%%L
REM show line to user REM must use ! and not % REM because of delayed expansion REM in loop echo !filepath!
) echo.
IF %count% EQU 1 echo %count% result found IF %count% GTR 1 echo %count% results found
echo this is the last match that was found echo. echo %filepath% echo.
Here, the found file and path is in the variable called "filepath"
If more than one match is found, only the last one will be stored at the end of the batch run.
Quote from: contrex on May 30, 2007, 11:52:12 AM It is not getting stored anywhere. What you see is the output of DIR.
I explain the relevant batch file code
Code: [Select]REM ask user for filespec to use with dir /b /s set /p FILE="What is the file name (include extension)?"
REM For each letter of the alphabet for %%D 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 (
REM if a volume of that letter exists, perform DIR (file spec) /b /s on that volume REM drive letter is obtained by expanding loop variable %%D
if exist "%%D:\" DIR %%D:\"%FILE%" /b /s )
If you want the filename paths to appear in a variable you could redirect the output to a file and read the file
Code: [Select]@echo off
REM you need this to use variables in a loop setlocal enabledelayedexpansion
REM clear temp file first if exist %temp%\filesearch.txt del %temp%\filesearch.txt
REM ask user for filespec to use with dir /b /s REM possible inputs REM readme.txt REM *.doc REM file name with spaces.txt set /p FILE="What is the file name (include extension)? "
echo. echo searching... echo.
REM For each letter of the alphabet for %%D 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 (
REM if a volume of that letter exists, perform DIR (file spec) /b /s on that volume REM drive letter is obtained by expanding loop variable %%D
if exist "%%D:\" (
REM inform user of drive being searched echo searching drive %%D:\
REM perform DIR and redirect output to file DIR %%D:\"%FILE%" /b /s >> %temp%\filesearch.txt echo. )
)
echo. echo search finished. echo search results: echo.
REM (1) count number of matching files found REM use set /a to set counter to 0 REM shows how to count in batch set /a count=0
REM for each line in output file for /f "delims==" %%L in (%temp%\filesearch.txt) do ( set /a count+=1 )
REM (2) show results to user REM for each line in output file for /f "delims==" %%L in (%temp%\filesearch.txt) do (
REM make variable equal to found file path set filepath=%%L
REM show line to user REM must use ! and not % REM because of delayed expansion REM in loop echo !filepath!
) echo.
IF %count% EQU 1 echo %count% result found IF %count% GTR 1 echo %count% results found
echo this is the last match that was found echo. echo %filepath% echo.
Here, the found file and path is in the variable called "filepath"
If more than one match is found, only the last one will be stored at the end of the batch run.
Hi Contrex, That was a great help from you .Thankyou so much. One more doubt.how can i get the file extension after storing the filepath into the variable.i want to just display the file extension after displaying the file path.do i have any option like give only the file name without extension and search for it.if its found then dispaly the path and extension of the file seperately?any help will be HIGHLY appreciated. thanx in advance
PrajithQuotedo i have any option like give only the file name without extension Use wildcards, as with DOS command line. EG to find files called "example" of whatever extension, supply
example.*
Quote and search for it.if its found then dispaly the path and extension of the file seperately? see modified code below
Code: [Select]@echo off
setlocal enabledelayedexpansion
REM clear temp file first if exist %temp%\filesearch.txt del %temp%\filesearch.txt
REM ask user for filespec to use with dir /b /s set /p FILE="What is the file name (include extension)? "
echo. echo searching... echo.
REM For each letter of the alphabet for %%D 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 (
REM if a volume of that letter exists, perform DIR (file spec) /b /s on that volume REM drive letter is obtained by expanding loop variable %%D
if exist "%%D:\" (
REM inform user of drive being searched echo searching drive %%D:\ for %FILE%
REM perform DIR and redirect output to file DIR /a-d %%D:\"%FILE%" /b /s >> %temp%\filesearch.txt echo. )
)
echo. echo search finished. echo search results: echo.
REM (1) count number of matching files found REM use set /a to set counter to 0 REM shows how to count in batch set /a count=0
REM for each line in output file for /f "delims==" %%L in (%temp%\filesearch.txt) do ( set /a count+=1 )
REM (2) show results to user REM for each line in output file for /f "delims==" %%L in (%temp%\filesearch.txt) do (
REM make variable equal to found file path set filepath=%%L
REM show line to user REM must use ! and not % REM because of delayed expansion REM in loop echo file path: !filepath! echo.
REM you will need to edit, this is just to REM show you how to use the file info
echo here are the different elements of echo information about the file echo. echo drive: %%~dL path: %%~pL filename: %%~nL extension: %%~xL echo. echo drive: %%~dL echo path: %%~pL echo filename: %%~nL echo extension: %%~xL echo drive, path and name: %%~dpnL echo.
) echo.
IF %count% EQU 1 echo %count% result found IF %count% GTR 1 echo %count% results found
echo this is the last match that was found echo. echo %filepath% echo.
Quote from: contrex on June 01, 2007, 01:30:17 AMQuotedo i have any option like give only the file name without extension Use wildcards, as with DOS command line. EG to find files called "example" of whatever extension, supply
example.*
Quote and search for it.if its found then dispaly the path and extension of the file seperately? see modified code below
Code: [Select]@echo off
setlocal enabledelayedexpansion
REM clear temp file first if exist %temp%\filesearch.txt del %temp%\filesearch.txt
REM ask user for filespec to use with dir /b /s set /p FILE="What is the file name (include extension)? "
echo. echo searching... echo.
REM For each letter of the alphabet for %%D 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 (
REM if a volume of that letter exists, perform DIR (file spec) /b /s on that volume REM drive letter is obtained by expanding loop variable %%D
if exist "%%D:\" (
REM inform user of drive being searched echo searching drive %%D:\ for %FILE%
REM perform DIR and redirect output to file DIR /a-d %%D:\"%FILE%" /b /s >> %temp%\filesearch.txt echo. )
)
echo. echo search finished. echo search results: echo.
REM (1) count number of matching files found REM use set /a to set counter to 0 REM shows how to count in batch set /a count=0
REM for each line in output file for /f "delims==" %%L in (%temp%\filesearch.txt) do ( set /a count+=1 )
REM (2) show results to user REM for each line in output file for /f "delims==" %%L in (%temp%\filesearch.txt) do (
REM make variable equal to found file path set filepath=%%L
REM show line to user REM must use ! and not % REM because of delayed expansion REM in loop echo file path: !filepath! echo.
REM you will need to edit, this is just to REM show you how to use the file info
echo here are the different elements of echo information about the file echo. echo drive: %%~dL path: %%~pL filename: %%~nL extension: %%~xL echo. echo drive: %%~dL echo path: %%~pL echo filename: %%~nL echo extension: %%~xL echo drive, path and name: %%~dpnL echo.
) echo.
IF %count% EQU 1 echo %count% result found IF %count% GTR 1 echo %count% results found
echo this is the last match that was found echo. echo %filepath% echo.
Hi Contrex,
Thanks for your tremendous help.i bothered you because it was a very critical situation . anyways thanx once again for your help.
Prajith
|