You are not logged in.
Pages: 1
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
Your problem is )else(. Change it to ) else (.
Windows Shell Scripting and InstallShield
Offline
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
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
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
This line requires the last character added, to begin with.
for /f "usebackq tokens=*" %%a in (`dir /b/s "%root%\%fname%.ipt"`) do (
Offline
Thanks foxidrive; I should have mentioned that it was untested. I'll edit my original post.
cmd | *sh | ruby | chef
Offline
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
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
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
Hmm .. try changing this
%time:=%
To this:
%time::=%
cmd | *sh | ruby | chef
Offline
Pages: 1