You are not logged in.

#1 17 Dec 2015 22:21

CMDneubie
Member
Registered: 17 Dec 2015
Posts: 5

search subfolders to launch file

let me get this out of the way... i am new to batch programming and dont know the language very well... still playing around but i like a challenge.

at work i constantly have to search for files and i wanted a way where i could search the file name and it would automatically open up in inventor.

i dont quite know how to search a folder and its subdirectories, find, and open a specific file.

see my program below and tell me how far off i am:
@echo off
title .IPT search and open
color 0c
::COMMENTS HERE   

:one
echo.
echo Enter CPR or MetaData number to search and open in AutoDesk Inventor
echo.
echo.
set /p fname=">>"
echo.
echo.
start %fname%.ipt [/d /s Z:\Raw_drawings] /max
timeout /t 2
if exist %fname%.ipt (
echo OPENING Inventor
    )else(
    echo THE FILE WAS NOT FOUND
    goto two)
timeout /t 10
goto one

:two
cls
echo.
echo.
echo SYSTEM CANNOT LOCATE FILE
echo.
echo.
timeout /t 0
goto one

i also tried to use dir "Z:\Raw_drawings" /s start %fname%.ipt but it doesnt work like desired

Offline

#2 18 Dec 2015 02:00

RG
Member
From: Minnesota
Registered: 18 Feb 2010
Posts: 362

Re: search subfolders to launch file

Your problem is )else(. Change it to ) else (.


Windows Shell Scripting and InstallShield

Offline

#3 18 Dec 2015 15:21

CMDneubie
Member
Registered: 17 Dec 2015
Posts: 5

Re: search subfolders to launch file

so i think ive found a better way to search but it is not finding my file and opening it up:

@echo off
title .IPT search and open
color 0c
::################   

:one
echo.
echo Enter CPR or MetaData number to search and open in AutoDesk Inventor
echo.
echo.
set /p fname=">>"
echo.
echo.
set var=dir "Z:\Raw_drawings" /B /S %fname%.ipt
cmd c/ %var%\%fname%
if exist %fname% (
echo OPENING Inventor
    ) else (
    echo THE FILE WAS NOT FOUND
    goto two)
timeout /t 10
goto one

:two
cls
echo.
echo.
echo     SYSTEM CANNOT LOCATE FILE
echo PLEASE CHECK THAT VALUE *%FNAME%* IS CORRECT
echo.
echo.
timeout /t 5
goto one

Offline

#4 18 Dec 2015 16:28

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: search subfolders to launch file

I think I see what you're trying to do there, the basic idea is sound, but your code is not valid. Try this instead:

@echo off
title .IPT search and open
set "root=Z:\Raw_drawings"
color 0c
::################   

:one
echo.
echo Enter CPR or MetaData number to search and open in AutoDesk Inventor
echo.
echo.
set /p fname=">>"
echo.
echo.
set "found="
REM search in directory structure for matching file names; open each of them
for /f "usebackq tokens=*" %%a in (`dir /b/s "%root%\%fname%.ipt"`) do (
    echo Found "%%~a"
    REM set a flag to indicate at least one file was found
    set found=1
    REM open file using default file association
    start "" "%%~a"
)

REM check for flag and alert user if no files were discovered
if not defined found (
   echo Nothing matched "%fname%"
)

timeout /t 10
goto one

Relies on having the file associated with the correct editor.
Please note, this will open all matches (IE if you have duplicate file names in different directories.

EDIT: fixed missing "(", with thanks to foxidrive

Last edited by bluesxman (21 Dec 2015 09:09)


cmd | *sh | ruby | chef

Offline

#5 18 Dec 2015 18:13

CMDneubie
Member
Registered: 17 Dec 2015
Posts: 5

Re: search subfolders to launch file

Thanks for the reply bluesxman - wow, i have a lot to learn!

i ran the code and the error 'the syntax of the command line is incorrect' appeared

i then added a space in line 17 between the /b and /s but that didnt change the error. i also looked at the commands to see if there were any immediate problems but couldnt spot one (then again i am new).
there is a lot of commands of FOR so i kind of got lost with the usebakq tokens and the %%A... anyway i tried to decipher most of it!

Offline

#6 20 Dec 2015 10:57

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: search subfolders to launch file

This line requires the last character added, to begin with.

for /f "usebackq tokens=*" %%a in (`dir /b/s "%root%\%fname%.ipt"`) do (

Offline

#7 21 Dec 2015 09:09

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: search subfolders to launch file

Thanks foxidrive; I should have mentioned that it was untested.  I'll edit my original post.


cmd | *sh | ruby | chef

Offline

#8 21 Dec 2015 14:44

CMDneubie
Member
Registered: 17 Dec 2015
Posts: 5

Re: search subfolders to launch file

with the "(" in the correct spot it works!

is there a way to open up only one instance rather than multiples? or set a date range? typically i will only open a file from within the last month and if there are more instances, they will be in the past.

Last edited by CMDneubie (21 Dec 2015 14:58)

Offline

#9 21 Dec 2015 16:07

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: search subfolders to launch file

This amendment to the "for" command will mean it opens only the most recently updated matched file
*** Untested ***

REM robocopy needs an empty (preferably non-existent) target directory so we can misuse it to generate a file list; it will NOT be created or otherwise touched
set "dummy_dir=%temp%\%date:/=%_%time:=%_%random%.dummy"
REM generate a file list including date stamp using robocopy; date is in yyyy/mm/dd HH:MM:SS format so a reversed sort makes newest file appear at the start of the list
for /f "usebackq tokens=1,2*" %%a in (`robocopy /s /l /ndl /njs /njh /np /nc /ts /ns "%root%." "%dummy_dir%." "%fname%.ipt" ^| sort /r`) do (
    REM don't do anything if we already found a match (IE only process first item in list)
    if not defined found (
        echo Found "%%~c"
        echo Dated %%a %%b
        REM set a flag to indicate a file was found
        set found=1
        REM open file using default file association
        start "" "%%~c"
    )
)

Last edited by bluesxman (21 Dec 2015 16:13)


cmd | *sh | ruby | chef

Offline

#10 21 Dec 2015 16:50

CMDneubie
Member
Registered: 17 Dec 2015
Posts: 5

Re: search subfolders to launch file

Error message reads:
=%_%random%.dummy"
was unexpected at this time
the syntax of the command is incorrect

@echo off
title .IPT search and open
set "root=Z:\Raw_drawings"
color 0c
::################   

:one
echo.
echo Enter CPR or MetaData number to search and open in AutoDesk Inventor
echo.
echo.
set /p fname=">>"
echo.
echo.
REM robocopy needs an empty (preferably non-existent) target directory so we can misuse it to generate a file list; it will NOT be created or otherwise touched
set "dummy_dir=%temp%\%date:/=%_%time:=%_%random%.dummy"
REM generate a file list including date stamp using robocopy; date is in yyyy/mm/dd HH:MM:SS format so a reversed sort makes newest file appear at the start of the list
for /f "usebackq tokens=1,2*" %%a in (`robocopy /s /l /ndl /njs /njh /np /nc /ts /ns "%root%." "%dummy_dir%." "%fname%.ipt" ^| sort /r`) do (
    REM don't do anything if we already found a match (IE only process first item in list)
    if not defined found (
        echo Found "%%~c"
        echo Dated %%a %%b
        REM set a flag to indicate a file was found
        set found=1
        REM open file using default file association
        start "" "%%~c"
    )
)
REM check for flag and alert user if no files were discovered
if not defined found (
   echo Nothing matched "%fname%" Check Value
)
timeout /t 5
goto one

Last edited by CMDneubie (21 Dec 2015 17:08)

Offline

#11 22 Dec 2015 21:13

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: search subfolders to launch file

Hmm .. try changing this

%time:=%

To this:

%time::=%

cmd | *sh | ruby | chef

Offline

Board footer

Powered by