Computing.Net > Forums > Programming > Can't check if input file was chose

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Can't check if input file was chose

Reply to Message Icon

Name: pball
Date: December 1, 2008 at 15:41:00 Pacific
OS: XP Pro
CPU/Ram: 2.8 ghz / 2 gig
Product: home made
Comment:

I have this batch script which does something which shouldn't be important. I want to check if a file was drug into the window and thus was selected.

It works when no file is entered, but when a file is entered it gives the error "or was unexpected at this time." which has me puzzeled.


@echo off

setlocal enabledelayedexpansion

echo drag and drop sfv onto window
set /p settings=
cls

if "%settings%" == "" goto fail

for /f "usebackq tokens=*" %%x in (%settings%) do (
call :check %%x
if /I not "!z:~0,8!" == "!z:~14,22!" echo %%x
set z=
)

goto end

:check
set x= %1 %2
:again

if "%x:~-1%" == "[" set s=%z% & goto :EOF

set z=%x:~-1%%z%

set x=%x:~0,-1%

goto again

:fail
echo no file selected

:end
pause



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: December 2, 2008 at 23:36:36 Pacific
Reply:

[Edit]
Try the "if not defined settings goto fail" bit first, especially if you file will always contain "["
[/Edit]


I suspect your problems are coming from trying to use variables that are undefined. Try adding this line just below the :again label.


if not defined x goto :eof

I can also see another problem as well, the first time you call check %z% is also undefined, so if the last character of %x% was "[" %s% would be set to nothing. It may be a good idea to promote the line "set z=%x:~-1%%z%" above the line above it, making any other adjustments necessary, to ensure it can deal with the possibility.

You could also use "if not defined settings goto fail" instead of "if "%settings%" == "" goto fail"

Without knowing more about your text file's contents and your objective I'm not sure if this will fix the problem or not.


0
Reply to Message Icon

Related Posts

See More


Batch file check files sp... Regex / Pattern Matching ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Can't check if input file was chose

C++ can you check if a file exists? www.computing.net/answers/programming/c-can-you-check-if-a-file-exists/11411.html

Can't put quotes around files names www.computing.net/answers/programming/cant-put-quotes-around-files-names/14321.html

Can't write object to file c++ www.computing.net/answers/programming/cant-write-object-to-file-c/17338.html